Android ActionBar compat overflow menu not showing on sdk 10
Asked Answered
P

3

6

Hello and thank you for the time you take in reading this question.

I am trying to develop an android app which will use the ActionBar compat library. I have followed (as far as I see it) all the recommendations when using the compat library. My Manifest looks like this(only relevant code shown):

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"        
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

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

    </application>

</manifest>

As you can see I am targeting sdk 8+. I have used the Theme.AppCompat theme as recommended.

My menu file looks like this:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:cds="http://schemas.android.com/apk/res-auto" >

    <item
        android:id="@+id/action_map"
        android:icon="@drawable/ic_action_map"
        android:title="@string/action_map"
        cds:showAsAction="ifRoom"/>
    <item
        android:id="@+id/action_search"
        android:icon="@drawable/ic_action_search"
        android:title="@string/action_search"
        cds:showAsAction="ifRoom"/>
    <item
        android:id="@+id/action_mail"
        android:icon="@drawable/ic_action_mail"
        android:title="@string/action_mail"
        cds:showAsAction="ifRoom"/>

</menu>

I am using my own namespace for the showAsAction attribute.

My activity extends the ActionBarActivity class.

The problem is this: On sdk 10 (android 2.3.3), both on device and emulator, the overflow menu (the three dots on the right side of the action bar) are not shown. Only the first 2 of the menu items are shown on the action bar. If i press the "Menu" button on the device then the third item is shown from the bottom left corner of the screen (not from the upper right corner as on the devices with more recent android versions). The same code works well on android sdk 17 on emulator (the overflow menu is shown with the proper actions).

I have searched the web for a solution but I could not find one with this specific problem. I would have abandoned the issue if I wouldn't have installed apps on the android 2.3.3 device that have the same action bar and which show the overflow menu icon and work properly like on any recent android device. One example of this app is the todoist app (https://en.todoist.com/android) or the handcent app(https://play.google.com/store/apps/details?id=com.handcent.nextsms&hl=en) which both behave well on this device.

Is there anything I am missing or is there an alternative solution to the recommended way of using the actionbar compat?

Thank you for your time.

Pavlish answered 1/12, 2013 at 10:55 Comment(0)
T
7

@Andrei Google have disabled the menu overflow button in appcompat on pre honycomb. If You really want to add it go to the android's github repository and download platform_frameworks_support. It contains sorce for appcompat in platform_framework_support_master/v7/appcompat.

Create a libs folder inside appcompat and put latest android-support-v4.jar. Now open file v7/appcompat/src/android/support/v7/internal/view/ActionBarPolicy.java. You will see that showOverflowMenuButton is returned false for pre honycomb.Just return it true and add this edited appcompat as library to your project and you will not need any custom overflow button This worked with me. Sorry for my English

EDIT: actual code from android/support/v7/internal/view/ActionBarPolicy.java

public boolean showsOverflowMenuButton() {
    // Only show overflow on HC+ devices
    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB;
}
Throughway answered 21/1, 2014 at 22:2 Comment(2)
Thank you for your answer. As i wrote below, I went for a "split action bar" for this project but I will give your solution a try on my future projects. I will let you know when I have done enough tests.Pavlish
Why does Google do something like this? Is there really no other way to do that, what I don't like about your suggestion is that new updates to the support library aren't automatically installed...Reckoning
K
5

Try to show as I did.

I add overflow menu (three dots) manually:

<item
    android:id="@+id/menu_more"
    android:icon="@drawable/abc_ic_menu_moreoverflow_normal_holo_light"
    android:title="@string/action_settings"
    myapp:showAsAction="always">
    <menu>
        <item
            android:id="@+id/submenu_about"
            android:showAsAction="always"
            android:title="@string/menu_about"/>
    </menu>
</item>

and override menu button click in activity to show this menu (solution from Opening submenu in action bar on Hardware menu button click):

private Menu mainMenu;    

...

@Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_UP) {
            switch (keyCode) {
            case KeyEvent.KEYCODE_MENU:

                mainMenu.performIdentifierAction(R.id.menu_more, 0);
                return true;
            }

        }
        return super.onKeyUp(keyCode, event);
    }

Result on 2.2 looks like:

Android 2.2 Overflow menu

Hope it helps you.

Kobarid answered 12/12, 2013 at 12:47 Comment(2)
First of all sorry for the delay in answering this. I have had a lot of work during the past few months and did not have the time to proper answer you. I am sorry but I have tried your solution and it did not work. And by "did not work" I mean the outcome is not the same on all devices. On pre-Honeycomb the "Settings" dropdown appears somewhere in the middle of the screen instead of dropping from the upper right corner. I went for a "split action bar" for this project as my main concern was that users on pre-Honeycomb devices will not know there are other options to the menu.Pavlish
Thanks for reply. You are right, menu appears in the middle of the screen. I didn't find correct solution yet.Kobarid
U
-1

I found the answer finally for this situation.

All you need to do is call the following in the OnCreate in your activity

ActionBarPolicy.get(this).showsOverflowMenuButton();
Utham answered 24/6, 2014 at 17:4 Comment(2)
This works for only API 11+: android.googlesource.com/platform/frameworks/support/+/81df381/…Constrictive
The method source is one liner return Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB; also the class is from internal not public package package android.support.v7.internal.view;Climatology

© 2022 - 2024 — McMap. All rights reserved.