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!
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
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.
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).
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With