I am trying to write an Android code to create three states for an Android Switch. Three state means ON - Default - OFF.
When I will open my app , the Switch will be in default mode and the button will be in centre. When I will drag the switch into left side then the state will change to ON and when I will drag to right side then the state will change to OFF.
In Android Switch , we have two state ON/OFF. I am not able to find any technique to add third state into the switch.
How can I add the third state in Android Switch?
Using scale property to change size worked for me. Add these lines to your <switch/> tag in xml file. You can change scale value as per your need. Here value 2 makes it double in size, similarly value 0.5 makes it half in size.
Since the Android 4.0 version (API level 14), it has another type of toggle button called switch which provides user slider control. Programmatically, isChecked() method is used to check the current state of the toggle button. This method returns a boolean value.
You could make a custom control based on the Android Switch control that has the 3 states you want.
You can find some info here:
http://developer.android.com/guide/topics/ui/custom-components.html
Tutorial:
http://www.tutorialspoint.com/android/android_custom_components.htm
I'd personally take the approach used in the tutorial. Just extend from the switch class in your 3 state switch and try to override the states to add a third one.
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