DD225 DD225 - 5 months ago 14
Android Question

Android - Prevent TextView from shifting upwards when appended to

My XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_media_play" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textInput"
android:maxLines="5"
android:layout_gravity="center_horizontal|top"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:ems="15"
android:maxEms="15"
android:layout_marginTop="150dp"
/>

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="40sp"
android:id="@+id/mText"
android:gravity="center"
android:paddingStart="20dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:layout_gravity="center_vertical"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>

</android.support.design.widget.CoordinatorLayout>


The TextView appears below the EditText which is fine and the TextView is blank by default. In my code I am appending to the TextView over time, but after one line has filled up, the previous line shifts upwards closer to the EditText until they are overlapping.

Is there a way for the TextView to grow only downwards when text is appended to it, rather than shifting upwards?

To be more clear:

EditText


first line of BlankTextView


Goes to:

EditText

first line of TextView <- first line shifted up one place
second line of TextView

Answer

First of all, to be able to define the relative position of your EditText and TextView to each other, you should add them to the same RelativeLayout.

After that, you could use the android:layout_below attribute, to position your TextView below your EditText, to prevent them from overlapping.

Try the following:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_media_play" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textInput"
            android:maxLines="5"
            android:layout_gravity="center_horizontal|top"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:ems="15"
            android:maxEms="15"
            android:layout_marginTop="150dp" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/textInput"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textSize="40sp"
            android:id="@+id/mText"
            android:gravity="center"
            android:paddingStart="20dp"
            android:paddingLeft="20dp"
            android:paddingRight="20dp"
            android:layout_gravity="center_vertical"
            android:layout_centerVertical="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />
    </RelativeLayout>

</android.support.design.widget.CoordinatorLayout>
Comments