In the ActionBar Menu, how do you allow the icon of an item to always be visible, and text only when there is space?

0

I'm trying and researching for some time the possibility of having behavior in my ActionBar , as follows for some items:

  • Icon ( android:icon ): always visible in ActionBar ;
  • Text ( android:title ): only visible if there is space;

What I've already tried:

Try 1:

<item
    android:id="@+id/action_add"
    android:icon="@android:drawable/ic_menu_add"
    android:orderInCategory="100"
    android:title="@string/btn_add"
    android:showAsAction="ifRoom|withText"/>
// Resultado: Mostra o item somente se houver espaço, mas o ícone juntamente com o texto

Attempt 2:

android:showAsAction="ifRoom"
// Resultado: Mostra o item somente se houver espaço, mas somente ícone sem o texto

Try 3:

android:showAsAction="always"
// Resultado: Mostra o item sempre, mas somente o ícone

Attempt 4:

android:showAsAction="always|withText"
// Resultado: Mostra o item sempre, mas o ícone juntamente com o texto

What would you like to do:

For demonstration purposes what I would have wanted would be something similar to this (there is obviously this):

// always|withIcon (sempre ícone) e ifRoom|withText (se houver espaço com texto)
android:showAsAction="always|withIcon|ifRoom|withText"

So is there any alternative to getting this behavior in a menu item of ActionBar ?

    
asked by anonymous 25.03.2015 / 18:53

1 answer

2

You can not assign more than one "showAsAction" type to a single menu item. When you try to do this, the following exception is thrown:

java.lang.IllegalArgumentException: SHOW_AS_ACTION_ALWAYS,SHOW_AS_ACTION_IF_ROOM, and SHOW_AS_ACTION_NEVER are mutually exclusive.

You might achieve a similar effect by using an item for text and another item for the icon and go alternating visibility directly in the code, for example: When the device is horizontal (view more wide of the action bar), place the icon to be invisible and the text to be visible and when the device is upright (less broad view of the action bar), do the opposite.

Anyway, I do not think this would be easy to do and it would not give the desired effect either, but unfortunately I do not know of another alternative.

OBS: Always try to use the UI defaults indicated / provided by the Android APIs as this standardizes the applications and the user experience, which is faster since it does not have to "discover" the functions that the application provides by clicking on various options until you get it right. And remember, always use icons that are self-descriptive: lupa = perform search, floppy = save, sign of + = add / create, etc, as this eliminates the need to put text.

>     
26.03.2015 / 02:06