How to show animate an item view in LazyColumn on Jetpack Compose Android

Update for Compose 1.1.0: Animating item position changes are now possible but deletion/insertion animations are still not possible and need to be implemented by the method I’ve explained. To animate item position changes you just have to provide the item keys in your list by key = { it.id } and use Modifier.animateItemPlacement(). Original Answer … Read more

How to disable and enable scrolling in LazyColumn/LazyRow in Jetpack Compose?

Since 1.2.0-alpha01 userScrollEnabled was added to LazyColumn, LazyRow, and LazyVerticalGrid Answer for 1.1.0 and earlier versions: @Ryan’s solution will also disable programmatically-called scrolling. Here’s a solution proposed by a maintainer in this feature request. It’ll disable scrolling, allow programmatic scrolling as well as children view touches. private val VerticalScrollConsumer = object : NestedScrollConnection { override … Read more

How to achieve a staggered grid layout using Jetpack compose?

One of Google’s Compose sample Owl shows how to do a staggered grid layout. This is the code snippet that is used to compose this: @Composable fun StaggeredVerticalGrid( modifier: Modifier = Modifier, maxColumnWidth: Dp, children: @Composable () -> Unit ) { Layout( children = children, modifier = modifier ) { measurables, constraints -> check(constraints.hasBoundedWidth) { … Read more

How to set the Scaffold Drawer Width in JetpackCompose?

You can modify the shape (including width and height) using drawerShape parameter in Scaffold method. So for instance Scaffold( scaffoldState = scaffoldState, drawerContent = { Text(“Drawer content”) }, drawerShape = customShape(), content = {inner padding -> /* Body*/} ) Then your customShape function fun customShape() = object : Shape { override fun createOutline( size: Size, … Read more

Error when adding buildFeatures in build.gradle file

I caught this error when I tried to add Jetpack to my existing app. I followed Suraj’s answer, even used the newest Kotlin gradle plugin, and couldn’t exactly figure out what was wrong. I also followed the official setup guide and it didn’t work. Everything seemed okay, but nothing helped. Installing Android Studio 4.0 canary … Read more

Jetpack Compose LazyColumn – How to update values of each Item seperately?

Create a mutableStateListOf(…) (or mutableStateOf(listOf(…)) object if the former does not support your data type) in your ViewModel. Now, access this state from the composable you wish to read it from, i.e., your LazyColumn. Inside your ViewModel, you can set the values however you wish, and they will be updated in the Composable as and … Read more

Jetpack Compose lazy column all items recomposes when a single item update

MutableState works using structural equality which check if you update state.value with new instance. You are creating a new instance of your list on each time you select a new item. You can use SnapshotStateList which triggers recomposition when you add, delete or update existing item with new instance. SnapshotStateList is a List which gets … Read more

AppCompatActivity instead of ComponentActivity in Jetpack compose

You can use the AppCompatActivity since it extends FragmentActivity which extends ComponentActivity. class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { val activity = LocalContext.current as AppCompatActivity Button(onClick={ showDatePicker(activity)}){ Text(“Picker”) } } } } fun showDatePicker(activity: AppCompatActivity){ val picker = MaterialDatePicker.Builder.datePicker().build() activity?.let { picker.show(it.supportFragmentManager, picker.toString()) picker.addOnPositiveButtonClickListener { } } } Note: … Read more