How to convert Dp to pixels in Android Jetpack Compose?

There’re toPx() and roundToPx() methods defined by Density interface, you can use it like this: import androidx.compose.ui.platform.LocalDensity val pxValue = with(LocalDensity.current) { 16.dp.toPx() } // or val pxValue = LocalDensity.current.run { 16.dp.toPx() } Such an expression might look confusing if you’re new to Kotlin language so let’s break it down and see how it works. … Read more

Show custom alert dialog in Jetpack Compose

Starting from M3 1.1.0-alpha04 there is an AlertDialog composable function with a slot for content. val openDialog = remember { mutableStateOf(true) } if (openDialog.value) { androidx.compose.material3.AlertDialog( onDismissRequest = { // Dismiss the dialog when the user clicks outside the dialog or on the back // button. If you want to disable that functionality, simply use … Read more

Toggle password field jetpack compose

You can use the standard TextField composable: var password by rememberSaveable { mutableStateOf(“”) } var passwordVisible by rememberSaveable { mutableStateOf(false) } TextField( value = password, onValueChange = { password = it }, label = { Text(“Password”) }, singleLine = true, placeholder = { Text(“Password”) }, visualTransformation = if (passwordVisible) VisualTransformation.None else PasswordVisualTransformation(), keyboardOptions = KeyboardOptions(keyboardType … Read more

Draw a line in jetpack compose

You can use Divider Composable method for Horizontal line like below. Divider(color = Color.Blue, thickness = 1.dp) Example : @Composable fun drawLine(){ MaterialTheme { VerticalScroller{ Column(modifier = Spacing(16.dp), mainAxisSize = LayoutSize.Expand) { (0..3).forEachIndexed { index, i -> Text( text = “Draw Line !”, style = TextStyle(color = Color.DarkGray, fontSize = 22.sp) ) Divider(color = Color.Blue, … Read more

Jetpack compose – how do I refresh a screen when app returns to foreground

I came up with this: @Composable fun OnLifecycleEvent(onEvent: (owner: LifecycleOwner, event: Lifecycle.Event) -> Unit) { val eventHandler = rememberUpdatedState(onEvent) val lifecycleOwner = rememberUpdatedState(LocalLifecycleOwner.current) DisposableEffect(lifecycleOwner.value) { val lifecycle = lifecycleOwner.value.lifecycle val observer = LifecycleEventObserver { owner, event -> eventHandler.value(owner, event) } lifecycle.addObserver(observer) onDispose { lifecycle.removeObserver(observer) } } } It seems to work just fine. But there … Read more

How to show ellipsis (three dots) at the end of a Text line in Android Jetpack Compose?

Both BasicText and Text have overflow and maxLines arguments which can help you. Text(myText, maxLines = 1, overflow = TextOverflow.Ellipsis) Here’s a full single-line example: import androidx.compose.material.Text import androidx.compose.ui.text.style.TextOverflow @Composable fun EllipsisExample() { Box(modifier = Modifier.width(160.dp)) { Text( text = “Lorem ipsum dolor sit amet.”, maxLines = 1, overflow = TextOverflow.Ellipsis ) } } Of … Read more

background color on Button in Jetpack Compose

Use ButtonDefaults which is available in 1.0.0-alpha09 to alpha11 Button( onClick = {}, colors = ButtonDefaults.buttonColors(backgroundColor = Color.Yellow) ) { /**/ } OLD VERSION The backgroundColor for Button no longer work in 1.0.0-alpha7 Use the below instead Button( onClick = {}, colors = ButtonConstants.defaultButtonColors(backgroundColor = Color.Yellow) ) { /**/ }