Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch button does not show ON and OFF text in Android

I am creating a switch button programmatically. The problem I have is that the button does not show the ON / OFF text. This is the creation code:

        final RelativeLayout.LayoutParams ll = new RelativeLayout.LayoutParams(
              RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        ll.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        sw.setLayoutParams(ll);
        sw.setTextOn(values[0].getName());
        sw.setTextOff(values[1].getName());
        values[0].getName() returns "OK", and values[1].getName() returns "NOK"

What may be going on?

Thanks Jaime

like image 472
jstuardo Avatar asked Oct 19 '15 13:10

jstuardo


2 Answers

You can do this through XML

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

Or Programatically as

mSwitch.setShowText(true);
like image 52
Kavin Varnan Avatar answered Oct 22 '22 04:10

Kavin Varnan


Here is the answer from Android-API. You have to flag the switch that it can show labels at all!!

public void setShowText (boolean showText) Added in API level 21

Sets whether the on/off text should be displayed.

Related XML Attributes:

android:showText

Parameters showText true to display on/off text

http://developer.android.com/reference/android/widget/Switch.html#setShowText%28boolean%29

like image 2
bofredo Avatar answered Oct 22 '22 05:10

bofredo