Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set android:showAsAction="ifRoom|withText" programmatically

Is it possible to set the flags as suggested here android:showAsAction="ifRoom|withText" programmatically?

like image 356
Gaurav Agarwal Avatar asked Feb 28 '13 14:02

Gaurav Agarwal


2 Answers

For each MenuItem, do the following:

myMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT); 
like image 183
Geobits Avatar answered Sep 30 '22 00:09

Geobits


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);
}
like image 33
Michael Celey Avatar answered Sep 30 '22 02:09

Michael Celey