Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make a TextView automatically scroll as I add more lines of text?

So, I have a TextView like so:

<TextView     android:layout_width="fill_parent"     android:layout_height="140.7dp"     android:id="@+id/terminalOutput"     android:layout_marginBottom="0.0dp"     android:scrollbars="vertical"     android:scrollbarAlwaysDrawVerticalTrack="true"     android:maxLines="8" /> 

I use it as a sort of running log, displayed to the user so they can monitor progress of a task that takes about 3 minutes. However, once I go over 8 lines, the text goes off screen. This is unintuitive to the user because they have no way of knowing that it went off screen, other than to manually poll by trying scroll down.

How can I make it so that every time I add some text to this TextView I make it scroll down as low as it can go?

Also, this is in Xamarin Android, but I don't think it's relevant. It's easy to translate between it and Java

like image 729
Earlz Avatar asked Nov 07 '13 02:11

Earlz


People also ask

How do I enable scrolling in TextView?

To make TextView scrollable, you don't need to use ScrollView, just add android:scrollbars=”vertical” attribute in TextView and then add two line of code in your java activity file.

How do I add a newline to a TextView in android?

for the new line in TextView just add \n in middle of your text it works..


1 Answers

From your Code, two steps have to do:

Step 1

Although you code in Xamarin Android, but as in Java in the xxxActivity.java for terminalOutput invoke must be like this

TextView outputText = (TextView) findViewById(R.id.terminalOutput); outputText.setMovementMethod(new ScrollingMovementMethod()); 

Method setMovementMethod() by parameter ScrollingMovementMethod() is the gimmick.

Step 2

And in the layout as in Java-Android also in activity_xxx.xml for the TextView Declaration as above must have to add this

android:gravity="bottom" 

When you add new line into the outputText like this:

outputText.append("\n"+"New text line."); 

It will scroll to the last line automatically, and these all the magic for your need.

like image 167
3 revs, 2 users 96% Avatar answered Sep 23 '22 06:09

3 revs, 2 users 96%