Reputation:
I need a TextView which scrolls automatically vertically. 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 have to input this line:
tView.setMovementMethod(new ScrollingMovementMethod())
into 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 that, the TextView does not scroll automatically, if some new input (text) is inserted into the View. I know there are some other questions concerning the problem. But I've tried most of them.
How can I solve this problem?
Upvotes: 7
Views: 10260
Reputation: 11873
You are almost there. Just add a few more things to achieve this,
First of all add those properties in your TextView
android:maxLines = "AN_INTEGER"
android:scrollbars = "vertical"
android:gravity="bottom"
Secondly as usual use,
tView.setMovementMethod(new ScrollingMovementMethod());
This should solve your problem.
Upvotes: 3
Reputation: 12339
I would suggest wrapping your TextView
in a ScrollView
<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 append new text by doing
public void appendAndScrollDown(String someText) {
tView.append("\n" + someText);
sView.fullScroll(View.FOCUS_DOWN);
}
Upvotes: 1
Reputation: 844
Try to set the textView to android:gravity="bottom"
and use textView.append("\n"+"New Input Text.");
for new input text;
Upvotes: 7