How to use string resources in Android Jetpack Compose?

There’s androidx.compose.ui.res package containing functions for loading string resources as well as other resource types. string You can get a string using stringResource() function, for example: … import androidx.compose.ui.res.stringResource @Composable fun StringResourceExample() { Text( // a string without arguments text = stringResource(R.string.app_name) ) Text( // a string with arguments text = stringResource(R.string.greeting, “World”) ) } … Read more

Jetpack Compose how to remove EditText/TextField underline and keep cursor?

You can define these attributes to apply a Transparent color: focusedIndicatorColor unfocusedIndicatorColor disabledIndicatorColor Something like: TextField( //.. colors = TextFieldDefaults.textFieldColors( textColor = Color.Gray, disabledTextColor = Color.Transparent, backgroundColor = Color.White, focusedIndicatorColor = Color.Transparent, unfocusedIndicatorColor = Color.Transparent, disabledIndicatorColor = Color.Transparent ) ) Starting with 1.2.0 you can also use the new OutlinedTextFieldDecorationBox together with BasicTextField customizing the … Read more

What does Jetpack Compose remember actually do, how does it work under the hood?

remember – allows you to remember state from previous recompose invocation and just this. So, if you for instance randomize color at initial run. The randomized color is going to be calculated only once and later reused whenever re-compose is necessary. And hence, remember = store the value just in case recompose is called. Now, … Read more

Equivalent of constraint layout 0dp

Use Dimension.fillToConstraints: A Dimension that spreads to match constraints. Links should be specified from both sides corresponding to this dimension, in order for this to work. Add this line to your Text modifier: width = Dimension.fillToConstraints So it becomes: Text( text = “This would be some text”, style = TextStyle( color = Color.Black, fontSize = … Read more

ComponentActivity vs AppCompatActivity in Android Jetpack Compose

AppCompatActivity extends FragmentActivity which extends ComponentActivity. ComponentActivity has all you need for a Compose-only app. If you need AppCompat APIs, an AndroidView which works with AppCompat or MaterialComponents theme, or you need Fragments then use AppCompatActivity. Note: it requires at least the AppCompat 1.3.0 version.

Match Width of Parent in Column (Jetpack Compose)

You can use the Modifier .width(IntrinsicSize.Max) Column(Modifier.width(IntrinsicSize.Max)) { Box(Modifier.fillMaxWidth().background(Color.Gray)) { Text(“Short text”) } Box(Modifier.fillMaxWidth().background(Color.Yellow)) { Text(“Extremely long text giving the width of its siblings”) } Box(Modifier.fillMaxWidth().background(Color.Green)) { Text(“Medium length text”) } }

How to create rounded border Button using Jetpack Compose

To achieve a button with a border with rounded corners you can use the OutlinedButton component applying in the shape parameter a RoundedCornerShape: OutlinedButton( onClick = { }, border = BorderStroke(1.dp, Color.Red), shape = RoundedCornerShape(50), // = 50% percent // or shape = CircleShape colors = ButtonDefaults.outlinedButtonColors(contentColor = Color.Red) ){ Text( text = “Save” ) … Read more

Jetpack Compose Text hyperlink some section of the text

For a complete answer you can use ClickableText which returns the position of text, and UriHandler to open URI in a browser. val annotatedLinkString: AnnotatedString = buildAnnotatedString { val str = “Click this link to go to web site” val startIndex = str.indexOf(“link”) val endIndex = startIndex + 4 append(str) addStyle( style = SpanStyle( color … Read more