I'm android developer recently I have to maintain an old application, so I need to add Material Components 3 and update to androidx.core
and androidx.appcompat
. As you might understand my idea was to reuse the maximum code as I could. But I face some problems for example. Change the switch
button color. I tried all the techniques I know and I follow all the answers in this thread.
So I found a solution by myself combining all the answers and suggestions here.
Lets start with the build.gradle
file:
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}
android {
...
compileSdk 33
defaultConfig {
...
minSdk 21
targetSdk 33
versionCode 3004006
versionName "3.4.6"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures {
viewBinding true
}
buildToolsVersion '32.0.0'
}
dependencies {
implementation 'androidx.core:core-ktx:1.9.0'
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.8.0'
implementation 'androidx.compose.material3:material3:1.0.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.navigation:navigation-fragment-ktx:2.5.3'
implementation 'androidx.navigation:navigation-ui-ktx:2.5.3'
implementation 'androidx.preference:preference:1.2.0'
...
}
We are using these dependencies and some configuration like minSdk 21
and targetSdk 33
so we can target almost 99% of the devices.
What I want is to change the color of the switch button defined in the preferences.xml
file Originally I was using SwitchPreference
but I couldn't change the color neither the thumb nor the track. I tried using custom colors modifying the colorPrimary
and colorAccent
or directly setting app:thumbTint
and app:trackTint
. Finally I decided to extend the SwitchPreference
class. like this:
First create a new layout preference_material_switch.xml
with for the MaterialSwitch
<com.google.android.material.materialswitch.MaterialSwitch xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/switchWidget"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:includeFontPadding="false"
android:lineSpacingExtra="0dp"
android:background="@null"
android:clickable="false"
android:focusable="false" />
class CustomSwitchPreference(context: Context, attrs: AttributeSet?) :
SwitchPreference(context, attrs) {
private var materialSwitch: MaterialSwitch? = null
init {
widgetLayoutResource = R.layout.preference_material_switch
}
override fun onBindViewHolder(holder: PreferenceViewHolder) {
super.onBindViewHolder(holder)
materialSwitch =
holder.findViewById(androidx.preference.R.id.switchWidget) as MaterialSwitch?
materialSwitch?.isChecked = isChecked
}
override fun onClick() {
super.onClick()
materialSwitch?.isChecked = isChecked
}
}
The key points here are init
here we assign preference_material_switch
as the widget layout. Then we link the state of the switch with this code materialSwitch?.isChecked = isChecked
Then I modified the preferences.xml
file:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<co.kr.rtt.kogas.station_manager.CustomSwitchPreference
android:layout="@layout/custom_preference"
android:defaultValue="true"
android:key="notification_preference"
android:title="@string/notification_preference_title"
android:summaryOff="@string/notification_preference_summary_off"
android:summaryOn="@string/notification_preference_summary_on" />
<PreferenceCategory>
<Preference
android:layout="@layout/custom_preference"
android:key="version_preference"
android:summary="@string/app_version"
android:title="@string/version_preference_title" />
<Preference
android:layout="@layout/custom_preference"
android:key="privacy_policy"
android:summary="@string/privacy_preference_summary"
android:title="@string/privacy_preference_title" />
<Preference
android:layout="@layout/custom_preference"
android:enabled="true"
android:key="terms_of_service"
android:summary="@string/tos_preference_summary"
android:title="@string/tos_preference_title" />
</PreferenceCategory>
</PreferenceScreen>
Here I setup a new layout: android:layout="@layout/custom_preference"
for all the preferences. I found the source code of the Preferences
layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingEnd="?android:attr/scrollbarSize">
<ImageView
android:id="@android:id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
android:layout_marginTop="6dp"
android:layout_marginEnd="6dp"
android:layout_marginBottom="6dp"
android:layout_weight="1"
android:minHeight="64dp">
<TextView
android:id="@android:id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toEndOf="@android:id/icon"
android:includeFontPadding="false"
android:lineSpacingExtra="0dp"
android:maxLines="1"
android:paddingTop="16dp"
android:paddingEnd="16dp"
android:textAppearance="?attr/textAppearanceTitleMedium"
android:textStyle="bold"
android:textColor="@color/accent" />
<TextView
android:id="@android:id/summary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@android:id/title"
android:layout_gravity="center_vertical"
android:layout_toEndOf="@android:id/icon"
android:lineSpacingExtra="0dp"
android:maxLines="2"
android:paddingEnd="16dp"
android:textAppearance="?attr/textAppearanceLabelMedium"
android:textColor="@color/dark" />
</RelativeLayout>
<LinearLayout
android:id="@android:id/widget_frame"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="vertical" />
</LinearLayout>
Here I was able to change the color, font family, size, padding, etc. I got total control of the layout.
As you can se the LinearLayout
instance with id widget_frame
will be replaced with the MaterialSwitch
instance.
I hope this will help you to have more control of your legacy apps.
Switch
to this custom selector afterwards. – Thera