Java – Android TextView should automatically scroll vertically

Android TextView should automatically scroll vertically… here is a solution to the problem.

Android TextView should automatically scroll vertically

I need a TextView that scrolls vertically automatically. Here is my layout code :

activity_info.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<ImageView
        android:id="@+id/iView"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_centerHorizontal="true"
        android:contentDescription="@string/info"
        android:src="@mipmap/infoImage" />

<TextView
        android:id="@+id/tView"
        android:layout_below="@+id/iView"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_centerHorizontal="true"
        android:maxLines="500"
        android:scrollbars="vertical" />

<RelativeLayout
        android:layout_below="@+id/tView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:gravity="center">

<Button
            android:id="@+id/buttonYes"
            android:layout_width="100dp"
            android:layout_height="50dp"
            android:text="YES" />

<Button
            android:id="@+id/buttonNo"
            android:layout_toEndOf="@id/buttonYes"
            android:layout_width="100dp"
            android:layout_height="50dp"            
            android:text="NO" />

</RelativeLayout>

</RelativeLayout>

I

read that I have to type this line:

tView.setMovementMethod(new ScrollingMovementMethod())

Enter my onCreate method in my activity:

InfoActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_info);

TextView tView = (TextView) findViewById(R.id.tView);
    tView.setMovementMethod(new ScrollingMovementMethod());
}

If I do this, the TextView doesn’t scroll automatically if some new input (text) is inserted into the View. I know there are some other questions about this issue. But I’ve tried most of them.

How do I fix this?

Solution

Try setting the textView to android:gravity="bottom"
and use textView.append("\n"+"New Input Text."); as new input text;

Related Problems and Solutions