Sara Lince Sara Lince - 2 months ago 14
Java Question

onClick method in fragment never gets called

I tried to set up a 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 never gets called when I hit my button? Did I miss anything?

Answer

EDIT

Since Sevin made me notice I was not giving you a solution but an alternative way for reaching your goal, I'm editing this.

First: your code must work

The code you posted is correct

Now, do some checks:

  • Check if in debug mode the button is not null (should have throwed exception)
  • Check if in debug mode you are reaching the onClick, even if the text doesn't change and even if the toast doesn't show up.
  • Check if you are showing the fragment properly. Here is the official documentation about how to create a fragment.
  • Check if the fragment is clickable and if it has anything over it (simply, the button once clicked shows the classic "click" animation?)
  • Check in the xaml code if the id you used is assigned to the correct button.

And finally do some the following operations:

  • Clean your project
  • Rebuild your project
  • Check if in both xaml and java code you have any alert (in basic configurations, yellow mark over the line)
  • Uninstall the app and re-install it (pretty much the same, but since we don't know your project, it can still help)

After those, let us know

Possible solution:

You have a recycleview which is on top of the button. you have this view set with match parent both height and width inside a relativelayout, it means that this view will cover the button.

Remove this empty recycleview and the code will work