16

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

bofredo
  • 2,348
  • 6
  • 32
  • 51
jstuardo
  • 3,901
  • 14
  • 61
  • 136

5 Answers5

38

You can do this through XML

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

Or Programatically as

mSwitch.setShowText(true);
Kavin Varnan
  • 1,989
  • 18
  • 23
2

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

bofredo
  • 2,348
  • 6
  • 32
  • 51
0

What worked for me was to use

app:showText="true"

rather than

android:showText="true"
Robert Columbia
  • 6,313
  • 15
  • 32
  • 40
pedagogyfirst
  • 43
  • 1
  • 8
0

Reason:

For me, app:showText="true" was set, and still the ON/OFF text didn't show; and the issue was because I was using a dark theme on one of the Switch parents like:

<AppBarLayout>
    ....
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    
     <Toolbar>
     
     <ConstraintLayout>
     
        <SwitchCompat>

This took me a while to figure out the reason.

Solution:

Change the switch theme to a light theme:

<SwitchCompat>
     ...
     android:theme="@style/Theme.AppCompat.Light"

Or you can remove the dark theme from the parent or set it to a light theme. because if your app supports dark mode, the text will only appear in the light mode.

Zain
  • 37,492
  • 7
  • 60
  • 84
-1

Hope this will help

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity"
        android:padding="5dp">

        <Switch
            android:id="@+id/mySwitch"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginTop="20dp"
            android:text="Play with the Switch" />

        <TextView
            android:id="@+id/switchStatus"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/mySwitch"
            android:layout_marginTop="22dp"
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium" />

    </RelativeLayout>

MainActivity.java

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Switch;
import android.widget.TextView;

public class MainActivity extends Activity {

 private TextView switchStatus;
 private Switch mySwitch;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  switchStatus = (TextView) findViewById(R.id.switchStatus);
  mySwitch = (Switch) findViewById(R.id.mySwitch);

  //set the switch to ON 
  mySwitch.setChecked(true);
  //attach a listener to check for changes in state
  mySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {

   @Override
   public void onCheckedChanged(CompoundButton buttonView,
     boolean isChecked) {

    if(isChecked){
     switchStatus.setText("Switch is currently ON");
    }else{
     switchStatus.setText("Switch is currently OFF");
    }

   }
  });

  //check the current state before we display the screen
  if(mySwitch.isChecked()){
   switchStatus.setText("Switch is currently ON");
  }
  else {
   switchStatus.setText("Switch is currently OFF");
  }
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.activity_main, menu);
  return true;
 }

}
Aditya Vyas-Lakhan
  • 13,409
  • 16
  • 61
  • 96