Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why switch's textOn and textOff are not displaying?

Tags:

android

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!

like image 549
Chester Lim Avatar asked Jun 09 '15 07:06

Chester Lim


People also ask

How to set text in a switch in Java?

3. text: text attribute is used to set the text in a Switch. We can set the text in xml as well as in the java class. Below we set the text “Sound” for the Switch. In the below code we set text of Switch via java class. 4. gravity: The gravity attribute is an optional attribute which is used to control the alignment of the text in Switch.

How to set the size of the text of a switch?

7. textSize: textSize attribute is used to set the size of the text of a Switch. We can set the text size in sp (scale independent pixel) or dp (density pixel). Below is the example code in which we set the 25sp size for the text of a Switch.

How to set the text style of a switch in Swift?

9. textStyle: textStyle attribute is used to set the text style of the text of a Switch. The possible text styles are bold, italic and normal. If we need to use two or more styles for a text view then use “|” operator. Below we set the bold and italic text styles for text of a Switch.

What is the use of textstyle attribute in Swift 9?

9. textStyle: textStyle attribute is used to set the text style of the text of a Switch. The possible text styles are bold, italic and normal. If we need to use two or more styles for a text view then use “|” operator.


2 Answers

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>
like image 74
George Avatar answered Oct 13 '22 22:10

George


Text is not shown by default under Material theme.

You can change it using the android:showText property

<Switch
    android:id="@+id/itemListSwitch"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textOn="Vibrate on"
    android:textOff="Vibrate off"
    android:showText="true" />
like image 40
jlopez Avatar answered Oct 13 '22 22:10

jlopez