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

Asked

Viewed 794 times

0

I’ve been trying and researching for some time the possibility of having a behavior in my Menu of ActionBar, as follows for some items:

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

What I’ve already tried:

Attempt 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

Attempt 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 I’d like to do:

For the purpose of demonstrating what I would want is something similar to that (there is obviously no such thing):

// 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 get this behavior in a menu item from ActionBar?

  • 1

    Fernando, I think with the default API it is not possible to do. I think with the Toolbar and using Views normal and manipulating visibility and calculating space (will end up being pretty boring).

  • I get it, @Wakim. It just doesn’t seem to make much sense, because if I have an icon and a title for the button, and there’s space on the ActionBar, Why not display the icon and title? But that’s not crucial for my application. I only posted here to really get off, because I spent some time researching about it and nothing. So I can point out (to superiors) that this is not supported by the standard/native API, if that’s the case. (And I don’t intend to do "gambiarras" in my ActionBar pattern, hehe).

  • Yeah, like you said it has the icon+text setting, but it doesn’t have if espaco pra icone+texto then icone+texto else if espaco then icone else esconde icone, that’s the problem, if that’s what I get from use case.

  • @Wakim, actually the algorithm would be this: if espaco para icone+texto then icone+texto else if espaco para icone then icone else esconde icone+texto and add texto action overflow. Got it?

  • 1

    Hmm, I get it... This case doesn’t really have to, just by doing "hand" and with a good effort... I don’t know if writing the class of ActionBar/Toolbar could do this, in the method that builds the menu.

1 answer

2

It is not possible to assign more than one "showAsAction" type to a single menu item. When trying to do this, the following Exception is launched:

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

Maybe you can achieve a similar effect by using an item for text and another item for the icone and switching the visibility directly in the code, for example: When the device is horizontal (wider view of the action bar), put the icon to be invisible and the text to be visible and when the device is vertical (less wide view of the action bar), do the opposite.

Anyway, I don’t think this would be easy to do and it wouldn’t have the desired real effect either, but unfortunately I don’t know any other alternative.

NOTE: Always try to use the UI standards indicated/provided by the Android Apis, because this standardizes the applications and the user experience, which is faster, since it does not need to be "discovering" the functions that the application provides by clicking on various options until right. And remember, always use icons that are self-descriptive: magnifying glass = perform search, floppy disk = save, signal + = add/create, etc., because this eliminates the need to put a text.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.