How to scroll view to Top of screen

Viewed 1979

I have a EditText in my android NestedScrollView and I want it to go to top when It's focused, something like this:

enter image description here

I tried:

SmoothScrolling/Scrolling to a direct pixel, SmoothScrolling/Scrolling to the top of the view(or bottom)

none of them work, and not even respond consistently.

Edit:

I got it, will leave the question for documentation porposes.

I got the ScrollView size with

scrollView.getBottom();

And then scrolled to the intended place using:

scrollView.scrollTo(0,2*scrollView.getBottom()/5); //size will vary from view to view
1 Answers

Maybe your scrollView hasn't get enough time to calculate it's size? Did you try:

scrollView.post(new Runnable() {
    @Override
    public void run() {
        scrollView.scrollTo(0, 0);
    }
});
Related