MainActivity.kt
package com.cfsuman.jetpackcompose
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
class MainActivity : AppCompatActivity() {
@ExperimentalAnimationApi
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MainContent()
}
}
@ExperimentalAnimationApi
@Composable
fun MainContent(){
val isVisible = remember { mutableStateOf(value = false) }
Column(
modifier = Modifier
.background(Color.LightGray)
.fillMaxSize()
.padding(16.dp),
verticalArrangement = Arrangement.spacedBy(16.dp)
) {
Button(
onClick = { isVisible.value = !isVisible.value }
) {
Text(
text = if (isVisible.value) "Hide" else "Show"
)
}
AnimatedVisibility(isVisible.value) {
Box(
modifier = Modifier
.fillMaxWidth()
.height(275.dp)
.background(Color.Red)
)
}
}
}
@Preview
@Composable
fun ComposablePreview(){
//MainContent()
}
}
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiY-9HZ2dbrWKuEXQJAyRXMs3w32PbgrVNGTgV-G1myNrkxZDDqG62q6xwIpwry-_4IV2ucOD7ih7NEdlZrWv9xBdPnMERwc40ip1PQXaXP5to6A3Lb9FsQ7p6Efg6HwfaNmyS_pMOgGEuX/s0/AndroidJetpackComposeAnimateVisibilityChange.png)
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh6nZi6O21gCgBAo3xpPLMsOIaSYtM4n3BDVIyLHeVO-TCpaKmpDzsBsuPZelPPYZKWRhXCbR_NtOmkhI-1Y3KXC56abo0IuBz6HVPQKmuJ2AuoqU7oKZaIqaNjBfJQ6jX7DFA5UV84RZQu/s0/AndroidJetpackComposeAnimateVisibilityChange2.png)
- jetpack compose - How to use Text
- jetpack compose - ClickableText example
- jetpack compose - Password TextField example
- jetpack compose - Handle changes in a TextField
- jetpack compose - Radio group example
- jetpack compose - How to use Floating Action Button
- jetpack compose - How to use IconToggleButton
- jetpack compose - How to use Column layout
- jetpack compose - How to use Slider
- jetpack compose - How to use Switch
- jetpack compose - Image from URL
- jetpack compose - Image from assets folder
- jetpack compose - How to use LazyColumn
- jetpack compose - How to use LazyRow
- jetpack compose - Animate color change