简体   繁体   中英

Android Overflow Menu Button is not visible on some phones

The app runs fine. On devices with hard options menu button the menu shows up, so i know it works. On some devices it shows the overflow button at the top-right.

I my test case the device is Asus Zenphone 5 there is no hard button, i dont get a overflow button either. But when i run the showOptionsMenu() command from a button click it displays the options menu and all related events work no issues.

Menu - Xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@+id/select" android:title="Select" android:showAsAction="never"></item>
    <item android:id="@+id/add_kid" android:title="Add" android:showAsAction="never"></item>
</menu>

onCreate & onPrepare

@Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {

        getMenuInflater().inflate(R.menu.menu1, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) 
    {
        if(!getCheckInStatus())
        {
            menu.setGroupVisible(R.id.group1,false);
        }
        else
        {
            menu.setGroupVisible(R.id.group1,true);
        }
        return super.onPrepareOptionsMenu(menu);
    }

Manifest values for the Activity

<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
  .....

<activity
            android:name="com.my.package.MyActivity"
            android:configChanges="orientation|keyboard"
            android:label="@string/app_name"
            android:launchMode="singleInstance"

        >

        </activity>

I would really appreciate any help on this matter.

I used to have same problem in few of my projects. I followed the steps given below which worked for me 1. extends your activity to ActionBarActivity

  1. call the following code in onCreate method

     private void makeActionOverflowMenuShown() { //devices with hardware menu button (eg Samsung Note) don't show action overflow menu 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) { } } 

在menu.xml中设置android:showAsAction="ifRoom" ,该按钮将显示。

Try like this. This works for me..

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:icon="@drawable/ic_action_overflow"
        android:id="@+id/moreItmes"
        android:showAsAction="never"
        android:title="@string/more">        
        <menu>           
            <item
                android:id="@+id/select"
                android:showAsAction="never"
                android:title="@string/Select"/>

            <item
               android:id="@+id/add_kid"
                android:showAsAction="never"
                android:title="@string/Add"/>
        </menu>
    </item>

</menu>

As i commented in vinay's answer , the reason was that i was debugging an old program written by some one else. It seems all were Activities , but i had changed the target and compile-with to 22. Once i changed the Activity to ActionBarActivity the overflow appeared on the devices with soft menu button. While those with hard options menu button did not display it.

Menu - Xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@+id/select" android:title="Select" android:showAsAction="never"></item>
    <item android:id="@+id/add_kid" android:title="Add" android:showAsAction="never"></item>
</menu>

in your menu.xml you just use showAsAction as never which represent Never place this item in the Action Bar. please refer the link for more info

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM