Android

ScrollView auto scroll to show content in app screen

While displaying long text data or displaying data in lot of views, we use ScrollView to view whole screen by scrolling. At times we may require app to auto Scroll down in a screen to show a bottom content.

If you try using below code in Activity’s OnCreate method to scroll down in ScrollView

ScrollView mScrollView = (ScrollView) findViewById(R.id.myScrollView);
mScrollView.fullScroll(ScrollView.FOCUS_DOWN);

It won’t scroll. Since the layout is not yet done adding all views and you call fullScroll immediately after instantiating the ScrollView.

Solution for ScrollView to auto scroll down

auto scroll

To solve this issue, A short delay gives, the system enough time to settle. You have to use following code to set the delay

mScrollView.postDelayed(new Runnable() {
    @Override
    public void run() {
       //replace this line to scroll up or down
       mScrollView.fullScroll(ScrollView.FOCUS_DOWN);
    }
}, 100L);

voila, you can see app Scroll down to bottom of screen.

Similarly, If you want to set Scroll to top of screen, in above run() method replace line with

mScrollView.scrollTo(0, 0);

If you are using HorizotalScrollView, and if you want to move to right end of screen, use following code..

mHorizontalScrollView.postDelayed(new Runnable() {
    @Override
    public void run() {
      mHorizontalScrollView.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
    }
}, 100L);

here mHorizontalScrollView is object of HorizantalScrollView.

Hope it helps somebody.

🙂

You may be also interested in

3 thoughts on “ScrollView auto scroll to show content in app screen

Leave a Reply

Your email address will not be published.