I have created a custom Button as follows.
file : buttoncontrol.xml
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/greendot"
android:state_pressed="true" />
<item android:drawable="@drawable/reddot" />
</selector>
in the layout file.. I have used
<Button
...
android:background="@drawable/buttoncontrol"></Button>
When I try to run the program the layout shows the button with reddot (expected result). When I press the button, it turns green until I release the keypress. How can I make this green until the next click. More a toggle behaviour.
I have tried the same xml file in a toggle button as well. Same result.
Apart from this (for testing), I have also tried adding the following code in activity as well. Still not getting the desired result. (which is saving the state)
...under onClick of Button/ToggleButton
buttonControl.setPressed(true);
Any suggestions? My preference is to avoid programatically controlling the state. i.e. I can always keep these images and use buttonContol.setBackgroundDrawable for the buttons
Thanks
I made ToggleButton work by changing android:state_pressed=”true” to android:state_checked=”true” .
But the question still remains for Button. How to make button do a similar activity? Particularly when there are more than 2 states.