Building modern Android applications with polished UI requires migrating legacy Theme.AppCompat themes to Google’s latest Material Design 3 (Material You) library. Material 3 provides automatic dark mode switching, dynamic color extraction from user wallpapers on Android 12+, and updated component styling for buttons, text fields, and dialogs.
Step 1: Add Material Components Dependency in build.gradle.kts
Add the official Material Components library to your module-level build.gradle.kts file:
dependencies {
// Material Components for Android (Material 3)
implementation("com.google.android.material:material:1.11.0")
}Step 2: Configure Theme.Material3 in res/values/themes.xml
Update your application theme in res/values/themes.xml to inherit from Theme.Material3.DayNight.NoActionBar:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Base Application Theme inheriting from Material 3 -->
<style name="Theme.MyApp" parent="Theme.Material3.DayNight.NoActionBar">
<!-- Brand Primary Color Tokens -->
<item name="colorPrimary">@color/md_theme_primary</item>
<item name="colorOnPrimary">@color/md_theme_onPrimary</item>
<item name="colorPrimaryContainer">@color/md_theme_primaryContainer</item>
<!-- Background & Surface Colors -->
<item name="android:colorBackground">@color/md_theme_background</item>
<item name="colorSurface">@color/md_theme_surface</item>
<item name="colorOnSurface">@color/md_theme_onSurface</item>
</style>
</resources>Step 3: Enable Dynamic Colors in Application Class (Android 12+)
To apply wallpaper-based dynamic color palette extraction on Android 12 (API level 31) and higher, call DynamicColors.applyToActivitiesIfAvailable() inside your Application class:
package com.example.myapp
import android.app.Application
import com.google.android.material.color.DynamicColors
class MainApplication : Application() {
override fun onCreate() {
super.onCreate()
// Automatically apply Material 3 Dynamic Colors to all Activities
DynamicColors.applyToActivitiesIfAvailable(this)
}
}
Comments and corrections