Android TextView should auto scroll automatically

I need a TextView that scrolls vertically automatically. This 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 need to enter this line:

tView.setMovementMethod(new ScrollingMovementMethod())

      

to 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 will not scroll automatically if new input (text) is inserted into the view. I know there are other questions regarding this issue. But I've tried most of them.

How can I solve this problem?

+3


source to share


3 answers


Try to set textView to android:gravity="bottom"

and use textView.append("\n"+"New Input Text.");

for new input text;



+5


source


You're almost there. Just add a few more things to achieve this,

First of all add these properties to your TextView

android:maxLines = "AN_INTEGER"
android:scrollbars = "vertical"
android:gravity="bottom"

      



Second, as usual,

tView.setMovementMethod(new ScrollingMovementMethod());

      

This should fix your problem.

+2


source


I would suggest wrap yours TextView

inScrollView

<ScrollView
    android:id="@+id/sView"
    android:layout_below="@+id/iView"
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:layout_centerHorizontal="true" >

  <TextView
      android:id="@+id/tView"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:maxLines="500"
      android:scrollbars="vertical"
      android:gravity="bottom" />

</ScrollView>

      

You can add new text by doing

public void appendAndScrollDown(String someText) {
    tView.append("\n" + someText);
    sView.fullScroll(View.FOCUS_DOWN);
}

      

0


source







All Articles