'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
Solution 1:[1]
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:showTextParameters showText true to display on/off text
http://developer.android.com/reference/android/widget/Switch.html#setShowText%28boolean%29
Solution 2:[2]
What worked for me was to use
app:showText="true"
rather than
android:showText="true"
Solution 3:[3]
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.
Solution 4:[4]
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;
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | bofredo |
| Solution 2 | Robert Columbia |
| Solution 3 | Zain |
| Solution 4 | Aditya Vyas-Lakhan |
