I have created a toggle button in android and I have used a selector to change the background that switches between the on and off backgrounds.
My problem is how to make the background remain in the state it will be clicked, for example, if I press on "on", the background should remain in "on" background state, but in my case as soon as I stop pressing the button, it reverts to the original background.
Does anyone has any idea how to implement it?
Here is my selector file:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/off" /> <!-- default -->
<item android:state_pressed="true" android:drawable="@drawable/on" /> <!-- on state -->
<item android:state_focused="false" android:drawable="@drawable/off" /> <!-- off state -->
</selector>
There is CompoundButton which is probably what you want. Then there are a couple of subclasses specializing it too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With