6

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
  • 71
  • 1
  • 1
  • 4
  • you just need to add setHasOptionMenu(true) in onCreateView() of your fragment. http://stackoverflow.com/a/34082236/5722385 – Mrinmoy Dec 17 '16 at 05:51
  • I can't see any answer solving the problem you described. Did you manage to get it working? If so, how? – ptrico Mar 21 '19 at 10:49

3 Answers3

11

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

Ivan Karpiuk
  • 189
  • 2
  • 4
  • 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. – Varad Mondkar Aug 22 '18 at 07:22
3

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();
        }
    });
}
Rahul Sahni
  • 465
  • 4
  • 12
-2

Try this:

public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setHasOptionsMenu(true);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
  //TODO Add your menu entries here
  inflater.inflate(R.menu.menu, menu);
  super.onCreateOptionsMenu(menu, inflater);
}
Ian Wambai
  • 203
  • 3
  • 19
Hasan shaikh
  • 738
  • 1
  • 6
  • 16