How does Material 3 work on older android devices?

1k Views Asked by At

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?

1

There are 1 best solutions below

2
On

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
    )
}