30

I am writing an Android App in which i am trying to show Overflow Menu Items to ActionBar

using this great tutorial link: http://wptrafficanalyzer.in/blog/adding-action-items-and-overflow-menu-items-to-action-bar-in-android/

Problem:

Not getting Overflow Menu Items (Icon)

Please see below Screen Shot for more clarity: enter image description here

Manifest.xml:

<uses-sdk android:minSdkVersion="14" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" 
        android:uiOptions="splitActionBarWhenNarrow"              
        >

items.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
    android:id="@+id/phone"
    android:title="@string/phone"
    android:icon="@drawable/phone"
    android:showAsAction="ifRoom|withText"
/>

<item
    android:id="@+id/computer"
    android:title="@string/computer"
    android:icon="@drawable/computer"
    android:showAsAction="ifRoom|withText"
/>

<item
    android:id="@+id/gamepad"
    android:title="@string/gamepad"
    android:icon="@drawable/gamepad"
    android:showAsAction="ifRoom|withText"
/>

<item
    android:id="@+id/camera"
    android:title="@string/camera"
    android:icon="@drawable/camera"
    android:showAsAction="ifRoom|withText"
/>

<item
    android:id="@+id/video"
    android:title="@string/video"
    android:icon="@drawable/video"
    android:showAsAction="ifRoom|withText"
/>

<item
    android:id="@+id/email"
    android:title="@string/email"
    android:icon="@drawable/email"
    android:showAsAction="ifRoom|withText"
/>
 </menu>

I am using this tutorial, and trying to make Figure 6 : Action items and Overflow menu in Split Action Bar

Please help me to show Overflow Menu Items (ICON) to ActionBar

Now whenever i do click on Menu Button in emulator, then i am getting rest Menu Items....

Babu
  • 957
  • 1
  • 9
  • 21
  • 1
    change your ifRoom parameter to always to show all the items – the-ginger-geek Mar 19 '13 at 06:26
  • 1
    You can clearly see on the screenshot(s of the linked article) that the device has Back, Home and Tasks buttons so it's not possible summon the overflow menu: http://stackoverflow.com/a/23721321/253468 – TWiStErRob Sep 12 '14 at 13:06

7 Answers7

58

Just to say that If your device has a menu-button, the overflow-icon won't show, on the newer phones the overflow button will show. I would not reccomend ASMUIRTI's answer since it is an awful hack that breaks consistency with the rest of the apps on the platform.

you must use

android:showAsAction="never"

and let the android device decide if the overflow menu is needed for that device.

Akshat Agarwal
  • 2,837
  • 5
  • 29
  • 49
  • 3
    on some devices (I've tested on Galaxy Tab 3, OS 4.2.2) it cause hiding item to be available only on "Menu" button click – Sergii Apr 15 '14 at 13:48
47

To show three dot icon, to Action Bar, just use below method in your OnCreate():

   private void getOverflowMenu() {

    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();
   }
 }
ASMUIRTI
  • 1,332
  • 10
  • 20
  • 21
    This is a hack...I definitely would not use that. Accepted answer should be Akshat Agarwal's – Habizzle Dec 02 '13 at 09:21
  • This is this the proper way of showing overflow icon? Should this be accepted as a answer?!!! – Srinivasan N Mar 23 '15 at 09:53
  • @max.mustermann may be it is a hack or whatever it may be.But it is working good.I seen this answer [here](http://stackoverflow.com/questions/9286822/how-to-force-use-of-overflow-menu-on-devices-with-menu-button) – Stephen Jun 27 '15 at 05:23
  • @Naruto , as the answer from Akshat Agarwal does work also, uses less code (zero, its just configuration) and is not/less bug prone if a new android version is released, I really do not see a valid reason, why this is the accepted answer. – Habizzle Jun 27 '15 at 10:04
  • @max.mustermann This answer is an accepted answer because for a hardware button smartphone the overflow items will be displayed only by clicking the menu hardware button.But it wouldn't display the overflow menu items in top right side in the action bar.For more Reference check [user timo ohr answer here](http://stackoverflow.com/questions/9286822/how-to-force-use-of-overflow-menu-on-devices-with-menu-button) – Stephen Jun 27 '15 at 10:11
  • 1
    @Naruto this was not pointed out, neither in question nor answer or am I missing something ;). In this case this might be the best working solution. But as a general recommendation, I would prefer XML-configuration over accessing private fields via reflection. – Habizzle Jun 27 '15 at 10:18
  • Great answer, I was looking just for this because the hardware button hides the overflow icon. – Corven Dallas Oct 15 '15 at 12:20
12

This could be another work around, which really helped me. Keep one drawable with three dots and give it as a menu item.

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


<item
    android:id="@+id/saveDetails"
    android:showAsAction="always"
    android:title="@string/save"/>

<item
    android:id="@+id/menu_overflow"
    android:icon="@drawable/dts"
    android:orderInCategory="11111"
    android:showAsAction="always">
    <menu>
        <item
            android:id="@+id/contacts"
            android:showAsAction="never"
            android:title="Contacts"/>


        <item
            android:id="@+id/service_Tasks"
            android:showAsAction="never"
            android:title="Service Tasks"/>


        <item
            android:id="@+id/charge_summary"
            android:showAsAction="never"
            android:title="Charge Summary"/>


        <item
            android:id="@+id/capture_signature"
            android:showAsAction="never"
            android:title="Capture Signature"/>
    </menu>
</item>

 </menu>
Rino
  • 1,215
  • 1
  • 16
  • 28
  • For some reason this is the ONLY WAY I CAN GET IT TO WORK. It's so weird! ( http://stackoverflow.com/questions/23720815/android-studio-just-cant-get-action-bar-overflow-to-work )Thanks for the great tip. THANKS! – Fattie May 18 '14 at 10:13
1

If I understand your question correctly and you want to show all your icons on the action bar change this parameters in your menu items

android:showAsAction="ifRoom|withText"

to this

android:showAsAction="always"
the-ginger-geek
  • 7,041
  • 4
  • 27
  • 45
1

If you are using from supporting libraries, in order to show menu items in action bar you must use from the following syntax in your xml:

yourappname:showAsAction="ifRoom|withText"
Mohammad
  • 118
  • 3
0

The phones which have menu hardware button show extra menu items on click of the hardware button. Newer phones, with no hardware menu button automatically add an Overflow menu icon to the Action Bar. The extra menu items are those which have "showAsAction" property set to never.

v01d
  • 569
  • 6
  • 10
0

Within AndroidManifest.xml between add

android:theme="@android:style/Theme.Holo.Light"

which adds the action bar in your application.

After that, go to menu.xml and add the followings

xmlns:tools="http://schemas.android.com/tools

between Finally in each item add

android:showAsAction="always"
tools:ignore="AppCompatResource"
KostasA
  • 5,204
  • 6
  • 23
  • 29