Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Column doesn't scroll when keyboard opens

I want to develop my next Android App with Jetpack Compose. I know it is new and in Alpha state.

With this code, I want to implement a login view. It works so far until the keyboard opens :-(

enter image description here

private val items = listOf(Tab.Home)

private sealed class Tab(@StringRes val resourceId: Int) {
    object Home : Tab(R.string.home)
}

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MyApplicationTheme {
                Surface(color = MaterialTheme.colors.background) {
                    Scaffold(
                        topBar = {
                            TopAppBar(
                                title = {
                                    Text(text = "Home")
                                }
                            )
                        },
                        bottomBar = {
                            BottomNavigation {
                                items.forEach { screen ->
                                    BottomNavigationItem(
                                        icon = { Icon(Icons.Filled.Favorite, null) },
                                        label = { Text(stringResource(screen.resourceId)) },
                                        selected = true,
                                        onClick = { /* ... */
                                        }
                                    )
                                }
                            }
                        }
                    ) {
                        Column(
                            Modifier.verticalScroll(rememberScrollState()).padding(16.dp)
                        ) {
                            Text(text = "Lorem ipsum ...")
                            Spacer(modifier = Modifier.height(32.dp))
                            Text(text = "Lorem ipsum ...")
                            Spacer(modifier = Modifier.height(32.dp))
                            Text(text = "Lorem ipsum ...")
                            Spacer(modifier = Modifier.height(32.dp))
                            TextField(
                                value = "",
                                label = { Text("Name", color = MaterialTheme.colors.onPrimary.copy(alpha = 0.5f)) },
                                onValueChange = { /*TODO*/ },
                                modifier = Modifier.fillMaxWidth()
                            )
                            Spacer(modifier = Modifier.height(16.dp))
                            TextField(
                                value = "",
                                label = { Text("Password", color = MaterialTheme.colors.onPrimary.copy(alpha = 0.5f)) },
                                onValueChange = { /*TODO*/ },
                                modifier = Modifier.fillMaxWidth()
                            )
                            Spacer(modifier = Modifier.height(16.dp))
                            Button(
                                content = { Text("Login") },
                                onClick = { /*TODO*/ },
                            )
                        }
                    }
                }
            }
        }
    }
}

I use this vertical scrollable column, but it doesn't scroll if parts of the column are behind the keyboard.

To fix it, I added android:windowSoftInputMode="adjustResize|stateHidden" in the manifest.

Now I can scroll the column but on top of the keyboard is also the bottomBar. And the bottomBar covers the login button at the bottom of the column. The bottomBar also covers the button if the content is enough that it also scrolls if the keyboard is closed.

enter image description here

Now I have three questions:

  1. How to make the column scrollable when the keyboard opens but without the bottomBar on top of the keyboard?

  2. How to implement an auto-scroll to the focused text field?

  3. How to prevent the bottomBar from covering the button?

like image 545
Ralph Bergmann Avatar asked Jan 30 '21 19:01

Ralph Bergmann


People also ask

Why can’t I scroll in Excel?

Frozen panes and zoomed in might be the reason that you can’t scroll in Excel. Are there frozen panes? E.g. the first row or column?

How do I Turn Off Scroll Lock on Windows 10?

On Windows 10, scroll lock can be toggled on or off via the “ScrLK” button on your keyboard. However, in some cases this may not work, due to the button not existing on some keyboard models, or laptops. To get around this we can utilise Windows 10’s on-screen keyboard. To open the onscreen keyboard press the “Win” key.

How to scroll the input fields on top of the column?

Remove SingleChildScrollView from second column widget and add in top of column , its working fine. Please try it once, and share your design also . You can just scroll your input fields up by using resizeToAvoidBottomInset: false property in Scaffold widget instead of screen scrolling up.

Is it bad to scroll down in listview?

Unfortunately, it can introduce a new bug if the scrolling pushes the text fields upwards and they end up getting hidden by the top. If you only have a few text fields and they're at the bottom of your ListView, this might be just fine, but it would still be a problem if you have a lot of text fields.


1 Answers

This is a known issue (at least with LazyColumn in my experience). Please feel free to star that bug.

In that thread there's a hacky solution that involves RelocationRequester (which relocates a composable so it's visible) and onfocusChanged which tells you when to do the relocation.

like image 192
mmm111mmm Avatar answered Oct 22 '22 03:10

mmm111mmm