Search code examples
androidandroid-actionbarandroid-actionbar-compat

Showing "More Menu Items" on top right drop down menu with Android ActionBarCompat


I am developing an Android app with ActionBarCompat (released by google). In tablets,it shows more menu items as a dropdown menu at top right (Image 1) but in small devices (handsets) you should hit menu button to see more items (Image 2).

enter image description here

enter image description here

Is there any way to have top right dropdown menu like tablets in handset devices? If yes, how? Thank you.


Solution

  • Sure you can !

    First of all, make a menu.xml in res > menu.

    <menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:technicachat="http://schemas.android.com/apk/res-auto"
        android:layout_width="wrap_content"
        android:layout_height="match_parent">
    
        <item 
            android:id="@+id/dropdown_menu"
            android:showAsAction="ifRoom"
            technicachat:showAsAction="ifRoom"
            android:title="Options"
            android:icon="@drawable/ic_menu_moreoverflow_normal_holo_light">
    
            <menu>
                <item
                    android:id="@+id/item1"
                    android:showAsAction="ifRoom"
                    technicachat:showAsAction="ifRoom"
                    android:title="Refresh"
                    android:icon="@drawable/ic_menu_refresh"/>
            </menu>
        </item>
    </menu>
    

    All items under the second menu will appear when you click on the dropdown menu button. You don't even need to use onOptionsItemSelected to drop it.

    Just proceed as usual to handle item selection.

    @Override
    public boolean onOptionsItemSelected(MenuItem item) 
    {   
        switch (item.getItemId())
        {
            case R.id.item1: 
            /*DO STUFF*/
        }
    
        return super.onOptionsItemSelected(item);
    }
    

    In case you want pngs for your apk, they are under /your sdk path/platforms/android-xx/data/res/ and you got drawable-hdpi, mdpi etc. For example the one you need is ic_menu_moreoverflow_normal_holo_light.

    Here you are ! ;)