6

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?

Mohit Kanwar
  • 2,962
  • 7
  • 39
  • 59
MyProg
  • 121
  • 1
  • 2
  • 6
  • 2
    Make your own switch class as a custom view. Android's just isn't made to be tri-state. Really, it can't be more than an hour to do in total, you'll wait longer than that hoping for answers here. – Gabe Sechan May 09 '14 at 06:47
  • yes for that you need to got with your custom widgets. – Mani May 09 '14 at 06:51
  • Yes that i know , that's why i have posted my question here ... please suggest me how can i add the custom widget for switch. – MyProg May 09 '14 at 06:57
  • Using a custom view, one will loose the benefit of themes and the Android look'n'feel, except by reproducing it all the way. Hence using the Switch class would be nice if possible at all. – 3c71 May 17 '15 at 17:13

1 Answers1

1

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.

  • Thank man for tutorial , but i am not getting , how to code for switch ? – MyProg May 09 '14 at 06:55
  • You create a class ThreeStateSwitch and you extend the android switch. Look up what method is used in Android switch to declare it's states and simply override that method to add in your third state. Here is the documentation on the android switch control: http://developer.android.com/reference/android/widget/Switch.html You can find most of this by simply looking it up in google btw. – Speedy Codezales May 09 '14 at 06:59
  • 1
    Pretty helpless answer, there's no way to add a third state to a Switch, if you find one please enlighten me. – 3c71 May 17 '15 at 17:12