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.
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.
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.
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.
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.
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); } });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With