Search code examples
androidxmltextviewkeyboard

Android Studio - EditText input moves TextView


I have a Text View below an EditText.
When I enter data into the EditText, the TextView moves up, so that it is still visible.
I mean I enter data, the keyboard comes up and the TextView moves also up.
But I don't want that, I want the TextView to stay where it is.
I am using a ConstraintLayout and I can't set the constraints tighter, because the TextView will be filled with data if I hit a button.

    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName"
        android:layout_marginTop="50dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        android:hint="@string/hint1"
        android:importantForAutofill="no"/>

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:layout_marginBottom="24dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constrainedHeight="true">

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/emptyString" />

    </ScrollView>

Solution

  • I found the solution, it was adding this line in the starting activity tag: android:windowSoftInputMode="adjustPan"

    Like this:

    <activity android:name=".MainActivity"
                android:windowSoftInputMode="adjustPan">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
    

    Now when the keyboard appears the TextView is not moving anymore.
    It is overlaid by the keyboard, but I like to have it that way rather than having moving views.