Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show different menu options on Toolbar for activity and fragment

Android Studio 1.4

I have a Toolbar that I inflate in my activity_main.xml. I have a menu called main.xml that gets inflated that has just 1 icon to display on it.

When the user clicks to open a fragment. I have another menu friends.xml that has 2 icons.

When I inflate the friends menu in the fragment it still displays the icon from the main.xml menu.

I thought that inflating a new menu on the toolbar would remove the existing menu.

This is a screenshot of the main.xml menu. The find icon is displayed enter image description here

This is the screenshot of the fragment as you can see the find icon is still there.

enter image description here activity_main.xml with toolbar included

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <include
            android:id="@+id/tbMain"
            layout="@layout/app_bar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

        <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clickable="true"/>
    </LinearLayout>
</android.support.v4.widget.DrawerLayout>

here is my code for create the menu in MainActivity.java

    private void setupToolBar() {
        mToolbar = (Toolbar)findViewById(R.id.tbMain);
        setSupportActionBar(mToolbar);

        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

 @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return super.onOptionsItemSelected(item);
    }

And in my fragment I have this, as you can see I am inflating the friends.xml menu.

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

Many thanks for any suggestions,

like image 357
ant2009 Avatar asked Oct 06 '15 10:10

ant2009


People also ask

How do I get the activity toolbar in fragment?

if you are using custom toolbar or ActionBar and you want to get reference of your toolbar/action bar from Fragments then you need to first get instance of your Main Activity from Fragment's onCreateView Method like below. ImageView vRightBtn = activity. toolbar.

Can I add toolbar in fragment?

Though you can add a Toolbar anywhere within your fragment's view hierarchy, you should generally keep it at the top of the screen. To use the Toolbar in your fragment, provide an ID and obtain a reference to it in your fragment, as you would with any other view.


2 Answers

I'm not sure if you can do this with onCreateOptionsMenu(). I think your better bet will be onPrepareOptionsMenu().

You can force Android to refresh the options menu by simply writing getActivity().invalidateOptionsMenu() in Fragment's onResume().

So your onPrepareOptionsMenu() will look like:

@Override
public void onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);
    menu.clear();    //remove all items
    getActivity().getMenuInflater().inflate(R.menu.menu_fragment, menu);
}
like image 181
Sufian Avatar answered Oct 16 '22 19:10

Sufian


Store the menu reference in a variable.

 private Menu menu;

       @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.main, menu);
            this.menu = menu;
            return super.onCreateOptionsMenu(menu);
        }

When replacing to mentioned fragment do the following.

private void hideOption(int id) {
        menu.findItem(id).setVisible(false);
    }

Call hideOption() with menu id. for ex,

hideOption(R.id.action_search);

And vice versa for showing.or follow suggestion of #Droidwala

like image 3
Anoop M Maddasseri Avatar answered Oct 16 '22 19:10

Anoop M Maddasseri