Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

android

xml

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!

like image 577
Axel Avatar asked Apr 22 '14 21:04

Axel


People also ask

How do I Hide and show a menu item in Android Actionbar?

This example demonstrates how do I hide and show a menu item in the Android ActionBar. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml. Step 3 − Add the following code to src/MainActivity.java

Where are the actions/operations of the items in an Actionbar declared?

Those operations/actions of the items are declared in that Activity file for which the ActionBar has been designed. In this example, the target activity is the MainActivity file. Further, the custom title, subtitle, and application logo are also defined in this file.

What is the difference between never and with text in Actionbar?

c. never: With this flag, the item will be not be displayed as an icon in ActionBar, but will be present in the overflow menu. d. withText: To represent an item as both icon and the title, one can append this flag with the always or ifRoom flag (always|withText or ifRoom|withText).

What is the difference between always and ifroom in Actionbar?

a. always: To display the item in the ActionBar all the time. b. ifRoom: To keep the item if space is available. c. never: With this flag, the item will be not be displayed as an icon in ActionBar, but will be present in the overflow menu.


1 Answers

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).

like image 161
Jack Avatar answered Sep 23 '22 04:09

Jack