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 theActionBar
; - 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
?
Fernando, I think with the default API it is not possible to do. I think with the
Toolbar
and usingViews
normal and manipulating visibility and calculating space (will end up being pretty boring).– Wakim
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 myActionBar
pattern, hehe).– Fernando Leal
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
@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?– Fernando Leal
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.– Wakim