Items not showing in the ActionBar with showAsAction=“always”
Asked Answered
C

2

13

I'm new to Android programming and currently trying to get the Actionbar working. My problem is, that although I set an item in the XML file to "always", it constantly ends up in the overflow menu no matter what I try. I found several similar problems on here, but none of the solutions fixed the problem.

Additional info: The ic_action_search icons are in the drawable folders of the project.

main_activity_actions.xml:

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

<!-- Search, always displayed -->
<item android:id="@+id/action_search"
      android:icon="@drawable/ic_action_search"
      android:title="@string/action_search"
      app:showAsAction="always" />


<!-- Settings, should always be in the overflow -->
<item android:id="@+id/action_settings"
      android:title="@string/action_settings"
      app:showAsAction="never" />
</menu>

strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>

  <string name="app_name">My First App</string>
  <string name="edit_message">Enter a message</string>
  <string name="button_send">Send</string>
  <string name="action_settings">Settings</string>
  <string name="action_search">Search</string>
  <string name="title_activity_main">MainActivity</string>
  <string name="title_activity_display_message">My Message</string>
  <string name="hello_world">Hello world!</string>
</resources>

In MainActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu items for use in the action bar
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);
    return super.onCreateOptionsMenu(menu);
}

Thanks for your help!

Chronogram answered 22/4, 2014 at 21:55 Comment(0)
C
34

try android:showAsAction instead of app:showAsAction. Or if you're using the appcompat_v7 backport, use both android:showAsAction and app:showAsAction(Thanks to Commonsware).

Catanddog answered 22/4, 2014 at 22:6 Comment(6)
Or, if you are using the appcompat_v7 action bar backport, you need both android:showAsAction and app:showAsAction.Behead
Changed it to android:showAsAction, the item is still in the overflow menu.Chronogram
I have been looking to solve this problem from last 1 day. Thanks for answer.Marcheshvan
If you have both of them present and you are using Android Studio, it displays an error message in the XML editor (although the app can be run and it works properly). To get rid of the error indicator, add the following to the layout XML: tools:ignore="AppCompatResource".Breadnut
here in my case I used both but still not showing it up?Scarab
I used it like <item android:id="@+id/action_mark_as_read" android:icon="@drawable/ic_done_all_black_24dp" android:orderInCategory="1" android:title="Mark as read" app:showAsAction="ifRoom|withText" android:showAsAction="ifRoom|withText" tools:ignore="AppCompatResource" />Scarab
P
0

It worked for me.

    <item
    android:orderInCategory="8"
    android:id="@+id/traffic_light"
    android:icon="@drawable/ic_green_circle"
    android:showAsAction="always"
    tools:ignore="AppCompatResource"
    android:title="Status" />
Potiche answered 20/8 at 22:7 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Bigmouth

© 2022 - 2024 — McMap. All rights reserved.