I've migrated my project to AndroidX
. My MainActivity
extend FragmentActivity
my first SwitchCompat
looks all white, it doesn't have any color at all when I first time come to that screen. SwitchCompat is white. All other SwitchCompact under it are with correct color. If I press back and come again to that screen, my first SwitchCompact
receive correct color and looks fine.
If I change that my MainActivty
extend AppCompactActivity
, then everything is ok when I first time reach that screen. Does anyone know where is problem here because before migration my MainActivity
also extend FragmentActivity
and everything was fine.
My xml
code is the same in the both case:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".BlankFragment">
<androidx.appcompat.widget.SwitchCompat
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<androidx.appcompat.widget.SwitchCompat
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
I didn`t go deep why this problem occurs when you extend FragmentActivity. but to show appropriate colors on the action event of "SwitchCompat" can achievable by using custom track and thumb.
your switch button
<androidx.appcompat.widget.SwitchCompat
android:id="@+id/sw_autoplay"
android:layout_width="44dp"
android:layout_height="25dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:paddingStart="4dp"
android:paddingEnd="4dp"
android:textOff=""
android:textOn=""
android:theme="@style/SwitchButtonTheme"
android:thumb="@drawable/thumb"
app:track="@drawable/track" />
thumb
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false">
<shape android:shape="oval">
<solid android:color="#ffffff" />
<size android:width="20dp" android:height="20dp" />
<stroke android:width=".5dp" android:color="#A4A0A0" />
</shape>
</item>
<item android:state_checked="true">
<shape android:shape="oval">
<solid android:color="#ffffff" />
<size android:width="20dp" android:height="20dp" />
<stroke android:width=".5dp" android:color="#57F47F" />
</shape>
</item>
track
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false">
<shape android:shape="rectangle">
<corners android:radius="20dp" />
<solid android:color="#ffffff" />
<stroke android:width=".3dp" android:color="#A4A0A0" />
<size android:height="20dp" />
</shape>
</item>
<item android:state_checked="true">
<shape android:shape="rectangle">
<corners android:radius="20dp" />
<solid android:color="#57F47F" />
<stroke android:width=".3dp" android:color="#A4A0A0" />
<size android:height="20dp" />
</shape>
</item>
It`s just for when you want you to use "FragmentActivity".
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