In the app I've been working on, I would like to have a multiple-state (in my case, three) toggle button, instead of the two that ToggleButton
provides. I've tried to start my own that extends Button
, following the CompoundButton
source, but quite honestly reading over its source got a bit overwhelming.
Is there a way to do a three-state toggle button using just a selector xml or something, or perhaps another method I haven't thought of? I'm rather at a loss of how to do this.
Why not use
RadioGroup
and style radios inside?You can certainly define a selector to use as a background that has three entries. The question is what button attributes you can use for the selector. You can have two boolean attributes, say A and B, and define the selector in terms of A, B, and default. (A && B will satisfy A, so more properly they could be thought of as A, !A && B, and !A && !B.) You can overload existing attributes (selected, focused, etc.) or, more elegantly, define your own custom attributes using the recipe described in this thread.
I implemented a multi-state toggle button, the source code is here
This is how it looks:
And it's quite easy to use it:
In your activity:
You can create a custom ImageButton to achieve this, you need 3 different images in this case. You can also add more states if you want.