I want to make a switch with 2 states clearly shown on it like this
Instead of this
I've tried to write this in the XML file
android:textOff="1"
android:textOn="2"
And this on the Java file
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Switch simpleSwitch = findViewById(R.id.simpleSwitch); // initiate Switch
simpleSwitch.setTextOff("1");
simpleSwitch.setTextOn("2");
}
}
But the result is always the latter with no text. Can someone guide me through this? It works with toggle buttons but the switch is more suitable for my UI.
In Android, ToggleButton is used to display checked and unchecked state of a button. ToggleButton basically an off/on button with a light indicator which indicate the current state of toggle button. The most simple example of ToggleButton is doing on/off in sound, Bluetooth, wifi, hotspot etc.
Certain View types, like Button , are denoted as clickable by default. In your app, if the View is not clickable, or does not perform an action when clicked, remove its OnClickListener or set android:clickable="false" .
Android Switch is also a two-state user interface element that is used to toggle between ON and OFF as a button. By touching the button we can drag it back and forth to make it either ON or OFF. The Switch element is useful when only two states require for activity either choose ON or OFF.
You can enable text inside switch. I think you forgot to add this line
android:showText="true" or Switch.setShowText(boolean)
finally your code be like this,
<Switch
android:id="@+id/switcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginBottom="16dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:fontFamily="sans-serif-condensed"
android:textOff="off"
android:showText="true"
android:textOn="on" />
Thank you
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With