0

I have 3 tabs which are fragments and I have a floating action button on one fragment. When I click it nothing happens. I'm pretty new to Android and this is my first attempt at tabs. I really don't understand what Toast wants as the first parameter and why, or why it's not working.

Here's the applicable Java method...

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.fragment_my_list, container, false);
    FloatingActionButton fab = (FloatingActionButton) v.findViewById(R.id.floatingActionButton);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Toast.makeText(getActivity(),"test", Toast.LENGTH_LONG).show();           
        }
    });

    return inflater.inflate(R.layout.fragment_my_list, container, false);
}

Here's my .xml file regarding the floating action button...

<android.support.design.widget.FloatingActionButton
        android:id="@+id/floatingActionButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_marginBottom="13dp"
        android:layout_marginEnd="14dp"
        android:clickable="true"
        android:src="@drawable/ic_action_add_exercise"
        app:backgroundTint="@color/colorMyBlue" />
Matt
  • 51
  • 5
  • You need to return the `View` you inflated and set up at the beginning of `onCreateView()`; i.e., `return v;`. As you have it now, you're returning a completely different instance, one that you've not set an `OnClickListener` on. – Mike M. May 13 '18 at 21:56
  • 1
    Thank you! Works now. Thanks for helping a newbie out. – Matt May 13 '18 at 22:13

0 Answers0