1

I want to create a custom switch control.

It's custom because I want to support Android min version: 2.2

Is there any native control I can inherit from?

Seekbar?

I want it to be like a toggleButton, but with the ability to both:

(1) move on single click

(2) be dragged right or left

ashatte
  • 5,442
  • 8
  • 39
  • 50
Elad Benda
  • 35,076
  • 87
  • 265
  • 471

2 Answers2

1

You can inherit from the CheckBox or the ToggleButton views. They work very similarly. You will need to make some drawables for your View

OR

You can use any of these libraries which bring the switch to Android 2.2

https://github.com/BoD/android-switch-backport

https://github.com/pellucide/Android-Switch-Demo-pre-4.0/tree/master/android-switch-demo

https://github.com/ankri/SwitchCompatLibrary

Basant Singh
  • 5,736
  • 2
  • 28
  • 49
0

SeekBar? Rather not. Most of SeekBar's code is logic for flattening the background drawable while preserving clip drawables.

99% of cases where in some other framework you would build a custom widget, in Android is just changing the background drawable. I believe you should be fine with just using a ToggleButton without any text but with some custom statelist drawable in the background.

fdreger
  • 12,264
  • 1
  • 36
  • 42