Chester Lim
Chester Lim

Reputation: 519

Why switch's textOn and textOff are not displaying?

I am trying to make a listview with an on/off switch. I found this simple code but the problem is it is not working for me.

<Switch
    android:id="@+id/itemListSwitch"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textOn="Vibrate on"
    android:textOff="Vibrate off"/>

Yes, it displays a switch but the text (Vibrate on/off) is not. Only a circle that can be switched is displayed. I want to post a screenshot but i am not allowed to because i lack reputation. Can anyone tell me why because i tried to find answers but a simple code like the one above is working for them. If it helps, my Android phone version is 5.0.2. Thanks in advance!

Upvotes: 12

Views: 8675

Answers (1)

George
George

Reputation: 301

First of all you should use SwitchCompat in order to make it compatible with all android versions and have the nice look of material design of the switch.

Back to your problem, you are missing an attribute for showing the text -app:showText -, here is an example:

<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  ...>
    <android.support.v7.widget.SwitchCompat
        android:id="@+id/switch_compat"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="50dp"
        android:layout_marginStart="50dp"
        android:layout_marginTop="25dp"
        android:checked="false"
        android:textOff="OFF"
        android:textOn="ON"
        app:showText="true"/>
</RelativeLayout>

Upvotes: 19

Related Questions