Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set menu items in a fragment toolbar?

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?

like image 970
Bishwajyoti Roy Avatar asked Dec 16 '16 21:12

Bishwajyoti Roy


Video Answer


2 Answers

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

like image 85
Ivan Karpiuk Avatar answered Oct 20 '22 23:10

Ivan Karpiuk


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();
        }
    });
}
like image 3
Rahul Sahni Avatar answered Oct 21 '22 00:10

Rahul Sahni