Search code examples
androidkotlinandroid-jetpack-composeviewmodelandroid-softkeyboard

how to avoid jetpack compose content going up when keyboard opens


List of items should stay in their position

As shown above, the list of items, the text input field and the add button go up when the user open the keyboard, I want the list of items to stay in position while the text input field and the add buton go up as it does.

code:

Activity:

class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContent {
        OlegarioLopezTheme {
            Surface(
                modifier = Modifier.fillMaxSize(),
                color = MaterialTheme.colors.background
            ) { Navigation() }
        }
    }
}

The Navigation() func just call the Composable

Composable:

    @Composable
fun ListScreen(
    viewModel: MainScreenViewModel,
    navController: NavController
) {
    LazyColumn{...}
MainTextField(viewModel)
    AddButton(viewModel)
}

Solution

  • Ensure that the activity's windowSoftInputMode is set to adjustResize:

    <activity
          android:name=".MyActivity"
          android:windowSoftInputMode="adjustResize">
    </activity>
    

    In this way the activity's main window is always resized to make room for the soft keyboard on screen.

    Then just use a layout as:

    Column() {
        LazyColumn(Modifier.weight(1f)) {
          //..
        }
        Row(){
            TextField()
            Button()
        }
    }