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

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

How do I define default animations for Navigation Actions?

R.anim has the default animations defined (as final): nav_default_enter_anim nav_default_exit_anim nav_default_pop_enter_anim nav_default_pop_exit_anim in order to change this behavior, you would have to use custom NavOptions, because this is where those animation are being assigned to a NavAction. one can assign these with the NavOptions.Builder: protected NavOptions getNavOptions() { NavOptions navOptions = new NavOptions.Builder() .setEnterAnim(R.anim.default_enter_anim) .setExitAnim(R.anim.default_exit_anim) … Read more

Android Work Manager vs Services?

WorkManager comes with following features: Provides tasks which can survive process death It can waken up the app and app’s process to do the work thereby guarantees that works will be executed. Allows observation of work status and the ability to create complex chains of work Allows work chaining which allows to segregate big chunk … Read more

When should I use Android Jetpack Compose Surface composable?

Surface composable makes the code easier as well as explicitly indicates that the code uses a material surface. Let’s see an example: Surface( color = MaterialTheme.colors.primarySurface, border = BorderStroke(1.dp, MaterialTheme.colors.secondary), shape = RoundedCornerShape(8.dp), elevation = 8.dp ) { Text( text = “example”, modifier = Modifier.padding(8.dp) ) } and the result: The same result can be … Read more

how to instantiate ViewModel In AndroidX?

Updated answer: Things changed a little bit, as the previously needed dependency – ViewModelProviders – got deprecated (see the old answer for details). You can now use the ViewModelProvider constructor directly. So, in this case, the answer would be: private val viewModel = ViewModelProvider(this).get(SheduleViewModel::class.java) Note that, however, if you include the androidx.activity:activity-ktx:$Version dependency (a few … Read more

How to upgrade an Android project to Java 11

From Android Studio Artic Fox 2020.3.1 Preferences (Settings) -> Build, Execution, Deployment -> Build Tools -> Gradle -> Gradle JDK -> Select JDK 11 or download JDK Before Artic Fox 2020.3.1 Version I assume you have Java 11 or later installed. Following steps: File -> Project Structure -> SDK Location -> Change JDK Location to … Read more

Bottom Nav Bar overlaps screen content in Jetpack Compose

As per the API definition for Scaffold, your inner content (the trailing lambda you have your BottomNavScreensController in), is given a PaddingValues object that gives you the right amount of padding for your top app bar, bottom bar, etc. Right now, you’re not referencing that padding at all and hence, your content is not padded … Read more