Show Menu item always in support action bar
Asked Answered
K

5

22

enter image description hereI am working on android application . I have implemented supported action bar in it .I want to show option menu item always . But it is not showing . it is showing in drop down menu . my code for menu item given below.

<item
    android:id="@+id/action_settings"
    android:icon="@drawable/add_post"
    android:title="@string/action_settings"

    />

And code of ActionBar Activity is given below:-

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return super.onCreateOptionsMenu(menu);
}

any help will be appreciated.

Kiushu answered 7/3, 2014 at 6:45 Comment(1)
May be you are missing a namespace.Please Check this linkHypogastrium
M
28

From docs you should add showAsAction attribute to always or ifRoom

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <!-- Search, should appear as action button -->
    <item android:id="@+id/action_search"
          android:icon="@drawable/ic_action_search"
          android:title="@string/action_search"
          android:showAsAction="ifRoom" />
    <!-- Settings, should always be in the overflow -->
    <item android:id="@+id/action_settings"
          android:title="@string/action_settings"
          android:showAsAction="never" />
</menu>
Melda answered 7/3, 2014 at 6:48 Comment(4)
There is no effect of this line.Kiushu
updated code ? and can you show the overflow icon also as screenshotMelda
I have added sample image . I want to show permanent button. not drop down.Kiushu
Thank you kabira . i got my answer.Kiushu
I
46

If you use the AppCompat ActionBar, you have to change your layout in this way:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto" >
    <item
        android:id="@+id/action_settings"
        android:icon="@drawable/add_post"
        android:title="@string/action_settings"
        app:showAsAction="always" />
</menu>  

showAsAction attribute will work only with a custom prefixe, see the documentation. According to it:

Notice that the showAsAction attribute above uses a custom namespace defined in the tag. This is necessary when using any XML attributes defined by the support library, because these attributes do not exist in the Android framework on older devices.

Hope this helps.

Include answered 7/3, 2014 at 6:55 Comment(1)
I am using AppCompat library. When i use app:showAsAction="always" , it does not give me any result. All the menu comes under the drop down. But if i put android:showAsAction="always" even if i use AppCompat, then i get the result as expected but with a red line warning under the statement. What should be done in such case?Stace
M
28

From docs you should add showAsAction attribute to always or ifRoom

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <!-- Search, should appear as action button -->
    <item android:id="@+id/action_search"
          android:icon="@drawable/ic_action_search"
          android:title="@string/action_search"
          android:showAsAction="ifRoom" />
    <!-- Settings, should always be in the overflow -->
    <item android:id="@+id/action_settings"
          android:title="@string/action_settings"
          android:showAsAction="never" />
</menu>
Melda answered 7/3, 2014 at 6:48 Comment(4)
There is no effect of this line.Kiushu
updated code ? and can you show the overflow icon also as screenshotMelda
I have added sample image . I want to show permanent button. not drop down.Kiushu
Thank you kabira . i got my answer.Kiushu
S
8

You can use the different values of android:showAsAction property in android menu xml file..

to show all time visible menu on actionbar, you should go with android:showAsAction="always"..

You can also use android:showAsAction="ifRoom" but it will show the menu in action bar not always but it will show according to different screen size.

Edited

Ref: Menu items doesn't show up on the actionbar

Try with the below code then..

<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" <!-- app:showAsAction="ifRoom" -->
        android:title="@string/menu_lang"
        android:icon="@android:drawable/ic_input_lang"/>
</menu>

Hope it will help you..!!!

Slowworm answered 7/3, 2014 at 6:50 Comment(1)
If you are in Eclipse sometimes the XML will show an error, you have to clean the project and the error will go away as long as you don't modify the XML again. If you do, clean project again and error will go away.Cum
T
5

remove this line xmlns:app="http://schemas.android.com/apk/res-auto"

use android:showAsAction="always" instead of app:showAsAction="always"

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

<item
    android:id="@+id/action_refresh"
    android:icon="@drawable/ic_refresh"
    android:showAsAction="ifRoom"
    android:title="@string/action_refresh"/>

<item
    android:id="@+id/action_filter"
    android:icon="@drawable/ic_filter"
    android:showAsAction="always"
    android:title="@string/action_filter"/>
</menu>
Tva answered 11/11, 2014 at 19:28 Comment(0)
U
3

I realize this question is 2+ years old, but for completeness, if you want to set this in code use the following:

menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);

other options are

MenuItem.SHOW_AS_ACTION_NEVER
MenuItem.SHOW_AS_ACTION_IF_ROOM
MenuItem.SHOW_AS_ACTION_ALWAYS
MenuItem.SHOW_AS_ACTION_WITH_TEXT
MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW
Uranus answered 19/7, 2017 at 21:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.