Set android:showAsAction="ifRoom|withText" programmatically
Asked Answered
S

3

23

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

Somniloquy answered 28/2, 2013 at 14:42 Comment(0)
I
50

For each MenuItem, do the following:

myMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
Integrant answered 28/2, 2013 at 14:57 Comment(1)
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?Rawdon
S
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);
}
Switch answered 28/2, 2013 at 14:58 Comment(0)
K
-1

Have you tried this:

getSupportActionBar().setDisplayOptions(MenuItem.SHOW_AS_ACTION_IF_ROOM);
getSupportActionBar().setDisplayOptions(MenuItem.SHOW_AS_ACTION_WITH_TEXT);
Keloid answered 28/2, 2013 at 14:47 Comment(2)
I believe that should be (MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT), flag-style.Integrant
Based on the documentation, I don't think this should work, since setDisplayOptions() needs the ActionBar DISPLAY_ constants Although, some constants do overlap.Coriecorilla

© 2022 - 2024 — McMap. All rights reserved.