Yannik Pieper Yannik Pieper - 1 month ago 8
Android Question

EditText moves TextView out of the screen when they keyboard is opening

I have an EditText on the bottom of my layout und a few TextViews on the top. When I click on the EditText it moves from the bottom above the keyboard which is good. But the TextViews also move up and out of the screen so you can't see them until you close the keyboard again. How can I avoid this but still have my EditText above the keyboard? I'm using a RelativeLayout.
Thanks!

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.ncss.tyfby.Feeling"
android:background="#1baa84">

<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">

<TextView
android:text="I am"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/IamTV"
android:layout_weight="0.10"
android:textSize="40sp"
android:textStyle="normal|bold"
android:gravity="bottom" />

<EditText
android:layout_width="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/iamAdjective"
android:background="@android:drawable/editbox_background_normal"
android:hint="powerful"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginLeft="10dp" />

<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/send"
android:layout_weight="0.10"
android:src="@android:drawable/ic_menu_send"
android:background="@drawable/transparent"
android:onClick="send"
android:layout_marginLeft="5dp" />
</LinearLayout>


<TextView
android:text="I am"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/iam1"
android:textSize="18sp" />

<TextView
android:text="I am"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/iam2"
android:textSize="18sp"
android:layout_below="@+id/iam1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp" />

<TextView
android:text="I am"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/iam4"
android:textSize="18sp"
android:layout_below="@+id/iam3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp" />

<TextView
android:text="I am"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/iam5"
android:textSize="18sp"
android:layout_below="@+id/iam4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp" />

<TextView
android:text="I am"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/iam3"
android:textSize="18sp"
android:layout_below="@+id/iam2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp" />



Screen whithout keyboardScreen with Keyboard (TextView moved out of the screen)

Answer

You may explore windowSoftInputMode that you can configure for your Activity in the AndroidManifest.xml file: activity | Android Developers.

Perhaps windowSoftInputMode="adjustResize" might be useful for you.

In addition to that you will need to change your layout XML to something like:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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_height="match_parent"
    android:layout_width="match_parent"
    tools:context="com.ncss.tyfby.Feeling"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    android:background="#1baa84">

    <ScrollView
        android:layout_height="match_parent"
        android:layout_width="wrap_content"
        android:layout_weight="1">

        <LinearLayout
            android:layout_height="match_parent"
            android:layout_width="wrap_content"
            android:orientation="vertical">

            <TextView
                android:text="I am"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/iam1"
                android:textSize="18sp" />

            <TextView
                android:text="I am"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/iam2"
                android:textSize="18sp"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_marginTop="10dp" />

            <TextView
                android:text="I am"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/iam4"
                android:textSize="18sp"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_marginTop="10dp" />

            <TextView
                android:text="I am"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/iam5"
                android:textSize="18sp"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_marginTop="10dp" />

            <TextView
                android:text="I am"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/iam3"
                android:textSize="18sp"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_marginTop="10dp" />

        </LinearLayout>

    </ScrollView>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">

        <TextView
            android:text="I am"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/IamTV"
            android:layout_weight="0.10"
            android:textSize="40sp"
            android:textStyle="normal|bold"
            android:gravity="bottom" />

        <EditText
            android:layout_width="wrap_content"
            android:inputType="textPersonName"
            android:ems="10"
            android:id="@+id/iamAdjective"
            android:background="@android:drawable/editbox_background_normal"
            android:hint="powerful"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_marginLeft="10dp" />

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/send"
            android:layout_weight="0.10"
            android:src="@android:drawable/ic_menu_send"
            android:background="@drawable/transparent"
            android:onClick="send"
            android:layout_marginLeft="5dp" />

    </LinearLayout>

</LinearLayout>
Comments