The toggle button does not display the text ON and OFF on Android

I create a switch button programmatically. The problem is that the button does not display the text ON / OFF . 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 could be?

Thanks Jaime

+7
android android-layout android-switch
source share
3 answers

You can do it through XML

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

Or programmatically as

 mSwitch.setShowText(true); 
+13
source share

Here is the answer from the Android API. You must specify the switch so that it can show shortcuts at all!

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

Sets whether to turn on / off text.

Related XML Attributes:

 android:showText 

Parameters showText true to display text on / off

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

+2
source share

Hope this helps

 <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; } } 
0
source share

All Articles