Menu items doesn't show up on the actionbar [duplicate]
Asked Answered
G

3

33

I am using appcompat in my app. I want the menu items to show on actionbar or at least the overflow(3 dots) to show them when there is no room. There is lot of space on the actionbar, but still they don't show up. The menu flow raises from the bottom and that too only when menu button is pressed.

menu_activity.xml:

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

    <item
        android:id="@+id/menu_lang"
        android:showAsAction="always"
        android:title="@string/menu_lang"
        android:icon="@android:drawable/ic_input_lang"/>

</menu>

activity:

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

This post says that it not works when hardware menu button is present. But other apps are able to show items on the same device. So, that answer seems to be incorrect. Can someone please help on this?

enter image description here

Gamboge answered 22/8, 2013 at 12:12 Comment(11)
So basically you want to open the actionbar menu when hardware menu button is tapped (if present) \?Zygophyllaceous
show us your entire menu xmlHereditable
@prateek No. I want the items to display on actionbar as shown in the above pic. As you can see there are two options at right side search and add. So, I want them visible on actionbar. But they are showing up from down to the screen only when menu button is pressed.Gamboge
@Hereditable This is my entire menu.xml. I have only option. So I want it display on actionbar.Gamboge
@Hereditable I tried. No use :/Gamboge
Are you facing this problem in all the versions of Android or is it just in 4.x versions?Murder
@mANDROID Even on 2.2 too. I don't understand why it doesn't show items on actionbar though it has lots of space and even overflow dots is not visible. It just shows from the bottom with just title when menu button is pressedGamboge
Oh okay, because I faced several problems while testing on Android 4.x that I mentioned in this question and an answer. Still I will see if I can find a solution for you.Murder
Check my answer I just posted and let me know if it works for you.Murder
@Robert P. and all, if you check the question asked date, that is later than the time I asked this question. So, I just wanna know how can my question be duplicated?Gamboge
@Gamboge how u reoslved i also facing sameSacrificial
B
70

You seem to be using the wrong menu: Your file is named "menu_activity.xml" and you inflate the menu with the Resource-Id: R.menu.reminder_menu

The Resource name of the menu should be the same as the file name, i.e.: R.menu.manu_activity

Try it with this again - I ran into this too once and it drove me nuts...

Update After clarification that the above part was for obfuscation, please make sure that:

  1. You extend ActionBarActivity.
  2. You use (or extend) one of the Theme.AppCompat themes for the activity (or whole app)
  3. Because on older devices, the Actionbar related attributes are not present, make sure that all these attributes in the XML are used with a custom namespace. Here that would be the showAsAction attribute, so that the compatibility library can pick them up.

You already had the custom namespace defined ("app", in the menu tag). You need to change the android:showAsAction tag to app:showAsAction according to the Android guide.

Your corrected menu_activity.xml would then look like this:

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

    <item
        android:id="@+id/menu_lang"
        app:showAsAction="always"
        android:title="@string/menu_lang"
        android:icon="@android:drawable/ic_input_lang"/>

</menu>

The extended Android guide for actionbar covers these new and nasty traps...

Burgoyne answered 22/8, 2013 at 12:17 Comment(10)
I forgot to change the name while posting a question to maintain anonimity. But this is not really an answer to my question. Sorry for that.Gamboge
No problem. If you use AppCompat by Google, also make sure that you extend ActionBarActivity :)Burgoyne
Thanks for your answer. I did it. I am able to find options only when I click on menu button. But not on actionbar. What I want is to show items on actionbar as shown in the above pic.Gamboge
Updated with further info: Especially check that you use the app prefix that you defined for the showAsAction attribute!Burgoyne
I don't understand the 3rd point clearly. Can you please elaborate?Gamboge
Sure, I added a corrected version of the XML and extended explanationBurgoyne
Thank you so much. I solved it from this post few hours ago.#18387142 Your answer also explains the same. I missed the yourapp namespace for showAsAction. Instead I kept android:Gamboge
Happy to help - even a few hours late ;)Burgoyne
let us continue this discussion in chatGamboge
@Rat-a-tat-a-tatRatatouille can u tell me how u resolved its not solving my issueSacrificial
M
5

Okay I think I found a solution for you. It is called Overflow Menu and you need to call the below method in your onCreate method.

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();
}
}

Try this and let me know if it works for you.

EDIT:

I think I understood your problem finally. I can give you one idea. If you do not want to use overflow menu and just display menu items as displayed in the screen-shot you have posted, you can create a layout on top of your activity.

You can take a linear layout, give a background that looks the same as action bar and place whatever icons you want to put there, and give functionality to them with onClickListener. Hope this will give you some idea. This way you can create your own custom menu. Try the layout below and replace ic_launcher drawable with menu icons. Then just set onClickListeners to them and perform whatever functions you want to perform inside onClick method.

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="#66000000" >

    <ImageView
        android:id="@+id/xMenuBtn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:background="@drawable/ic_launcher" />
     <TextView
        android:id="@+id/xMenuTxt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Chats"
        android:textSize="18sp"
        android:layout_toRightOf="@+id/xMenuBtn1"
        android:layout_centerVertical="true" />
    <ImageView
        android:id="@+id/xMenuBtn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/xMenuBtn3"
        android:background="@drawable/ic_launcher" />
    <ImageView
        android:id="@+id/xMenuBtn3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:background="@drawable/ic_launcher" />

</RelativeLayout>
Murder answered 22/8, 2013 at 18:15 Comment(3)
+1 Thank you so much for the answer. It shows at least overflow menu and better than before. But why doesn't the menu item show up on actionbar? Because there is lot of space. Is there anything hack like this that I need to do to show it on the actionbar?Gamboge
Okay, do you need those menu buttons along with the overflow menu? or you just want to display all the menu items as they are in the screen-shot u have displayed?Murder
Please check EDIT part in my answer, hope you will get some idea on how to do it.Murder
T
0

Try in your activity onCreate() :

ActionBar bar = getSupportActionBar();
bar.setTitle(R.string.app_name);

and then :

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {    
        menu.clear();
        getSupportMenuInflater().inflate(R.menu.menu_activity, menu);
        return super.onPrepareOptionsMenu(menu);
    }
Tullus answered 22/8, 2013 at 12:23 Comment(7)
Can you please read my question carefully? Because I am using appcompatGamboge
support v7 include v4 am i not right ? This should works. By the way why not use ABS ?Tullus
Because I thought to follow google provided one instead of third party library.Gamboge
Action Bar Sherlock is based on The support lib v4, and the ActionBar. It's surly the most stable/portable lib. You certainly won't do better (or it ll take you lot of time, kind of a waste) and with this jgilfelt.github.io/android-actionbarstylegenerator/… you get an immediate good looking action bar;)Tullus
@Yume117 ActionBarSherlock is now no longer supported; Jake Warthon (the Author) clearly stated that he was going to make sure it worked but no new features are going to be added. He suggests that everybody should use (whenever possible) ActionBarCompat (the same idea released by Google), for obvious reasons. At the time of the writing of this question, ActionBarCompat had already been released, hence why the OP clearly says: "instead of using a third party library".Hospers
Nice intell, can you provide a link that redirect us where you found the info ?Tullus
@Yume117 here is the link to his blog postOstentation

© 2022 - 2024 — McMap. All rights reserved.