MainActivity.kt
package com.cfsuman.jetpackcompose
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.compose.animation.*
import androidx.compose.animation.core.*
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MainContent()
}
}
@Composable
fun MainContent(){
val shortText = "This is a short text."
val longtext = "Lorem Ipsum is simply dummy text of the printing" +
" and typesetting industry. Lorem Ipsum has been the" +
" industry's standard dummy text ever since the 1500s," +
" when an unknown printer took a galley of type and" +
" scrambled it to make a type specimen book."
val isShortText = remember { mutableStateOf(true) }
val result = remember { mutableStateOf("")}
Column(
modifier = Modifier
.fillMaxSize()
.background(Color(0xFFBCD4E6))
.padding(16.dp),
verticalArrangement = Arrangement.spacedBy(16.dp),
horizontalAlignment = Alignment.CenterHorizontally
) {
Button(
onClick = { isShortText.value = !isShortText.value },
colors = ButtonDefaults.buttonColors(
Color(0xFF2D383A), Color(0xCCFFFFFF)
)
) {
Text(
text = "Animate Content Size",
modifier = Modifier.padding(12.dp)
)
}
Box(
modifier = Modifier
.clip(RoundedCornerShape(16.dp))
.background(Color(0xFF4F42B5))
.padding(16.dp)
.fillMaxWidth()
.animateContentSize(
// spring creates a physics-based animation
// between start and end values.
animationSpec = spring(
// dampingRatio defines how bouncy the spring should be.
dampingRatio = Spring.DampingRatioHighBouncy,
// stiffness defines how fast the spring should
// move toward the end value.
stiffness = Spring.StiffnessLow
),
finishedListener = { initialSize,endSize ->
result.value = "Form ($initialSize) \nTo ($endSize)"
}
)
) {
Text(
text = if (isShortText.value) shortText else longtext,
fontSize = 30.sp,
fontWeight = FontWeight.Bold,
textAlign = TextAlign.Center,
fontFamily = FontFamily.Cursive,
color = Color(0xFFA4DDED)
)
}
Text(
text = result.value,
fontSize = 20.sp,
fontFamily = FontFamily.Serif
)
}
}
@Preview
@Composable
fun ComposablePreview(){
//MainContent()
}
}
- 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 - LinearProgressIndicator example
- jetpack compose - Repeatable animation
- jetpack compose - Snap animation
- jetpack compose - Combine multiple transitions
- jetpack compose - AnimateContentSize customization