Android Question How to add Checked State to a Panel?

Widget

Well-Known Member
Licensed User
Longtime User
I have a custom view called MyCheckButton that uses a panel as the background for a fancy button. I had to use a panel because it contains other views and it works really well. (I can't change back to using a Button, ToggleButton or anything like that. It has to be a panel.)

I want to add StateListDrawable to the MyCheckButton panel so it has different colors when Pressed, Disabled etc. That works fine.

Here's the problem. The button also has a Checked property so I would also like to add a color for State_Checked. But how do I notify Android that the panel is now Checked so it will use the State_Checked color? I have a property MyCheckButton.Checked, but how do I notify the Android OS that the panel is now in the Checked state so it will do the coloring for me? The way I've been doing it up until now is to manually redraw the button color in the setChecked method. I thought if I could use StateListDrawable it would simplify things.

Any ideas on how to set the panel to a Checked state that is recognized by Android?

TIA
 

dibb3386

Member
Licensed User
Longtime User
Might be confused about what your asking but why not make a transparent button with the parent of your panel and send it to the back with the designer (right click on button view) and just fill the panel with the button, that way all your other views will be on top of the transparent button? Then code your button states to the desired changes.

Or when your panel has been pressed what change is made? you say it has 3 changes pressed,not pressed,disabled so use 'if' statement to check the state and make the changes you want, if background image = 1.jpg Then... etc
 
Last edited:
Upvote 0

Widget

Well-Known Member
Licensed User
Longtime User
Might be confused about what your asking but why not make a transparent button with the parent of your panel and send it to the back with the designer (right click on button view) and just fill the panel with the button, that way all your other views will be on top of the transparent button? Then code your button states to the desired changes.

Or when your panel has been pressed what change is made? you say it has 3 changes pressed,not pressed,disabled so use 'if' statement to check the state and make the changes you want, if background image = 1.jpg Then... etc

Thanks for the reply. The reason I wanted the Panel to have States was so I could assign a StatelistDrawable to the panel's background so when it is pressed, it will flash the Pressed color. Your approach will work if the Button view's background is transparent except for the Pressed state, then it will change color only when pressed. That is an interesting solution. I never would have thought of it.:confused: But it means adding another view (Button) into MyCheckButton, and since MyCheckButton already manually colors its panels for Enabled, Checked, Unchecked, I think it will be more efficient to create a 1/5 second animation to simulate the Pressed color event.
 
Upvote 0
Top