65

I've a problem I can't solve: inside a ScrollView I only have a LinearLayout. By a user action I'm programmatically adding 2 TextView on this LinearLayout, but by the default the scroll keeps on the top. Since I controll the user action, I should be easy to scroll to the bottom with something like:

ScrollView scroll = (ScrollView) this.findViewById(R.id.scroll);
scroll.scrollTo(0, scroll.getBottom());

But actually not. Because immediately after adding this two new elements getBottom() still returns the previous two. I tried to refresh the state invoking refreshDrawableState(), but I doesn't work.

Do you have any idea how could I get the actual bottom of a ScrollView after adding some elements?

3
  • Actually ScrollView may change or not change it's height(it is a container for LinearLayout). Try to use getBottom() of ScrollView's first(and single) child LinearLayout. Commented Nov 4, 2011 at 21:00
  • Both containers (ScrollView and LinearLayout) look that they have the same issue: the bottom position doesn't reflect the last added items :-/ Commented Nov 5, 2011 at 9:45
  • Post the code, where you're adding new elements. It is hard to say without your code. I think, it may be weights issue or something. Commented Nov 5, 2011 at 10:06

7 Answers 7

151

You need to use the message queue or else it won't work. Try this:

scrollView.post(new Runnable() {
    @Override
    public void run() {
        scrollView.fullScroll(ScrollView.FOCUS_DOWN);
    }
});

This is what worked for me.

Sign up to request clarification or add additional context in comments.

2 Comments

this also works for NestedScrollView, i tried. Thanks
this is the recommended for full scroll, scrollTo() methods does not scroll to full top.
86

This doesn't actually answer your question. But it's an alternative which pretty much does the same thing.

Instead of Scrolling to the bottom of the screen, change the focus to a view which is located at the bottom of the screen.

That is, Replace:

scroll.scrollTo(0, scroll.getBottom());

with:

Footer.requestFocus();

Make sure you specify that the view, say 'Footer' is focusable.

android:focusable="true"
android:focusableInTouchMode="true"

5 Comments

This focusable property is very nice to be used in this scenario... when having complex layout.. thanks
I am finding it difficult to get the view at the bottom of the screen. I have a ListView that contains TextViews. I've attempted listView.getItemAtPosition(elementCount -1) to get that text view, but instead it returns a String instead of a view. I suspect that I will need to make a custom arrayAdapter in this case.
when there's a button at the bottom then ripple effect would be shown in the button which makes it weired!
It works perfectly if you are doing showCaseView
If you call footer.requestFocus() several times, don't forget to remove focus! footer.clearFocus() or another_edit_text.requestFocus() and then footer.requestFocus().
21

this will be ok

scrollView.post(new Runnable() {
    @Override

    public void run() {

        scroll.scrollTo(0, scroll.getBottom());

    }
});

Comments

5

Have you tried scroll.fullScroll(View.FOCUS_DOWN)?

1 Comment

scroll.fullScroll(View.FOCUS_DOWN) was the first thing I tried, but it doesn't work.
2

Since, I am also using hide keyboard, below code with a delay of 200 milli second worked better for me. Note: I just replaced post() with postDelayed() from above examples:

    final ScrollView scrollView_main = (ScrollView) findViewById(R.id.scrollView_main);
    scrollView_main.postDelayed(new Runnable() {
        @Override
        public void run() {
            scrollView_main.fullScroll(View.FOCUS_DOWN);
        }
    }, 200);

2 Comments

this is copied from another answer
I had already added note. Note: I just replaced post() with postDelayed() from above examples The issue with me is, I am also using hide keyboard. hence post() did not help me. "200 milliseconds" delay did the magic for me.
0

you can apply this also

 mScrollviewHomepage.setScrollY(View.FOCUS_DOWN);

Comments

-1

I Hope it Work

Try this:

nsscroll.post(new Runnable() { @Override

                    public void run() {

                        nsscroll.scrollTo(0, nsscroll.getBottom());

                    }
                });

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.