I'm a beginner in android and jetpack compose and I want to build apps with Material 3, but Material 3 works only on Android 12 and above. Does that mean that the app will not work on older android devices or it will automatically switch to Material 2 components?
Asked
Active
Viewed 475 times
1 Answers
1
Material3 doesn't have this limit. It works also for android<12.
Dynamic color is available on Android 12 (API level 31) and above.
If dynamic color is available, you can set up a dynamic ColorScheme
.
If not, you should fall back to using a custom light or dark ColorScheme
.
Something like:
fun supportsDynamic() : Boolean = Build.VERSION.SDK_INT >= Build.VERSION_CODES.S
@Composable
fun M3Theme(content: @Composable() () -> Unit) {
val inDarkMode: Boolean = isSystemInDarkTheme()
val colors = if (supportsDynamic()) {
val context = LocalContext.current
if (inDarkMode) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context)
} else {
if (inDarkMode) DarkColorScheme else LightColorScheme
}
androidx.compose.material3.MaterialTheme(
colorScheme = colors,
content = content
)
}

Gabriele Mariotti
- 320,139
- 94
- 887
- 841
-
Thanks for the explanation! So does that mean that the only thing for Android 12 and above is dynamic color and that the UI components will work fine on any device? – Youssef Emad Fakhry Nov 08 '22 at 08:24
-
1@YoussefEmadFakhry Starting from Android5 (minSdk=21). – Gabriele Mariotti Nov 08 '22 at 08:35