The onClick method in the fragment is never called

I tried setting onClickListener inside my fragment.

public class HomeFragment extends Fragment implements View.OnClickListener { Button btn_eventList; public HomeFragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_home, container, false); btn_eventList = (Button) view.findViewById(R.id.buttonEventList); btn_eventList.setOnClickListener(this); return view; } @Override public void onClick(View v) { btn_eventList.setText("TEST"); Toast.makeText(getActivity(), "TEST" , Toast.LENGTH_SHORT).show(); } } 

Here is my layout:

 <?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/nvd" android:layout_height="match_parent" android:layout_width="match_parent"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/textView2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:fontFamily="sans-serif" android:gravity="center" android:text="test" android:textAppearance="@style/TextAppearance.AppCompat.Display1" /> <TextView android:id="@+id/textView4" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/textView2" android:layout_centerHorizontal="true" android:layout_marginTop="1dp" android:gravity="center" android:text="test" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_alignParentTop="true" android:layout_marginTop="45dp" android:orientation="horizontal"> <Button android:id="@+id/buttonEventList" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="Button" /> <Button android:id="@+id/button9" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="Button" /> <Button android:id="@+id/button7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="Button" /> </LinearLayout> <android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:layout_height="wrap_content"> </android.support.v7.widget.RecyclerView> </LinearLayout> </RelativeLayout> <android.support.v7.widget.RecyclerView android:id="@+id/lst_nav_drawer" android:layout_width="match_parent" android:layout_height="match_parent" app:layoutManager="android.support.v7.widget.LinearLayoutManager" > </android.support.v7.widget.RecyclerView> </android.support.v4.widget.DrawerLayout> 

Does anyone know why the onClick method is never called when I click the button? Did I miss something?

+5
source share
3 answers

EDIT

Since Sevin noticed that I am not giving you a solution, but an alternative way to achieve your goal, I have been editing this.

First: your code should work

Correctly executed code

Now do a few checks:

  • Make sure the button is not zero in debug mode (an exception must be thrown)
  • Make sure in debug mode you reach onClick, even if the text does not change and even if the toast does not appear.
  • Check if you are showing the fragment correctly. Here is the official documentation on how to create a snippet.
  • Check if the fragment is available, and if it has something (just the pressed button shows the classic "click" animation?)
  • Check the xaml code if the correct button is assigned to the identifier you specified.

And finally, perform the following operations:

  • Clean up project
  • Rebuild your project
  • Check if you have any signal in the xaml and java code (in the basic configurations, a yellow sign above the line)
  • Uninstall the application and reinstall it (almost the same, but since we don’t know your project, it can still help)

After that, let us know

Possible Solution:

You have a recycleview that sits on top of the button . you have this view with match parent both height and width inside relativelayout , this means that this view will span the button.

Remove this empty recycleview and the code will work

+4
source

EDIT

after publishing your layout, I can conclude with complete certainty that your REcyclerView occupies the entire space of your layout, thereby accepting all the touch inputs from what it costs.

Solutions:

You can either delete the recycler view, or reinstall it, or add it to the last RelativeLayout and use layout_below and place it below LinearLayout

+1
source

The problem is that you did not implement this method correctly. onClick() method does not know which view was specifically made by clicking. Change the onClick () method to the following form:

 @Override public void onClick(View v) { switch (v.getId()) { case R.id.your_button_id: btn_eventList.setText("TEST"); Toast.makeText(getActivity(), "TEST" , Toast.LENGTH_SHORT).show(); break; } } 
-1
source

Source: https://habr.com/ru/post/1257528/


All Articles