Record text on a track in a toggle button on Android

I am trying to configure the Switch button on Android. I have a problem that I want to show the text ON-OFF on a non-Thumb track by default Android. Can we customize this.

+4
source share
1 answer

Have you tried the Toggle Button? You can configure as a switch button. I use it like this. 1- Open xml in a folder with selection

Your Drawable xml @switch_thumb

 <?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:drawable="@drawable/on" />
    <item android:state_checked="false" android:drawable="@drawable/off" />
 </selector>

2-set background for the switch in the layout

Your xml layout

<ToggleButton
     android:layout_width="60dp"
     android:layout_height="30dp"
     android:textOn=""
     android:textOff=""
     android:background="@drawable/switch_thumb"
     android:id="@+id/toggle"
     />

3-basic action when checking is turned off and if you want to set marked

mToggle.setChecked(true);//mToggle.setChecked(prefs.getBoolean("toggle", false)

mToggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            //put in SharedPreferences 
            editor.putBoolean("toggle", mToggle.isChecked());
            editor.apply();
        }
    });
+1
source

All Articles