4

I just created an app including the new Android toolbar. My problem is: How would I hide this overflow menu icon/button: http://prntscr.com/62mmus ? I already tried this, but it's not working:

        Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
        mToolbar.hideOverflowMenu();
dahwN
  • 67
  • 1
  • 1
  • 8
  • 2
    "How would I hide this overflow menu icon/button" -- don't put any action items in the action bar. Remove your `onCreateOptionsMenu()` and `onOptionsItemSelected()` methods, along with the `menu` resource(s) that they use. Then, the overflow should never show up. – CommonsWare Feb 08 '15 at 17:34

7 Answers7

23

Just use:

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
        return false;
}
Leebeedev
  • 2,126
  • 22
  • 31
3

This is how I did it.

This is my original menu_main.xml file under the menu folder:

<menu 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" tools:context=".MainActivity">
    <item android:id="@+id/action_settings" android:title="@string/action_settings"
        android:orderInCategory="100" app:showAsAction="never" />
</menu>

Delete the item:

    <item android:id="@+id/action_settings" android:title="@string/action_settings"
        android:orderInCategory="100" app:showAsAction="never" />

Go to your main activity and comment out the following if (id == R.id.action_settings) :

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
   // if (id == R.id.action_settings) {
   //     return true;
   // }

    return super.onOptionsItemSelected(item);
}

Run your app - the overflow menu icon is gone.

Simon
  • 19,658
  • 27
  • 149
  • 217
2

what worked for me was: add the following: android:visible="false" to the menu item in the menu file (global.xml) in the menu folder.

ex:

 <item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    android:showAsAction="never"
    android:title="@string/action_settings"
    android:visible="false"

    />
1

Just return false when calling the method onCreateOptionsMenu

Ex:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    if (!showToolbarMenu)
        return false;
    getMenuInflater().inflate(R.menu.main_activity, menu);
    return super.onCreateOptionsMenu(menu);
}
Amit Shadadi
  • 617
  • 2
  • 7
  • 20
0

Use the command invalidateOptionsMenu();

And your problems will be solved.

madlymad
  • 6,367
  • 6
  • 37
  • 68
Mohit Vyas
  • 21
  • 1
  • Please consider adding additional details as to why this is the correct answer. – David L Apr 22 '17 at 04:10
  • This works if neither the current fragment nor activity override onCreateOptionsMenu, as it forces the system to try to inflate the menu again. Not finding any menu to show, it will then hide the icon. – Nicolò Parolini Sep 27 '19 at 03:33
0

This worked for me, I just deleted the line getMenuInflater().inflate(R.menu.main_activity, menu);

4star
  • 1
0

Don't implement the onCreateOptionsMenu(Menu menu) method. If you do anyway ,don't use this method getMenuInflater().inflate(R.menu.main, menu); as this line in the onCreateOptionsMenu method inflates the overflow button with the menus declared in resource R.menu.main.

Xenon Kfr
  • 1,925
  • 1
  • 15
  • 13