Switch button does not show ON and OFF text in Android

jstuardo picture jstuardo · Oct 19, 2015 · Viewed 13.9k times · Source

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

Answer

Kavin Varnan picture Kavin Varnan · Oct 19, 2015

You can do this through XML

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

Or Programatically as

mSwitch.setShowText(true);