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.
DcodeReaderTheme
but you the changes you made are done toReaderTheme
.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 ?