how to avoid jetpack compose content going up when keyboard opens
Asked Answered
B

2

6

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)
}
Bullheaded answered 22/10, 2022 at 10:27 Comment(0)
O
12

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()
    }
}
Organic answered 22/10, 2022 at 11:2 Comment(2)
I don´t know how, but it does exactly what I was looking for, it push up the text input and the add button, but it leave in position the list of items, I´d like to understand how it knows which composable should be pushed and which not, probably it was just luck. thanks!Rigney
@OlegarioLópez Check the doc.It explains how the main window of the activity interacts with the window containing the on-screen soft keyboard. Using adjustResize the activity's main window is always resized to make room for the soft keyboard on screen. Then checking your code and sceen I supposed that you are using a weight modifier to the List or a constraint to have to TextField to the bottom of the screen.Organic
M
1

This was exactly my problem with an extra twist that it only became an issue after the activity returned from pause.

A brand new state had no issue, but when resuming suddenly my entire mainactivity was being pushed up with the keyboard. SUPER weird behavior.

I actually set windowSoftInputMode to "adjustPan" abd it works as expected now, which is how it worked as a fresh activity.

Mosely answered 26/1, 2023 at 1:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.