its a bit tricky as you cant write anything on track so you need a textview for your text on track and simply change its location when needed.
make your shapes or images ready then a switch and text view in a Constraint Layout... match them on same spot you need them then go for code and when switch turned on/off move the text view to other side... lol its work perfectly...
and for thumb text:
cSwitch.setTextOn("doctor");
cSwitch.setTextOff("user");
i know its have a lot of room to improve but this is how i did it and you can change the width and height for shapes to ...
this is my code i didnt make what you need lol you have to do it yourself haha
i hope this do any help.. cheers
enter image description here
enter image description here
switch track shape
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:width="11mm"
android:height="4.2mm">
<shape android:shape="rectangle">
<corners android:radius="3.7mm" />
<stroke
android:width="0.3mm"
android:color="@color/white" />
<solid android:color="@color/green" />
</shape>
</item>
</layer-list>
switch thumb shape
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<size
android:width="4.2mm"
android:height="4.2mm" />
<corners android:radius="2mm" />
<solid android:color="@color/white" />
<stroke
android:width="1dp"
android:color="#bdf7b8" />
</shape>
Layout XML code
<android.support.constraint.ConstraintLayout
android:id="@+id/signInLayout"
style="@style/LayoutStyle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:layout_marginTop="16dp">
<Switch
android:id="@+id/cSwitch"
android:layout_width="wrap_content"
android:layout_height="4mm"
android:switchMinWidth="11mm"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/cSwitch_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="2mm"
android:layout_marginStart="2mm"
android:text="ON"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="@id/cSwitch"
app:layout_constraintLeft_toLeftOf="@+id/cSwitch"
app:layout_constraintTop_toTopOf="@id/cSwitch" />
</android.support.constraint.ConstraintLayout>
and java code
final Switch cSwitch = rootView.findViewById(R.id.cSwitch);
final TextView cSwitchText = rootView.findViewById(R.id.cSwitch_textView);
cSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
ConstraintLayout constraintLayout = rootView.findViewById(R.id.signInLayout);
ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(constraintLayout);
constraintSet.connect(R.id.cSwitch_textView, ConstraintSet.TOP, R.id.cSwitch, ConstraintSet.TOP, 0);
constraintSet.connect(R.id.cSwitch_textView, ConstraintSet.BOTTOM, R.id.cSwitch, ConstraintSet.BOTTOM, 0);
cSwitch.setThumbDrawable(rootView.getResources().getDrawable(R.drawable.switch_thumb_green));
if (isChecked) {
cSwitchText.setText("ON");
constraintSet.connect(R.id.cSwitch_textView, ConstraintSet.LEFT, R.id.cSwitch, ConstraintSet.LEFT, 0);
constraintSet.connect(R.id.cSwitch_textView, ConstraintSet.RIGHT, ConstraintSet.UNSET, ConstraintSet.RIGHT, 0);
cSwitch.setTrackDrawable(ContextCompat.getDrawable(rootView.getContext(), R.drawable.switch_track_green));
} else {
cSwitchText.setText("OFF");
constraintSet.connect(R.id.cSwitch_textView, ConstraintSet.RIGHT, R.id.cSwitch, ConstraintSet.RIGHT, 0);
constraintSet.connect(R.id.cSwitch_textView, ConstraintSet.LEFT, ConstraintSet.UNSET, ConstraintSet.LEFT, 0);
cSwitch.setTrackDrawable(ContextCompat.getDrawable(rootView.getContext(), R.drawable.switch_track_red));
cSwitch.setThumbDrawable(rootView.getResources().getDrawable(R.drawable.switch_thumb_red));
}
constraintSet.applyTo(constraintLayout);
}
});