1

I'm starting learning jetpack compose, I'm trying to follow the tutorials shown here: https://developer.android.com/jetpack/compose/tutorial?hl=es-419

But in the part https://developer.android.com/jetpack/compose/tutorial?hl=es-419#color it says to use: "MaterialTheme.colors" to set the colors of the elements, the problem is that my new and empty project displays:

enter image description here

"Unsolved reference: colors"

I have added:

implementation 'androidx.compose.material3:material3:1.1.1'
implementation 'androidx.compose.material3:material3-window-size-class:1.1.1'

But I don't know how to have access to that element. How can I solved?

Eduardo Corona
  • 1,262
  • 4
  • 20
  • 31

3 Answers3

1

MaterialTheme.colors is refering the androidx.compose.material.MaterialTheme class, not androidx.compose.material3.MaterialTheme, which does not have the colors property.

You can write MaterialTheme.colorScheme.background instead when using Material3

Ricky Mo
  • 6,285
  • 1
  • 14
  • 30
1

You can opt for colorScheme of Material3 as suggested by @Ricky Mo

And if you still want to use MaterialTheme.colors.background then you need to update your import, i.e., replace import androidx.compose.material3.MaterialTheme with import androidx.compose.material.MaterialTheme

Megh
  • 831
  • 2
  • 12
1

In material3, you must use this method to call the colors

MaterialTheme.colorScheme.background

You can also add your own colors to the theme like this :

val ColorScheme.topAppbarColor: Color
@Composable
get() = Color.Black

And use this method to handle colors in dark theme

val ColorScheme.topAppbarColor: Color
@Composable
get() = if (isSystemInDarkTheme()) Color.Black else Purple40