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

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

Proguard causing runtime exception with Android Navigation Component

I know that Proguard and R8 should be keeping all the children of library classes but in this case, the fragment class seems to be missing. This keep rule solved my issue but technically we should not need this rule at all! -keep class * extends android.support.v4.app.Fragment{} If you are using AndroidX, then use this … Read more

Is it possible to set startDestination conditionally using Android Navigation Architecture Component(Android Jetpack)?

Finally, I got a solution to my query… Put below code in onCreate() method of Activity. Kotlin code val navHostFragment = (supportFragmentManager.findFragmentById(R.id.home_nav_fragment) as NavHostFragment) val inflater = navHostFragment.navController.navInflater val graph = inflater.inflate(R.navigation.nav_main) //graph.addArgument(“argument”, NavArgument) graph.setStartDestination(R.id.fragment1) //or //graph.setStartDestination(R.id.fragment2) navHostFragment.navController.graph = graph Java code NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.home_nav_fragment); // Hostfragment NavInflater inflater = navHostFragment.getNavController().getNavInflater(); NavGraph graph … Read more

How to change start destination of a navigation graph programmatically?

UPDATE: When you have nav graph like this: <fragment android:id=”@+id/firstFragment” android:name=”com.appname.package.FirstFragment” > <action android:id=”@+id/action_firstFragment_to_secondFragment” app:destination=”@id/secondFragment” /> </fragment> <fragment android:id=”@+id/secondFragment” android:name=”com.appname.package.SecondFragment”/> And you want to navigate to the second fragment and make it root of your graph, specify the next NavOptions: NavOptions navOptions = new NavOptions.Builder() .setPopUpTo(R.id.firstFragment, true) .build(); And use them for the navigation: Navigation.findNavController(view).navigate(R.id.action_firstFragment_to_secondFragment, … Read more

Android Jetpack Compose width / height / size modifier vs requiredWidth / requiredHeight / requiredSize

The difference is that plain modifiers like width() take into account layout Constraints while required modifiers like requiredWidth() ignore them. You can think of constrains as min/max width/height of a measured element. To better understand how layout modifiers and constraints work take a look at this post. Let’s see an example of width modifier, height … Read more

Jetpack Compose collapsing toolbar

I found a solution created by Samir Basnet (from Kotlin Slack Channel) which was useful for me, I hope it helps someone else… @Composable fun CollapsingEffectScreen() { val items = (1..100).map { “Item $it” } val lazyListState = rememberLazyListState() var scrolledY = 0f var previousOffset = 0 LazyColumn( Modifier.fillMaxSize(), lazyListState, ) { item { Image( … Read more