I am following the tutorial on developer.android.com and trying to add items on action bar.
Although i added all the code the search action shows as an overflow element instead of an action button element. I tried on 4" and 7" virtual devices with soft keyboard option.
Here is the
main_activity_actions.xml
<?xml version="1.0" encoding="utf-8"?>
<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>
Here is the MainActivity.java
's onCreateOptionsMenu
method.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
I want to learn what causes this problem.
All action buttons and other items available in the action overflow are defined in an XML menu resource. To add actions to the action bar, create a new XML file in your project's res/menu/ directory. The app:showAsAction attribute specifies whether the action should be shown as a button on the app bar.
To insert an action button on one slide: Click the Insert tab. Click the Shapes command in the Illustrations group. A drop-down menu will appear with the action buttons located at the very bottom.
Android ActionBar is a menu bar that runs across the top of the activity screen in android. Android ActionBar can contain menu items which become visible when the user clicks the “menu” button.
This is because if you use the support AppCompat ActionBar library and ActionBarActivity you should create your menus in a different than the standard way of creating xml menus in ActioBarSherlock or the default ActionBar.
So try this code :
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
app:showAsAction="always" />
<item android:id="@+id/action_compose"
android:icon="@drawable/ic_action_compose"
android:title="@string/action_compose"
app:showAsAction="always"/>
</menu>
Though the issue is resolved, let me post an answer with more information, may be found helpful by anyone later.
Now, the issue is you have used android:showAsAction="ifRoom"
and android:showAsAction="never"
, instead if you would want to make action button always visible then use android:showAsAction="always"
FYI, android:showAsAction can take either of any values:
android:showAsAction=["ifRoom" | "never" | "withText" | "always" | "collapseActionView"]
You can read more about Menu Resource
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