Hide default text for android switch

In my Android app, I used the toggle button. Is there a way to hide the text of a toggle button? There is an option to hide (API: showText) for API level 21, but I need to run it on lower level devices.

Here is my xml file:

<Switch android:layout_width="100dp" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="7dp" android:background="@drawable/offbuttonbg" android:textAppearance="@style/SwitchTextAppearance" android:thumb="@drawable/switchselector" /> 
+11
android switch-statement button
source share
2 answers

For the lower version, to hide the text, use

  <Switch android:layout_width="100dp" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="7dp" android:textOff="" android:textOn="" android:background="@drawable/offbuttonbg" android:textAppearance="@style/SwitchTextAppearance" android:thumb="@drawable/switchselector" /> 
+14
source share

You have two options: one through the other code through XML.

Via Code:

 yourSwitch.setTextOff("textWhenOff") //Sets the text when the component is not in checked state. yourSwitch.setTextOn("textWhenOn") //Sets the text when the component is not in checked state. 

XML

 <Switch android:layout_width="100dp" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="7dp" android:textOff="The text for the component when it not checked." android:textOn="The text for the component when it checked." android:background="@drawable/offbuttonbg" android:textAppearance="@style/SwitchTextAppearance" android:thumb="@drawable/switchselector" /> 

Always refer to Switch Documentation if you need more information.

0
source share

All Articles