Scroll to error field
Closed this issue · 10 comments
Is there a way to scroll and focus on the error field? Right now, all invalid fields are red, but the page stays scrolled to the bottom when it's a long form.
Thank you,
Igor
Hi Igor,
Thanks for your feedback.
In theory, yes, it's possible. Shouldn't be too much code change. I'll just try to add another API including an extra scrollToFirstError boolean parameter.
Actually at the beginning I thought about returning an ArrayList of invalid EditTexts to developers, then you can do whatever you want, including scrolling to any of them.
So I'll look into this later, not sure when I have time. Sorry for that, while I'll try my best.
Thanks again.
Best,
Jing
Thanks for making headway on this project. @thyrlian Have some @changetip bitcoin $1 on me.
Hi @thyrlian, @IgorGanapolsky sent you a Bitcoin tip worth 4,635 bits ($1.00), and I'm here to deliver it ➔ collect your tip.
Hi Igor,
I just got time to take a look at the code. Actually it turns out that there is already such a function.
Here is the code:
Validator.java
protected boolean checkFields(ValidationCallback callback) {
// some other code ...
boolean hasFailed = false;
for (ValidationHolder validationHolder : mValidationHolderList) {
// some other code ...
if (!valid && matcher != null) {
callback.execute(validationHolder, matcher);
if (!hasFailed) {
EditText editText = validationHolder.getEditText();
editText.requestFocus();
editText.setSelection(editText.getText().length());
hasFailed = true;
}
result = false;
}
}
return result;
}
Without giving the scrollable container, it's hard to guess which parent is the right one to call scrolling method on. In consideration of that, the code snippet above just simply requests focus for the first failed EditText field.
So could you please try again with the demo project along with this repo, just add a few more views inside to make it a long scrolling view. Then trigger the validation, to see if it could scroll as you expect? Thanks a lot.
Best,
Jing
Actually, the code you are referring to just requests focus on the EditText. However, on a big form (with a ScrollView), the user will not see this error unless they manually scroll up. So my solution, in my own project, was to use scrollTo(int x, int y)
with the y-coordinate of the edit text in question.
Yes, that code just requests focus. But I tried with the demo project, which also has a ScrollView, when the view gets longer, triggering validation will set the focus - which makes it scroll to that failed EditText field automatically.
That's strange if it doesn't work for you. While as mentioned earlier, without passing the scrollable view container as a parameter to the API, it's hard for the code to guess who should call scrollTo(int x, int y)
. So in that case, I think it's safer and easier to return an ArrayList of failed fields for trigger()
. Thus you could manually scroll to the first element of the ArrayList.
Now that I look at it closely, you're right. It does trigger the validation and scroll.
Cool, so it works for you?
But in fact I lied to you ;) Though it works for the demo project, while I found out there is a minor issue: the scrolling position is not exactly the beginning of the first failed field, but a few pixels down. Do you know what could be the problem there? Sorry for my laziness :P
I suggest you mark this issue as solved, even if there's room from improvement.
People not following the whole discussion could be under the a false impression that's some sort of bug or important limitation.
@androiddev-smartside thank you :)