Android: Adding items to TextView and scrolling them into view

I’m starting this series of articles describing some of the issues I encountered along the way for which I could not find too much documentation online, or the documentation I could find was vague.

Today’s issue is having a multiline TextView and programmatically adding new lines to it. Consider the TextView in the gist below. It holds about 10 lines, and any subsequent line gets hidden.

I wanted two things:

  1. To be able to scroll the TextView
  2. Adding a new line to the TextView to automatically scroll this line into view.

For the first point, the solution is this:

Add this lines in the OnCreate method.

For the second point:

The addLine function adds a new line of text to the TextView, and programmatically scrolls it into view.

A full implementation can be found here, and the code for the current article in in Activity_1. The other activities are for other, future articles.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.