I create an form with 15 editText inside NestedScrollView. Suppose second editText is empty. So after clicking Submit button, I want that page directly goes to the 2nd editText from Submit button to show error. But I don't know how to go from Submit button to second editText directly. Can you help me ? Thank you.
Asked
Active
Viewed 141 times
2 Answers
1
For that, you can use scrollTo()
or smoothScrollTo()
as mentioned here
To make it simpler, create a function as below:
//Used to smooth scroll the scrollbar to focused view. //@param view : parameter view is any view you want to scroll to //view.bottom indicates that you want to scroll to the bottom of the view, //you can change it to view.top private fun smoothScrollToThis(view: View) { scrollView.post { scrollView.smoothScrollTo(0, view.bottom) } /* view.post{} here is a runnable function and uses a separate thread to perform the operation so the UI doesn't freeze.*/ }
This will scroll the
ScrollBar
to the view. Difference betweenscrollTo
andsmoothScrollTo
is thatscrollTo
scrolls instantly skipping the in-between part andsmoothScrollTo
works like an actual scroll by a user showing all the widgets in between while scrolling. For a long layout ofScrollView
, you should preferscrollTo
as it's fast.Now, call the function from wherever you want as:
smoothScrollToThis(yourEditText) //You can pass any view

Lalit Fauzdar
- 5,953
- 2
- 26
- 50
1
A simple approach I generally use is this:
submitBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// sets focus on the editText such that the ScrollView automatically scrolls to the EditText
editText.requestFocus();
// displays the error message on the EditText
editText.setError("error message" );
}
});

Dev Sebastian
- 587
- 6
- 11