Question

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!

Was it helpful?

Solution 3

I already got the answer for this! Thank you for giving me some idea. I might be able to use them in the future. @Bharat Sharma, you almost got the answer! Thanks!

public void onClick(View v) {
            // TODO Auto-generated method stub
            log.setText(log.getText() + "\n" +input.getText());

            if(log.getLineCount() > 5){
                scroll_amount = scroll_amount + log.getLineHeight();
                log.scrollTo(0, scroll_amount);
            }
        }

I called the variable scroll amount outside the onCreate(). Thanks again!

OTHER TIPS

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);

you can use scrollView.scrollTo(x,y) to auto scroll to position that you want.

/*edit*/

create custom class for scrollview

package com.android.mypackage
public class myScrollView extends ScrollView{
    private int maxY = 0;
    @Override
    protected void onScrollChanged(int x, int y, int oldx, int oldy) {
        super.onScrollChanged(x, y, oldx, oldy);
       if(y>maxY)
        maxY=y;
    }
    public void moveToEnd(){
        this.scrollTo(0, maxY);
    }
}

using this custom class in layout xml as below:

...
<com.android.mypackage.myScrollView
    android:id="@+id/my_scrollview"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <TextView .../>
</com.android.mypackage.myScrollView>
.....

when you press Ok button just call function myscrollViewObj.movetoEnd(); it's just a draft code, still not tested.

This is it.

public void onScrollDownClicked(View view) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        mTextView.scrollTo(0, mTextView.getLayout().getHeight() - mTextView.getHeight());
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top