Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overflow not showing up in Action Bar

I have 5-6 items in my Action Bar. When I set ShowAsAction = "never", the items goes into the old styled menu which appears from bottom of the screen while I want the three dot styled icon to appear on Action Bar. Also when I click on it, the menu doesn't appear.

My menu file -

    <menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item 
     android:id="@+id/About"
     android:title="About"
     android:orderInCategory="1"
     android:showAsAction="always"/>


<item 
    android:id="@+id/Settings"
    android:title="settings"
    android:orderInCategory="2"
    android:showAsAction="always"/>

<item 
    android:id="@+id/item3"
    android:title="Item3"
    android:icon="@drawable/ic_launcher"
    android:orderInCategory="3"
    android:showAsAction="always"/>
<item 
    android:id="@+id/item4"
    android:title="Item3"
    android:icon="@drawable/ic_launcher"
    android:orderInCategory="4"
    android:showAsAction="always"/>

<item 
    android:id="@+id/item5"
    android:title="Item3"
    android:icon="@drawable/ic_launcher"
    android:orderInCategory="5"
    android:showAsAction="never"/>

<item 
    android:id="@+id/item6"
    android:title="Item3"
    android:icon="@drawable/ic_launcher"
    android:orderInCategory="6"
    android:showAsAction="never"/>

</menu>
like image 765
Priyesh Avatar asked Nov 28 '22 08:11

Priyesh


2 Answers

If you have a hard menu button on your device then the menu options that don't fit on the ActionBar will be placed on your device's menu button. If you have a newer device without this button then they will be placed in the overflow menu (the 3 vertical dots)

See Creating an Options Menu

I created a custom layout for a custom ActionBar so that I could have the same sort of look and functionality across all devices. You may consider doing the same thing if this is what you want.

like image 170
codeMagic Avatar answered Dec 04 '22 15:12

codeMagic


Android disabled showing the overflow icon on devices which have physical menu button, for some of you who still want it,here is a solution:

add this function to your activity:

private void forceShowActionBarOverflowMenu() {  
    try {  
        ViewConfiguration config = ViewConfiguration.get(this);  
        Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");  
        if (menuKeyField != null) {  
            menuKeyField.setAccessible(true);  
            menuKeyField.setBoolean(config, false);  
        }  
    } catch (Exception e) {  
        e.printStackTrace();  
    }  
}  

and finally you add "forceShowActionBarOverflowMenu();" right after setContentView() in onCreate() in your activity.

like image 26
user3097625 Avatar answered Dec 04 '22 14:12

user3097625