Is it possible to set the flags as suggested here android:showAsAction="ifRoom|withText"
programmatically?
Asked
Active
Viewed 1.3k times
22

Community
- 1
- 1

Gaurav Agarwal
- 18,754
- 29
- 105
- 166
3 Answers
49
For each MenuItem
, do the following:
myMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);

Geobits
- 22,218
- 6
- 59
- 103
-
I try `myMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);` on kotlin but it didnt work, it only allow to input with one value type, is there any other way? – Jan sebastian Jul 16 '21 at 11:08
14
If you want to set these properties at run time then you need to do so on the MenuItem
, not the ActionBar
.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
MenuItem item = menu.findItem(R.id.your_menu_item);
item.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
}

Michael Celey
- 12,645
- 6
- 57
- 62
-1
Have you tried this:
getSupportActionBar().setDisplayOptions(MenuItem.SHOW_AS_ACTION_IF_ROOM);
getSupportActionBar().setDisplayOptions(MenuItem.SHOW_AS_ACTION_WITH_TEXT);

KunalK
- 1,904
- 4
- 22
- 40
-
4I believe that should be `(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT)`, flag-style. – Geobits Feb 28 '13 at 14:50
-
4Based on the [documentation](http://developer.android.com/reference/android/app/ActionBar.html#setDisplayOptions(int)), I don't think this should work, since `setDisplayOptions()` needs the ActionBar `DISPLAY_` constants Although, some constants [*do* overlap](http://developer.android.com/reference/android/view/MenuItem.html#SHOW_AS_ACTION_ALWAYS). – A--C Feb 28 '13 at 14:52