99

I am displaying some data in a ScrollView. On activity startup (method onCreate) I fill the ScrollView with data and want to scroll to the bottom.

I tried to use getScrollView().fullScroll(ScrollView.FOCUS_DOWN). This works when I make it as an action on button click but it doesn't work in the onCreate method.

Is there any way how to scroll the ScrollView to the bottom on activity startup? That means the view is already scrolled to the bottom when first time displayed.

Palo
  • 10,591
  • 7
  • 28
  • 34
  • This also explains why I was getting 0 all the time when I was trying to use getMeasuredWidth while initialising my own subclass of a ScrollView. – Ghoti Jul 27 '12 at 15:01

9 Answers9

221

It needs to be done as following:

    getScrollView().post(new Runnable() {

        @Override
        public void run() {
            getScrollView().fullScroll(ScrollView.FOCUS_DOWN);
        }
    });

This way the view is first updated and then scrolls to the "new" bottom.

Palo
  • 10,591
  • 7
  • 28
  • 34
  • 1
    I'm trying exactly this but it doesn't seem to work still... are you calling the above code in `onCreate` of the containing `Activity`? – pstanton Jul 20 '11 at 11:03
  • 4
    Instead post() use postDelayed() with 100ms delay. it will work from any activitys life-cycle point. – Vitaliy A Jun 03 '15 at 13:39
50

Put the following code after your data is added:

final ScrollView scrollview = ((ScrollView) findViewById(R.id.scrollview));
scrollview.post(new Runnable() {
    @Override
    public void run() {
        scrollview.fullScroll(ScrollView.FOCUS_DOWN);
    }
});
Community
  • 1
  • 1
Harshid
  • 5,701
  • 4
  • 37
  • 50
32

This works for me:

scrollview.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        scrollview.post(new Runnable() {
            public void run() {
                scrollview.fullScroll(View.FOCUS_DOWN);
            }
        });
    }
});
Seraphim's
  • 12,559
  • 20
  • 88
  • 129
sonida
  • 4,411
  • 1
  • 38
  • 40
  • 2
    Best answer for this question so far. Lots of other approaches are conditional but this one is awesome n works super fine :) – rana Nov 16 '16 at 23:05
16

You can do this in layout file:

                android:id="@+id/listViewContent"
                android:layout_width="wrap_content"
                android:layout_height="381dp" 
                android:stackFromBottom="true"
                android:transcriptMode="alwaysScroll">
Tam Dao
  • 435
  • 4
  • 10
10
 scrollView.postDelayed(new Runnable() {
        @Override
        public void run() {
            scrollView.fullScroll(ScrollView.FOCUS_DOWN);
        }
    },1000);
Makvin
  • 3,475
  • 27
  • 26
5

Try this

    final ScrollView scrollview = ((ScrollView) findViewById(R.id.scrollview));
    scrollview.post(new Runnable() {
       @Override
       public void run() {
         scrollview.fullScroll(ScrollView.FOCUS_DOWN);
       }
    });
F.O.O
  • 4,730
  • 4
  • 24
  • 34
3

Right after you append data to the view add this single line:

yourScrollview.fullScroll(ScrollView.FOCUS_DOWN);
LEMUEL ADANE
  • 8,336
  • 16
  • 58
  • 72
3

This is the best way of doing this.

scrollView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            scrollView.post(new Runnable() {
                @Override
                public void run() {
                    scrollView.fullScroll(View.FOCUS_DOWN);
                }
            });
        }
});
Jacob
  • 299
  • 1
  • 18
rana
  • 1,824
  • 3
  • 21
  • 36
2

After initializing your UI component and fill it with data. add those line to your on create method

Runnable runnable=new Runnable() {
        @Override
        public void run() {
            scrollView.fullScroll(ScrollView.FOCUS_DOWN);
        }
    };
    scrollView.post(runnable);
Ramzy Hassan
  • 926
  • 8
  • 21