Ferran Maylinch
Ferran Maylinch

Reputation: 11529

State selector for a custom button layout (i.e. LinearLayout)

I'm wondering if it's possible to build a custom button layout with different appearance depending on state so it acts like a button. For example, a LinearLayout with an ImageView and TextView, with these states.

Thank you!

Upvotes: 0

Views: 237

Answers (1)

Karakuri
Karakuri

Reputation: 38605

Any view can have any background and any view can be made clickable, so there is nothing preventing you from having a LinearLayout with the background you described. It's worth noting that you don't need a LinearLayout to have an image next to text, you can use the fact that TextView supports drawables on any side of the text using the drawable[Left|Top|Right|Bottom] attributes.

If your question is about the syntax of a selector drawable, I would refer you to the documentation. Note that Android evaluates states top to bottom, so choose the order wisely. The last item should be the "default" state when none of the ones above apply. For the example you gave, you would probably have something like

<selector>
    <item android:state_pressed="true" android:drawable="..." />
    <item android:state_enabled="true" android:drawable="..." />
    <item android:drawable="..." /> <!-- disabled -->
</selector>

Upvotes: 1

Related Questions