How to set menu items in a fragment toolbar?

10,909

Solution 1

may be its too late, but just solved the same issue and think you would like to know. all you nee is just set up toolbar for activity

  ((MainActivity) getActivity()).setSupportActionBar(toolbar);
        setHasOptionsMenu(true);

this will trigger onCreateOptionsMenu in fragment

Solution 2

You can set an ImageView in Toolbar and open a popup menu when ImageView is clicked and them handle the menu items clicks in popup menu.

<android.support.v7.widget.Toolbar
            app:layout_collapseMode="pin"
            android:fitsSystemWindows="false"
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize">


            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent>

                <ImageView
                    android:layout_marginRight="8dp"
                    android:id="@+id/overflow_menu"
                    android:clickable="true"
                    android:background="?attr/selectableItemBackgroundBorderless"
                    app:srcCompat="@drawable/overflow_menu"
                    android:layout_centerVertical="true"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"/>

            </RelativeLayout>

</android.support.v7.widget.Toolbar>

Inside fragment

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

 //Other stuff  

    ImageView overflowMenuImageView = view.findViewById(R.id.overflow_menu);
    overflowMenuImageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            PopupMenu popupMenu = new PopupMenu(getActivity(), view);
            popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    if(item.getItemId() == R.id.menu_item_id){

                        //Do your thing

                    }
                    return false;
                }
            });
            popupMenu.inflate(R.menu.my_menu);
            popupMenu.show();
        }
    });
}
Share:
10,909
Bishwajyoti Roy
Author by

Bishwajyoti Roy

Updated on June 14, 2022

Comments

  • Bishwajyoti Roy
    Bishwajyoti Roy almost 2 years

    I have a fragment in my Activity and the fragment has its own toolbar. Like this:

    Image

    Here is the layout of the fragment:

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.hometsolutions.space.Fragments.ControlFragment">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <android.support.v7.widget.Toolbar
                android:id="@+id/Setup_next_toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="?attr/colorPrimary"
                android:elevation="4dp"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
    
            <android.support.v7.widget.RecyclerView
                android:id="@+id/recycler_view_setup_next"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_constraintTop_toBottomOf="@+id/setup_next_recycler" />
    
        </LinearLayout>
    </FrameLayout>
    

    I want to add menu on the Setup_next_toolbar. Not on the MainActivity toolBar.

    I did this on the fragment:

    on the onCreate: setHasOptionsMenu(true);

    then

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.Setup_next_menu, menu);
    }
    

    but it added the menu on the MainActivity tolbar. How can I set menuItems on the Setup_next_toolbar?

  • Bishwajyoti Roy
    Bishwajyoti Roy over 7 years
    This will work on activity but getMenuInflater() doesn't work on Fragments
  • Varad Mondkar
    Varad Mondkar over 5 years
    This help me with I called above code in onCreateView or onActivityCreated of my Fragment. But thinking what was the exact reason for hidden menu icons.
  • MD.Riyaz
    MD.Riyaz about 4 years
    its work. solved my problem. thank you so much. save my times.
  • iamkdblue
    iamkdblue almost 4 years
    wow, Thank you so much for this answer, found after searching 4 hours