Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jetpack compose theme color not applied

I created to kt files MainActivity.kt and MainPage.kt, I pu my own colors in color.ket and refeerenced them in theme.kt

Omposables in MainActivity.kt get the right colors but MainPage.kt do not

I have a card in MainPage.kt:

Card(
    colors = CardDefaults.cardColors(
        containerColor = MaterialTheme.colorScheme.onPrimary
    ),
    modifier = Modifier
        .clip(cardShape)
        .height(200.dp)
        .width(cardWidth)
        .border(
            width = 3.dp,
            color = MaterialTheme.colorScheme.secondaryContainer,
            shape = cardShape
        )
) {
    Column(modifier = Modifier.padding(start = 15.dp, top=10.dp)) {
        Text(text = "Info Card bottom")
    }
}

MaterialTheme.colorScheme.onPrimary (material3) should be greenish, but it gives me light purple.

Any ideas, plesae?

*** I am learning making android app and I probably just dd something wrong.

I deleted the original purple colors in color.kt so I do not know where that color comes from. I also pur dynamicColor to false in theme.kt

like image 812
zouheir bellouki Avatar asked Dec 01 '25 16:12

zouheir bellouki


2 Answers

May be helpful:

Sometimes all you need is to set: dynamicColor: Boolean = false, in your AppTheme Composable in Theme.kt.

like image 123
Farouk Sabiou Avatar answered Dec 04 '25 06:12

Farouk Sabiou


Try removing(or commenting) the following code snippet from the colorScheme in your Theme.kt file.

dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
    val context = LocalContext.current
    if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context)
}

It should be like:

val colorScheme = when {
    darkTheme -> darkColorScheme
    else -> lightColorScheme
}
like image 45
Sadisha Avatar answered Dec 04 '25 07:12

Sadisha



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!