How to have a Lollipop switch button

stoefln picture stoefln · Apr 20, 2015 · Viewed 26k times · Source

I want to have the Lollipop style switch button for my app:

enter image description here

How could I implement this button so it looks like this also on older versions of android?

Answer

dzikovskyy picture dzikovskyy · Aug 26, 2015

To have the Lollipop style switch button on older versions of android you should use SwitchCompat in layout xml file

<android.support.v7.widget.SwitchCompat
        android:id="@+id/compatSwitch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

and also in java file

SwitchCompat switchCompat = (SwitchCompat) findViewById(R.id.compatSwitch);