Current state: enter image description here
Need: enter image description here
track changes size depending on the size of thumb
Doesn't work
android:switchMinWidth="52dp"
My xml code: activity_main.xml
<Switch
android:id="@+id/switchViewOn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:text="Switch On"
android:thumb="@drawable/switch_thumb"
android:track="@drawable/switch_track"
tools:ignore="UseSwitchCompatOrMaterialXml" />
<Switch
android:id="@+id/switchViewOff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Switch Off"
android:thumb="@drawable/switch_thumb"
android:track="@drawable/switch_track"
tools:ignore="UseSwitchCompatOrMaterialXml" />
switch_thumb.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false">
<shape android:dither="true" android:shape="rectangle" android:useLevel="false" android:visible="true">
<solid android:color="#ffffff" />
<corners android:radius="32dp" />
<size android:width="32dp" android:height="32dp" />
<stroke android:width="8dp" android:color="#00FFFFFF" />
</shape>
</item>
</selector>
switch_track.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true">
<shape android:dither="true" android:shape="rectangle" android:useLevel="false" android:visible="true">
<solid android:color="#FF7733" />
<corners android:radius="32dp" />
<size android:width="52dp" android:height="32dp" />
</shape>
</item>
<item android:state_checked="false">
<shape android:dither="true" android:shape="rectangle" android:useLevel="false" android:visible="true">
<solid android:color="#D5D5D6" />
<corners android:radius="32dp" />
<size android:width="52dp" android:height="32dp" />
</shape>
</item>
</selector>
I tried changing Switch to
"androidx.appcompat.widget.SwitchCompat"
because the thumb is not letting the width
change also your android:switchMinWidth="52dp" did not work. The
output reached is not similar but to some extent it is nearly
working.
Also since using androidx so had to change these as well -
app:thumb="@drawable/switch_thumb" app:track="@drawable/switch_track"
activity_mail.xml is below:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.appcompat.widget.SwitchCompat
android:id="@+id/switchViewOn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="140dp"
android:layout_marginBottom="380dp"
android:text="@string/switch_on"
app:thumb="@drawable/switch_thumb"
app:track="@drawable/switch_track"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/switchViewOff"
tools:ignore="UseSwitchCompatOrMaterialXml" />
<androidx.appcompat.widget.SwitchCompat
android:id="@+id/switchViewOff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="188dp"
android:text="@string/switch_off"
app:thumb="@drawable/switch_thumb"
app:track="@drawable/switch_track"
app:layout_constraintEnd_toEndOf="@+id/switchViewOn"
app:layout_constraintStart_toStartOf="@+id/switchViewOn"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="UseSwitchCompatOrMaterialXml" />
</androidx.constraintlayout.widget.ConstraintLayout>