Auto scroll down on textviews

ron picture ron · Apr 26, 2012 · Viewed 9k times · Source

I have 3 components on my application, 1 textview(inputType:textMultiline, scrollbar:vertical, gravity:bottom|right) at the top, 1 editview at the middle and 1 button at the bottom. When I type something on my editview and I click the button, the text written on the edit view displays on the textview. Every time i hit ok, the text displays the the bottom and what is displayed is the first three inputs. I have to scroll down the textview in able for me to see my last input.

Now, I want my users to see their last input on their textview. I want to know if there is such code for auto scrolldown for textviews everytime I input a new text on it. I am new on developing android apps. Thanks!

Answer

Bharat Sharma picture Bharat Sharma · Apr 26, 2012

When you set text to textview just set foucus to it. like

tv.setFocusable(true);

It will automatically focus your view whenever you change your string on textview.

If you are adding text to your text view again and again then you can try this

    int scroll_amount = tv.getBottom();
    tv.scrollTo(0, scroll_amount);

Hope it will work not sure..

Try this also

    int scroll_amount = (int) (tv.getLineCount() * tv.getLineHeight()) - (tv.getBottom() - tv.getTop());
    tv.scrollTo(0, scroll_amount);