best way to show the three dot menu item in action bar without using “a hack” method [closed]

风格不统一 提交于 2019-12-05 19:54:04

It's simple. Some devices have menu buttons others don't. On devices with menu buttons the overflowing action items are shown when pressing the menu button and on devices without they are shown when pressing the three dots. That's how the device manufacturer designed their devices and any attempt to force them to do it differently is regarded a "hack". It's inconsistent because other apps on the same device do it differently.

It all comes down to the question whether you want to have consistency for a single app across different devices or consistency for all apps on a single device. Because app development should be about users in the end, IMO the consistency on a single device (for one user) is what really counts.

The best way to deal with this is to use the android:showAsAction tags and let Android decide, how to display the actions in the ActionBar, whether an overflow menu is needed or not and if yes how the user accesses the overflowing action items.

The Overflow menu is not shown for devices with a menu button.

The code you used will work fine on a more recent device and the overflow (three dots) will be displayed. As intented, these are not shown when there is a menu button

There is no way without a dirty hack that will break soon or later to force these dots to be dsiplayed on all devices

sravan953

So, turns out it's pretty simple, I recently implemented in my app.

The items that need to be shown in the overflow menu, nest them under one menu item as follows:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/empty"
        android:orderInCategory="101"
        android:showAsAction="always"
        android:icon="@drawable/ic_action_overflow">

        <menu>        

            <item
                android:id="@+id/action_settings"
                android:orderInCategory="96"
                android:showAsAction="never"
                android:title="@string/menu_settings"
                android:icon="@drawable/ic_action_settings"/>

            <item
                android:id="@+id/action_share"
                android:orderInCategory="97"
                android:showAsAction="never"
                android:title="@string/menu_share"
                android:icon="@drawable/ic_action_share"/>

            <item
                android:id="@+id/action_rate"
                android:orderInCategory="98"
                android:showAsAction="never"
                android:title="@string/menu_rate"
                android:icon="@drawable/ic_action_important"/>

            <item
                android:id="@+id/action_feedback"
                android:orderInCategory="99"
                android:showAsAction="never"
                android:title="@string/menu_feedback"
                android:icon="@drawable/ic_action_edit"/>

            </menu>         
        </item>
</menu>

Now, edit the main activity file as follows:

package com.example.test;
//all your import statements go here

Menu mainMenu=null;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState); }

@Override
public boolean onCreateOptionsMenu(Menu menu) {     
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
mainMenu=menu;
return true; }


//Menu press should open 3 dot menu
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode==KeyEvent.KEYCODE_MENU) {
        mainMenu.performIdentifierAction(R.id.empty, 0);
        return true; }
    return super.onKeyDown(keyCode, event); }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!