View doesn't scroll all the way up when clicking EditText

Viewed 143

When I tap 1 of the 3 edittexts, my keyboard appears and the view scrolls up a little bit. But my layout should move all the way up. My xml file:

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ScrollView
        android:id="@+id/scrollView"
        android:fillViewport="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/toolbar"
        app:layout_constraintVertical_bias="0.01999998">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="126dp">

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/padding_default"
                android:padding="@dimen/padding_small"
                android:paddingHorizontal="@dimen/padding_default"
                app:layout_constraintTop_toTopOf="parent"
                tools:layout_editor_absoluteX="16dp" />

            <EditText
                android:id="@+id/instructionsEditText"
                android:layout_width="match_parent"
                android:layout_height="600dp"
                android:layout_margin="@dimen/padding_default"
                android:gravity="top|start"
                android:padding="@dimen/padding_small"
                android:paddingHorizontal="@dimen/padding_default"
                app:layout_constraintTop_toBottomOf="@+id/amountPersonsEditText" />

            <EditText
                android:id="@+id/notesEditText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/padding_default"
                android:padding="@dimen/padding_small"
                android:paddingHorizontal="@dimen/padding_default"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/instructionsEditText"
                tools:layout_editor_absoluteX="16dp" />
        </androidx.constraintlayout.widget.ConstraintLayout>
    </ScrollView>

    <android.widget.Button
        android:id="@+id/saveInstructionsButton"
        style="@style/buttonStyle"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/padding_default"
        android:gravity="center"
        android:imeOptions="actionDone"
        android:text="@string/instructions_button_text"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

My androidmanifest:

    <activity
        android:name=".ui.instructions.InstructionsActivity"
        android:windowSoftInputMode="adjustResize"
        android:screenOrientation="portrait" />

I've also tried this:

fun ScrollView.moveToTop() {
    this.isFocusableInTouchMode = true
    this.fullScroll(View.FOCUS_DOWN)
    this.smoothScrollTo(0, bottom)
}

but the button is outside the scrollview and the button bottom should be at the top of the keyboard.

Start:

enter image description here

Actual:

enter image description here

Expected:

enter image description here

5 Answers

Add below code in your scrollview:

    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintBottom_toTopOf="@+id/saveInstructionsButton"

And this code in bottom button:

    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"

Example code:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ScrollView
        android:id="@+id/scrollView"
        android:fillViewport="true"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/saveInstructionsButton">
        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <EditText
                android:id="@+id/amountPersonsEditText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/padding_default"
                android:padding="@dimen/padding_small"
                android:paddingHorizontal="@dimen/padding_default"
                app:layout_constraintTop_toTopOf="parent"
                tools:layout_editor_absoluteX="16dp"
                android:focusableInTouchMode="true" />

            <EditText
                android:id="@+id/instructionsEditText"
                android:layout_width="match_parent"
                android:layout_height="600dp"
                android:layout_margin="@dimen/padding_default"
                android:gravity="top|start"
                android:padding="@dimen/padding_small"
                android:paddingHorizontal="@dimen/padding_default"
                android:focusableInTouchMode="true"
                app:layout_constraintTop_toBottomOf="@+id/amountPersonsEditText" />

            <EditText
                android:id="@+id/notesEditText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/padding_default"
                android:padding="@dimen/padding_small"
                android:paddingHorizontal="@dimen/padding_default"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/instructionsEditText"
                tools:layout_editor_absoluteX="16dp"
                android:focusableInTouchMode="true"/>
        </androidx.constraintlayout.widget.ConstraintLayout>
    </ScrollView>
    <android.widget.Button
        android:id="@+id/saveInstructionsButton"
        style="@style/buttonStyle"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:imeOptions="actionDone"
        android:text="@string/instructions_button_text"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>

Manifest file code:

<activity
    android:name=".ui.instructions.InstructionsActivity"
    android:windowSoftInputMode="adjustResize"
    android:screenOrientation="portrait" />

Output for above code is:

enter image description here

Your xml is OK, Try to modify your activity declaration in manifest like below,

<activity
    android:name=".ui.instructions.InstructionsActivity"
    android:configChanges="orientation|keyboardHidden|screenSize"
    android:screenOrientation="portrait" />

Replace this line :

android:windowSoftInputMode="adjustResize"

with :

android:windowSoftInputMode="adjustPan"

It will move your edittext above the keyboard when you type.

You must use stateAlwaysVisibe for windowSofInputMode as follows:

<activity
    android:name=".ui.instructions.InstructionsActivity"
    android:windowSoftInputMode="stateAlwaysVisible"
    android:screenOrientation="portrait" />
Related