I’m a beginner in Android, I’m currently trying to develop in the Kotlin, and I’m using Jetpack Compose.
It’s a very simple page, but Android Studio doesn’t render renderings properly
Here’s the code
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun MainUi() {
val context = LocalContext.current
val backgroundColor = ContextCompat.getColor(context, R.color.yellow)
val subBackgroundColor = ContextCompat.getColor(context, R.color.milk_white)
val topBarTitle = ContextCompat.getString(context, R.string.home_title)
val topAppBarColors = TopAppBarColors(
containerColor = Color(backgroundColor),
titleContentColor = Color.White,
scrolledContainerColor = Color(backgroundColor),
navigationIconContentColor = Color(backgroundColor),
actionIconContentColor = Color(backgroundColor)
)
Column(
horizontalAlignment = Alignment.CenterHorizontally,
modifier = Modifier
.fillMaxSize()
.background(Color(backgroundColor))
) {
TopAppBar(
title = { Text(text = topBarTitle) },
colors = topAppBarColors
)
Column(
modifier = Modifier
.fillMaxSize()
.background(Color(subBackgroundColor))
) {
Text(
text = "This is the content of the page",
modifier = Modifier
.padding(16.dp)
.align(Alignment.CenterHorizontally)
)
}
}
}
@Preview(showBackground = true)
@Composable
fun MainPreview() {
WPJ_KotlinTheme {
MainUi()
}
}
If anyone knows the reason or solution, please tell me, thank you very much ! ! !
I tried to clean up and rebuild the project
And I updated Android Studio
2
Answers
Thank you for your responses
I tried the following two changes
The problem was solved successfully
To know exact problem you can click on the "Render issues" text
In this case the problem was a internal problem of compose. Problem is happening because you are not using the things as you are supposed to in Compose.
You should use "colorResource" & "stringResource" methods for reading colors and strings and other similar methods to get values from R class.
Here is a working code