0

I have my own colors defined and set in Theme.kt file:

I followed Studio bot advice and set those to my colors. But they just refuse to work. I get white and black respectively on light and dark theme instead of set colors.

private val DarkColorScheme = darkColorScheme(
    primary = Purple80,
    secondary = PurpleGrey80,
    tertiary = Pink80,
    background = BackgroundColorGray_DarkTheme,
    onBackground = Color.LightGray
)

private val LightColorScheme = lightColorScheme(
    primary = Purple40,
    secondary = PurpleGrey40,
    tertiary = Pink40,
    background = BackgroundColorGray_LightTheme,
    onBackground = Color.LightGray
    
)

@Composable
fun ReaderTheme(
    darkTheme: Boolean = isSystemInDarkTheme(),
    // Dynamic color is available on Android 12+
    dynamicColor: Boolean = true,
    content: @Composable () -> Unit
) {
    val colorScheme = when {
        dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
            val context = LocalContext.current
            if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context)
        }

        darkTheme -> DarkColorScheme
        else -> LightColorScheme
    }

    MaterialTheme(
        colorScheme = colorScheme,
        typography = Typography,
        content = content
    )
}

I even force those colors on my composables e.g

@Composable
fun ListButton(
    goToCollectionList: () -> Unit
) {
    Box(
        modifier = Modifier
            .size(190.dp)
            .clip(RoundedCornerShape(11.dp))
            .background(MaterialTheme.colorScheme.background) //RIGHT HERE
            .clickable { goToCollectionList() },
        contentAlignment = Alignment.Center,
    ) {
        Column(
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Icon(
                painter = painterResource(id = R.drawable.manage_list_icon),
                tint = IconGreen,
                contentDescription = null
            )
            Text(
                modifier = Modifier.padding(top = 8.dp, bottom = 10.dp),
                text = "Manage List",
                fontSize = 19.sp
            )
        }
    }
}

Do i have to call my theme in navigation or something, because currently I use it in my Main ?

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()

        setContent {
            val viewModel = viewModel<ReaderViewModel>()
            ReaderTheme {
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    ReaderNavHost(navController = rememberNavController(),viewModel =viewModel)
                }
            }
        }
    }
}

No idea why its not working. Its my first time setting this up.

4
  • You are using the wrong theme: In your MainActivity you use DcodeReaderTheme but you the changes you made are done to ReaderTheme.
    – Leviathan
    Commented May 24 at 6:52
  • That was just a typo during editing Commented May 24 at 8:29
  • You are using dynamic color, so you won't be able to set a specific color unless on older versions.
    – andylee
    Commented May 24 at 8:44
  • I removed kt dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> { val context = LocalContext.current if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context) } and it applied the right color but after changing to darkMode the app crashed how can i prevent that ? Commented May 24 at 10:17

0