56

The behavior of the switch widget changed in Lollipop (5.0).

    <Switch
        android:id="@+id/switcher"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_marginBottom="16dp"
        android:layout_marginRight="8dp"
        android:layout_marginEnd="8dp"
        android:layout_toEndOf="@id/another_view"
        android:layout_toRightOf="@id/another_view"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:textOff="@string/disabled"
        android:textOn="@string/enabled"
        android:fontFamily="sans-serif-condensed"
        />

Rendered switch when targetSdkVersion=19:

enter image description here

Rendered switch when targetSdkVersion=21:

enter image description here

Note that preview rendering in Android Studio still produces a switch with text, but the switch loses it's text when an apk built with targetSdkVersion=21 is run on a device with Lollipop (Nexus 5). Running an apk built with targetSdkVersion=19 on the same Lollipop device renders the switch properly with text as expected.

Why? Any suggested workarounds?

1 Answer 1

141

Text is not shown by default under Material theme since the switch widget assets don't work well with text. Any text that you do set will be used to describe the content to accessibility services.

You can change this using the android:showText property or Switch.setShowText(boolean) method.

<Switch
    ...
    android:showText="true" />

If you are using AppCompat switches, use app:showText instead.

8
  • Thank you, this helped me as well.
    – Kitalda
    May 12, 2015 at 8:53
  • This attribute is indeed supported from API level 21 onwards.
    – manfcas
    Apr 7, 2016 at 16:45
  • 2
    Doesn't seem to work any more, using 23.4.0 running on an N-Preview3 device. Edit: Should be app:showText="true"
    – behelit
    May 26, 2016 at 6:38
  • 12
    This works but not good for the android.support.v7.widget.SwitchCompat becauae the text then is visible awkwardly over the switch thumb.
    – sud007
    Sep 27, 2016 at 7:12
  • 1
    Widget.Holo.Light.CompoundButton.Switch style is behind that trusty old squarish look Jul 5, 2017 at 16:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.