Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get action buttons with custom layouts to be styled like standard action buttons in Android 3.0+

I'm having a bit of trouble with custom action buttons in the honeycomb+ action bar. I'm adding a menu item that uses a custom layout (using the android:actionLayout attribute). The reason for the custom layout is that I want a button that has two lines of text that can be updated dynamically.

However, I still want this action button to operate like the other standard buttons. By this I mean that the background fades in when the button is selected, and fades out again if it is unselected, all in the style of the platform (the colour seems to differ between different platforms/devices - I've seen both grey and blue versions)

I've tried using the action button style for the custom layout:

style="@android:style/Widget.ActionButton"

and I've tried setting the background for the custom layout to:

android:background="?android:attr/actionBarItemBackground"

but to no avail, and I'm kind of trying things fairly randomly as I can't find any documentation on how to do this (or if indeed it is even possible).

I know I can approximate this behaviour myself by setting the background, but it would be nice if I could just set the item to behave like a normal action button in terms of how it appears when the user interacts with it.

Anyone have any ideas?

Thanks in advance!

like image 361
kingraam Avatar asked Dec 09 '22 05:12

kingraam


1 Answers

Ah, sorry to answer my own question but I have just stumbled upon a way to do this. I was halfway there - you need your custom layout's style to inherit from ActionButton:

@android:style/Widget.ActionButton

but then you also need to make the layout clickable:

android:clickable="true"

for it to work. Using both of these makes the custom action buttons look just like the regular ones when you press them.

Hopefully that'll help someone trying to do this!

like image 94
kingraam Avatar answered Dec 21 '22 11:12

kingraam