How to call Kotlin coroutine in composable function callbacks?

Create a coroutines scope, tied to the lifecycle of your composable, and use that scope to call your suspending function suspend fun getLocation(): Location? { /* … */ } @Composable fun F() { // Returns a scope that’s cancelled when F is removed from composition val coroutineScope = rememberCoroutineScope() val (location, setLocation) = remember { … Read more

How to add dividers between items in a LazyColumn Jetpack Compose?

Currently there is no built–in way to add dividers. However, you can just add a Divider in the LazyListScope. Something like: LazyColumn( verticalArrangement = Arrangement.spacedBy(12.dp), ) { items(itemsList){ Text(“Item at $it”) Divider(color = Color.Black) } } If you do not want the last item to be followed by a Divider, you can add dividers to … Read more

Exposed drop-down menu for jetpack compose

The M2 (starting from the version 1.1.0-alpha06) and M3 have the implementation of ExposedDropdownMenu based on ExposedDropdownMenuBox with TextField and DropdownMenu inside. Something like: val options = listOf(“Option 1”, “Option 2”, “Option 3”, “Option 4”, “Option 5”) var expanded by remember { mutableStateOf(false) } var selectedOptionText by remember { mutableStateOf(options[0]) } ExposedDropdownMenuBox( expanded = expanded, … Read more

Jetpack Compose: Launch ActivityResultContract request from Composable function

As of androidx.activity:activity-compose:1.3.0-alpha06, the registerForActivityResult() API has been renamed to rememberLauncherForActivityResult() to better indicate the returned ActivityResultLauncher is a managed object that is remembered on your behalf. val result = remember { mutableStateOf<Bitmap?>(null) } val launcher = rememberLauncherForActivityResult(ActivityResultContracts.TakePicturePreview()) { result.value = it } Button(onClick = { launcher.launch() }) { Text(text = “Take a picture”) } … Read more

Compose-Navigation: Remove previous composable from stack before navigating

In Jetpack Compose 1.0.0 to navigate and remove the previous Composable from the back stack You can use: navController.navigate(Screens.Login.name) { popUpTo(Screens.Splash.name) { inclusive = true } } The above code will navigate from the Splash screen to Login and will pop everything up, including the Splash screen. Navigate to a composable – docs

How to draw a circular image in Android Jetpack Compose?

There’s a clip modifier which can be applied to any composable as well as the Image, just pass a CircleShape into it: Image( painter = painterResource(R.drawable.sample_avatar), contentDescription = “avatar”, contentScale = ContentScale.Crop, // crop the image if it’s not a square modifier = Modifier .size(64.dp) .clip(CircleShape) // clip to the circle shape .border(2.dp, Color.Gray, CircleShape) … Read more

Jetpack Compose – Centering Text

To define how to align the text horizontally you can apply textAlign = TextAlign.Center in the Text: Column(modifier = Modifier .padding(30.dp) .fillMaxWidth() .wrapContentSize(Alignment.Center) .clickable(onClick = { } ) /*question = “3 Bananas required”*/ .clip(shape = RoundedCornerShape(16.dp)), ) { Box(modifier = Modifier .preferredSize(350.dp) .border(width = 4.dp, color = Gray, shape = RoundedCornerShape(16.dp)), alignment = Alignment.Center ) … Read more

ComponentActivity vs AppCompactActivity 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.