0

The ScrollView in Android is really confusing. I'm working on an app that adds a fragment of text to a TextView inside the ScrollView, and I want it to scroll to the bottom of the ScrollView.

I've tried these ways:

Scroll1.fullScroll(View.FOCUS_DOWN);

Scroll1.scrollTo(0, sv.getBottom());

Scroll1.scrollTo(0, Scroll1.getY() + Scroll1.getHeight());

NOTE: 1. Scroll1 is the ScrollView I used. 2. The first two methods can't scroll to the bottom. The last just doesn't work.

How can I achieve this? Please help. Thanks.

3 Answers 3

1

try this it may helps if I know your problem: paste it in scroll view in xml

android:fillViewport="true"

OR

in java activity

scrollView.setFillViewport(true);
Sign up to request clarification or add additional context in comments.

Comments

0

use the following code once you want to update the scrollview:

scrollView.post(new Runnable() { 
     public void run() {
         scrollView.fullScroll(View.FOCUS_DOWN);
     }
 });

Comments

0

Your scrollView may not scroll to bottom in onCreate until you give some time to the view to inflate fully.

So to make it scroll...

mScrollView.postDelayed(new Runnable() {
            @Override
            public void run() {
                View lastChild = mScrollView.getChildAt(mScrollView.getChildCount() - 1);
                int bottom = lastChild.getBottom() + mScrollView.getPaddingBottom();
                int sy = mScrollView.getScrollY();
                int sh = mScrollView.getHeight();
                int delta = bottom - (sy + sh);

                mScrollView.smoothScrollBy(0, delta);
            }
        },1000);

You can also use scrollView.fullScroll(View.FOCUS_DOWN); in run()

I combinde this from a lot of answers. It finally helped me.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.