Is there a way to programmatically scroll a scroll view to a specific edit text?

NotACleverMan picture NotACleverMan · Jul 26, 2011 · Viewed 154.5k times · Source

I have a very long activity with a scrollview. It is a form with various fields that the user must fill in. I have a checkbox half way down my form, and when the user checks it I want to scroll to a specific part of the view. Is there any way to scroll to an EditText object (or any other view object) programmatically?

Also, I know this is possible using X and Y coords but I want to avoid doing this as the form may changed from user to user.

Answer

Sherif elKhatib picture Sherif elKhatib · Jul 26, 2011
private final void focusOnView(){
        your_scrollview.post(new Runnable() {
            @Override
            public void run() {
                your_scrollview.scrollTo(0, your_EditBox.getBottom());
            }
        });
    }