Set android:showAsAction="ifRoom|withText" programmatically-ThrowExceptions

Exception or error:

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

How to solve:

For each MenuItem, do the following:

myMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);


If you want to set these properties at run time then you need to do so on the MenuItem, not the ActionBar.

public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(, menu);
    MenuItem item = menu.findItem(;
    item.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);


Have you tried this:


Leave a Reply

Your email address will not be published. Required fields are marked *