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.
The following is what I'm using:
This gets the scroll amount necessary to show the bottom of the view, including any margin on the bottom of that view.
Vertical scroll, good for forms. Answer is based on Ahmadalibaloch horizontal scroll.
I think I have found more elegant and less error prone solution using
There is no math involved, and contrary to other proposed solutions, it will handle correctly scrolling both up and down.
It is a good idea to wrap it into
postDelayed
to make it more reliable, in case theScrollView
is being changed at the momentThis works well for me :
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
My solution is:
If scrlMain is your NestedScrollView, then use the following,