Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to scroll to bottom in a ScrollView on activity startup

Tags:

android

scroll

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.

like image 531
Palo Avatar asked Jul 22 '10 09:07

Palo


People also ask

How do I scroll to the bottom of the page on Android?

Make sure the webpage is long enough so that the browser shows a scroll bar. Once there, to get to the bottom of the page, simply tap the top right corner on your device. In the screenshot below, I need to tap the area where the time is shown. The page will automatically scroll all the way down to the bottom.

How do I make my activity scrollable?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In this above code, we have declare Linear layout as parent and added Vertical Scroll view.

What is fillViewport in ScrollView?

fillViewport allows scrollView to extend it's height equals to the full height of device screen's height in the cases when the child of scroll view has less height.


2 Answers

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.

like image 121
Palo Avatar answered Oct 17 '22 00:10

Palo


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);     } }); 
like image 44
Harshid Avatar answered Oct 17 '22 01:10

Harshid