MainActivity.kt
package com.cfsuman.jetpackcompose
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.ClickableText
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.graphics.Color
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.withStyle
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 radioOptions = listOf("Java", "Kotlin", "C#")
val (selectedOption, onOptionSelected) = remember {
mutableStateOf(radioOptions[1])
}
Column(
Modifier
.fillMaxWidth()
.background(Color(0xFFEDEAE0)),
horizontalAlignment = Alignment.CenterHorizontally
) {
Card(
backgroundColor = Color(0xFFFAE7B5),
modifier = Modifier
.padding(15.dp)
.fillMaxWidth(0.90f),
elevation = 8.dp,
shape = RoundedCornerShape(12.dp),
) {
Column(
Modifier.padding(10.dp)
) {
Text(
text = "Which is your most favorite?.",
fontStyle = FontStyle.Normal,
fontWeight = FontWeight.Bold,
modifier = Modifier
.padding(5.dp),
textAlign = TextAlign.Start,
color = Color(0xFF3D2B1F)
)
radioOptions.forEach { item ->
Row(
Modifier
.padding(5.dp),
verticalAlignment = Alignment.CenterVertically
) {
RadioButton(
selected = (item == selectedOption),
onClick = { onOptionSelected(item) }
)
val annotatedString = buildAnnotatedString {
withStyle(
style = SpanStyle(
fontWeight = FontWeight.Bold
)
){
append(" $item ")
}
}
ClickableText(
text = annotatedString,
onClick = {
onOptionSelected(item)
}
)
}
}
}
}
Text(
text = "You selected : $selectedOption",
fontSize = 22.sp,
fontStyle = FontStyle.Normal,
fontWeight = FontWeight.Normal,
fontFamily = FontFamily.SansSerif,
modifier = Modifier
.padding(bottom = 15.dp)
.fillMaxWidth(),
textAlign = TextAlign.Center,
color = Color(0xFF665D1E)
)
}
}
@Preview
@Composable
fun ComposablePreview(){
//MainContent()
}
}
- jetpack compose - How to use Text
- jetpack compose - ClickableText example
- jetpack compose - How to use Button
- jetpack compose - How to use TextField
- jetpack compose - OutlinedTextField example
- jetpack compose - Password TextField example
- jetpack compose - Handle changes in a TextField
- jetpack compose - How to use Card
- jetpack compose - How to use Checkbox
- jetpack compose - Animate visibility change
- jetpack compose - Accessing string resources
- jetpack compose - String resource positional formatting
- jetpack compose - String resource plurals
- jetpack compose - Get dimension resource
- jetpack compose - Get color resource