0

I'm using Jetpack Compose and trying to find a way to detect if the keyboard is open.

I've tried to use the below code, but I get an error stating Unresolved reference: ime. When I click on the recommended imports (the 2 shown below), this error still remains.

import android.view.WindowInsets import android.view.WindowInsets.Type.ime @Composable fun signInView() { val isVisible = WindowInsets.ime.getBottom(LocalDensity.current) > 0 } 

How can I resolve this?

2
  • How about androidx.compose.foundation.layout.WindowInsets? Commented Jan 8, 2023 at 19:02
  • I get Unresolved reference: WindowInsets when I add this import statement Commented Jan 8, 2023 at 21:12

1 Answer 1

1

Add the dependencies for the artifacts you need in the build.gradle file for your app or module:

dependencies { implementation "androidx.compose.foundation:foundation:1.3.1" } android { buildFeatures { compose true } composeOptions { kotlinCompilerExtensionVersion = "1.3.2" } kotlinOptions { jvmTarget = "1.8" } } 

Example:

 @Composable fun signInView() { var isVisible by remember { mutableStateOf(false) } val ime = androidx.compose.foundation.layout.WindowInsets.ime val navbar = androidx.compose.foundation.layout.WindowInsets.navigationBars var keyboardHeightDp by remember { mutableStateOf(0.dp) } val localDensity = LocalDensity.current LaunchedEffect(localDensity.density) { snapshotFlow { ime.getBottom(localDensity) - navbar.getBottom(localDensity) }.collect { val currentKeyboardHeightDp = (it / localDensity.density).dp keyboardHeightDp = maxOf(currentKeyboardHeightDp, keyboardHeightDp) isVisible = currentKeyboardHeightDp == keyboardHeightDp } } } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.