How can I detect keyboard opening and closing in jetpack compose?

Update With the new WindowInsets API, it gets easier First, to return the correct values, you need to set: WindowCompat.setDecorFitsSystemWindows(window, false) Then to use Keyboard as a state: @Composable fun keyboardAsState(): State<Boolean> { val isImeVisible = WindowInsets.ime.getBottom(LocalDensity.current) > 0 return rememberUpdatedState(isImeVisible) } use example: val isKeyboardOpen by keyboardAsState() // true or false ps: I’ve tried … 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 get preview in composable functions that depend on a view model?

This is exactly one of the reasons why the view model is passed with a default value. In the preview, you can pass a test object: @Preview @Composable private fun HomeScreenPreview() { val viewModel = HomeViewModel() // setup viewModel as you need it to be in the preview HomeScreen(viewModel = viewModel, navigateToDetailsAction = {}, openCardDetailsAction … Read more

What is the equivalent of [NestedScrollView + RecyclerView] or [Nested RecyclerView (Recycler inside another recycler) in Jetpack compose

I think the best option, would be if the LazyVerticalGrid allows some sort of expand logic on each item, but looks like it’s not supported yet (beta-03). So I’m leaving here my solution using one single LazyColumn for the entire list and LazyRow for “My Books” section. LazyColumn( modifier = Modifier.fillMaxSize(), ) { // My … Read more

How to get exact size without recomposition?

If you are gonna use size of your Composable for drawing lines you can change your function to extension of DrawScope which returns size of your Composable. If that’s not the case check answer below. fun DrawScope.drawLine() { this.size } And call this function inside either of Modifier.drawBehind{}, Modifier.drawWithContent{} or Modifier.drawWithCache{}. Also you can pass … Read more

How to clear TextField focus when closing the keyboard and prevent two back presses needed to exit app in Jetpack Compose?

There’s a compose issue with focused text field prevents back button from dismissing the app when keyboard is hidden. It’s marked as fixed, but will be included in some future release, not in 1.0 But, as I understand, the fact that text field is not loosing focus after keyboard being dismissed, is intended behaviour on … Read more

Is there a way to increase a Composable size by chaining with another Modifier?

Changing Modifier size maybe possible with creating a Modifier such as SizeModifier. If anyone posts accurate answer that accomplishes task that way would be more than welcome. I solved this by first setting a requiredSize to have minimum dimensions for this Composable. @Composable fun TransformLayout( modifier: Modifier = Modifier, enabled: Boolean = true, handleRadius: Dp … Read more