Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iphone switch like button for android

Tags:

android

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>
like image 481
raaj Avatar asked Nov 15 '22 02:11

raaj


1 Answers

There is CompoundButton which is probably what you want. Then there are a couple of subclasses specializing it too.

like image 179
alexanderblom Avatar answered Dec 18 '22 19:12

alexanderblom