Reputation: 2913
I have an action bar with the following menu items;
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.blah.blah.app.ClientActivity" >
<item android:id="@+id/action_search"
android:icon="@drawable/search"
android:title="@string/action_search"
android:orderInCategory="1"
app:showAsAction="ifRoom|withText"/>
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:orderInCategory="5"
app:showAsAction="ifRoom"/>
<item android:id="@+id/action_waiter"
android:title="@string/action_waiter"
android:orderInCategory="6"
app:showAsAction="ifRoom"/>
<item android:id="@+id/action_cleantable"
android:title="@string/action_cleantable"
android:orderInCategory="7"
app:showAsAction="ifRoom"/>
<item android:id="@+id/action_suggest"
android:title="@string/action_suggest"
android:orderInCategory="8"
app:showAsAction="ifRoom"/>
<item android:id="@+id/action_waiterlogin"
android:title="@string/action_waiterlogin"
android:orderInCategory="9"
app:showAsAction="ifRoom"/>
</menu>
The problem is my search button doesn't show on action bar but the text is showed in overflow. There's plenty of room in my action bar
I'm using "@style/Theme.AppCompat.Light"
Can anyone help me ?
Upvotes: 10
Views: 14762
Reputation: 1
app:showAsAction
is an AppCompatActivity
attribute. You could use androidx.appcompat.app.AppCompatActivity
instead of android.view.MenuInflater
. Code sample could be as below
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.toolbar_menu, menu);
return super.onCreateOptionsMenu(menu);
}
Upvotes: 0
Reputation: 1131
You must have a namespace for the app declaration to work
<menu ......
xmlns:app="http://schemas.android.com/apk/res-auto
for the app namespace prefix to work in your action item.
Upvotes: 0
Reputation: 67
app:showAsAction="ifRoom"
Change to
android:showAsAction="ifRoom|withText"
Upvotes: -1
Reputation: 379
ToolBar (that is in ActivityBar) strives not to exceed some amount of visible elements. And that limit is lower than ToolBar can contain indeed. The limit is set in android.support.v7.view.ActionBarPolicy class:
`/**
* Returns the maximum number of action buttons that should be permitted within an action
* bar/action mode. This will be used to determine how many showAsAction="ifRoom" items can fit.
* "always" items can override this.
*/
public int getMaxActionButtons() {
final Resources res = mContext.getResources();
final int widthDp = ConfigurationHelper.getScreenWidthDp(res);
final int heightDp = ConfigurationHelper.getScreenHeightDp(res);
final int smallest = ConfigurationHelper.getSmallestScreenWidthDp(res);
if (smallest > 600 || widthDp > 600 || (widthDp > 960 && heightDp > 720)
|| (widthDp > 720 && heightDp > 960)) {
// For values-w600dp, values-sw600dp and values-xlarge.
return 5;
} else if (widthDp >= 500 || (widthDp > 640 && heightDp > 480)
|| (widthDp > 480 && heightDp > 640)) {
// For values-w500dp and values-large.
return 4;
} else if (widthDp >= 360) {
// For values-w360dp.
return 3;
} else {
return 2;
}
}`
As you can see the limit is between 2 and 5, and it depends on the screen width. So if you want to exceed the limit, you should use showAsAction="always" or create your own view for ActionBar .
Upvotes: 0
Reputation: 77
In case someone else gets into the same problem - I could not get the button to show up until I overrode the onCreateOptionsMenu function and Inflated the menu (I am using com.android.support:appcompat-v7:24.2.1 version)
Here is the code snippet:
@Override
public boolean onCreateOptionsMenu (Menu menu){
getMenuInflater().inflate(R.menu.toolbar_menu, menu);
return super.onCreateOptionsMenu(menu);
}
Upvotes: 1
Reputation: 171
In my case I had to add a few lines to onCreateOptionsMenu.
Android Studio didn't let me use android:showAsAction="ifRoom" while using appCompat.
app:showAsAction="ifRoom" wasn't working and I removed it without problems.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
for (int i = 0; i < menu.size(); i++) {
menu.getItem(i).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
}
return super.onCreateOptionsMenu(menu);
}
Upvotes: 7
Reputation: 4357
Please Try to use
android:showAsAction="ifRoom|withText"
instead of app:showAsAction="ifRoom|withText"
Upvotes: 10