"render Issues" with Jetpack Compose - android

I am having some render issue while showing the jetpack components. The screenshot is like this
If I click on the error icon from the screenshot, it shows
java.lang.ClassNotFoundException: androidx.activity.compose.LocalOnBackPressedDispatcherOwner   
at java.lang.ClassLoader.loadClass(ClassLoader.java:589)   
at java.lang.ClassLoader.loadClass(ClassLoader.java:522)   
at androidx.compose.ui.tooling.ComposeViewAdapter.WrapPreview   ... (ComposeViewAdapter.kt:528)   
at androidx.compose.ui.tooling.ComposeViewAdapter.access$WrapPreview(ComposeViewAdapter.kt:124)   
at androidx.compose.ui.tooling.ComposeViewAdapter$init$3.invoke(ComposeViewAdapter.kt:583)  
at androidx.compose.ui.tooling.ComposeViewAdapter$init$3.invoke(ComposeViewAdapter.kt:580)
The composable is something like
#Composable
#Preview
fun TestCompose(){
Column {
Text(text = "something")
}
}

Related

How to create an annotation that composes other annotations in kotlin for composable functions?

I would like to create an annotation called #MyComposablePreview that is composed of:
#Preview(
uiMode = Configuration.UI_MODE_NIGHT_NO,
showBackground = true,
name = "Light Mode"
)
#Preview(
uiMode = Configuration.UI_MODE_NIGHT_YES,
showBackground = true,
name = "Dark Mode"
)
#Composable
So that I could call it in a composable function like this:
#MyComposablePreview
fun CardPreiview() {
//Code of preview
}
Is it possible to do this in jetpack compose? I tried to use annotated classes but I couldn't :/

Custom class in layouts on JetpackCompose

I've read the documentation on how to compose basic Layouts . I wonder if a class can be implemented in order to create these views. EG from Android
#Composable
fun ArtistCard(artist: Artist) {
Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.End
) {
Image(/*...*/)
Column { /*...*/ }
}
}
That produces some kind of CardView, but can that be converted in a class that I can just provide the image and title? In order to create a mutable list or I'm missing something else that exists?
I have no code about the class mentioned since I don't know how to start in this case with Jetpack components.

Add a text element using jetpack compose?

I am trying lesson 1
However, the first step Add a text element is not working, i just get a blank screen.
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
Text("Hello world!")
}
}
}
What may be happening, is that since you're not defining a theme to work with, the text color might be white, as well as the background.
If your background is white, try to replace
Text("Hello world!")
with
Text(text = "Hello world!", color = Color.Black)

MutableState variable cannot be changed again on the subsequent click of Jetpack Compose Checkbox in Beta02, but works on Beta01

I have a simple Jetpack Compose checkbox (modified slightly from the Compose Template Sample design from Android Studio Preview Canary 11), when it check it will switch from false to true and vice versa...
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
val darkMode = remember { mutableStateOf(false) }
ExperimentTheme(darkMode.value) {
// A surface container using the 'background' color from the theme
Surface(modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colors.background) {
DarkModeCheckBox(darkMode)
}
}
}
}
}
#Composable
fun DarkModeCheckBox(darkMode: MutableState<Boolean>) {
Row {
Checkbox(
checked = darkMode.value,
onCheckedChange = { checked -> darkMode.value = checked },
)
}
}
As it switch, it will change the color mode from LightMode to DarkMode and vice versa, as shown in GIF below.
This works in
compose_version = '1.0.0-beta01'
// and
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.30"
However when I move it to
compose_version = '1.0.0-beta02'
// and
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.31"
It can only switch from false to true once. Then the value stuck and not changeable with the subsequent click. Is it because in 1.0.0-beta02 the behavior of changing mutable state is different now? If it has changed, how should I fix the above?
This code works without issues with 1.0.0-beta03.
There is a bug on MaterialTheme in beta02 fixed in beta03.

Problems with Android Jetpack Navigation

I have created an Empty Compose Activity template using Android Studio Canara 2020.03. Here is the code of the file " MainActivity. kt":
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
ButtonPage()
}
}
}
#Composable
fun ButtonPage(){
Button(onClick = {}){Text("Click to go next")}
}
#Composable
fun TextPage(){
Text("Second Page")
}
How do I modify the code so that when you click on this button, it draws only text? (That is, you need that when you click the button, the program draws other content by deleting this one first).
Jetpack Compose version 1.0.0-alpha09, jdk version 15, android version 11
Thank you in advance
A simple solution would be to have a variable that dictates the current screen.
var showSecondScreen by remember { mutableStateOf(false) }
if (!showSecondScreen) {
Button(onClick = {showSecondScreen = true}){Text("Click to go next")}
} else {
Text("Second screen")
}
This doesn't have to be a boolean, you could declare var currentScreen by remember { mutableStateOf("homeScreen") } and use a when block for which screen to show.
#Composable fun MyApp(currentScreen: String) {
when (currentScreen) {
"homeScreen" -> HomeScreen()
"secondScreen" -> SecondScreen()
}
}
So you can think of it less as a transaction and more as a stateful navigation.
But you'll soon realize that this doesn't handle back navigation, so you'll need a navigation library like jetpack compose navigation, decompose, compose-router, etc. Here's more information on jetpack compose navigation.

Categories

Resources