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

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


private final void focusOnView(){        your_scrollview.post(new Runnable() {            @Override            public void run() {                your_scrollview.scrollTo(0, your_EditBox.getBottom());            }        });    }


The answer of Sherif elKhatib can be greatly improved, if you want to scroll the view to the center of the scroll view. This reusable method smooth scrolls the view to the visible center of a HorizontalScrollView.

private final void focusOnView(final HorizontalScrollView scroll, final View view) {    new Handler().post(new Runnable() {        @Override        public void run() {            int vLeft = view.getLeft();            int vRight = view.getRight();            int sWidth = scroll.getWidth();            scroll.smoothScrollTo(((vLeft + vRight - sWidth) / 2), 0);        }    });}

For a vertical ScrollView use

...int vTop = view.getTop();int vBottom = view.getBottom();int sHeight = scroll.getBottom();scroll.smoothScrollTo(((vTop + vBottom - sHeight) / 2), 0);...


This works well for me :

  targetView.getParent().requestChildFocus(targetView,targetView);

public void RequestChildFocus (View child, View focused)

child - The child of this ViewParent that wants focus. This view will contain the focused view. It is not necessarily the view that actually has focus.

focused - The view that is a descendant of child that actually has focus