0

I have menu xml file in menu->main.xml in which two item are available. I have two activities and i want to add this two item in overflow menu in first activity but in the second activity i want to add only one item in overflow menu.

menu->main.xml

<?xml version="1.0" encoding="utf-8"?>
<menu
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/action_refresh"
        android:title="@string/action_refresh"
        app:showAsAction="never"/>
    <item
        android:id="@+id/action_setting"
        android:title="@string/action_setting"
        app:showAsAction="never"/>
</menu>

So how to do that?

1
  • 1
    if you give app:showAsAction="never" means those menu option not at all visible in action bar .. Commented Jan 17, 2017 at 8:07

4 Answers 4

4

you can do it in two ways:

  1. Have separate menu XMLs for both activities. one with both items and other with only one that you require.

  2. remove the unneeded menu item in code inside 2nd activity, by overriding onCreateOptionsMenu e.g:

        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.main,menu);
            menu.removeItem(R.id.action_setting);
            return super.onCreateOptionsMenu(menu);
        }
    
Sign up to request clarification or add additional context in comments.

Comments

0

you could do it programmatically by hiding it...For example

MenuItem refreshIcon = menu.findItem(R.id. action_refresh);
refreshIcon.setVisible(false);

Comments

0

Get a reference to your menu item in onCreateOptionsMenu(menu) and update show as action as below in your second activity.

MenuItem menuitem = menu.finditem(your_id);
menuitem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

Comments

0

You can use the onCreateOptionsMenu that you'll have to override. You could try something like that, depending on what you need to show here:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    menu.getItem(0).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.