-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Fix: Enter triggers OK without extra press #18354
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Fix: Enter triggers OK without extra press #18354
Conversation
First PR! 🚀 We sincerely appreciate that you have taken the time to propose a change to AnkiDroid! Please have patience with us as we are all volunteers - we will get to this as soon as possible. |
@@ -144,7 +145,27 @@ fun AlertDialog.Builder.cancelable(cancelable: Boolean): AlertDialog.Builder = t | |||
*/ | |||
inline fun AlertDialog.Builder.show(block: AlertDialog.Builder.() -> Unit): AlertDialog { | |||
this.apply { block() } | |||
return this.show() | |||
val dialog = this.show() | |||
return dialog.setupEnterKeyHandler() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For reviewer 2 to confirm, but I'd rather explicitly opt-in to this on a per-dialog basis, OR find a way to make this a standard default via accessibility
This use of setOnKeyListener
overrides any listeners which we set in calling code, which isn't intuitive to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the feedback! You're absolutely right about setOnKeyListener potentially overriding existing listeners — that could definitely lead to unexpected behavior.
To address this, I’m planning to go with an opt-in approach by modifying the show() function to accept an optional parameter:
fun AlertDialog.Builder.show(enableEnterKeyHandler: Boolean = false)
This way, setupEnterKeyHandler() will only be applied when explicitly requested, preserving backward compatibility and avoiding side effects in existing dialogs.
Let me know if this direction works for you, and I’ll update the PR accordingly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds great!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perfect, cheers!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM - pending merge, needs squash so has to wait for merge queue to be empty
- add support for optional Enter key handler in error dialog
32bce7d
to
e2b8b1c
Compare
I'm in the middle of a merge festival and the isn't going to empty until long after I've moved to other things so I rebased this one to squash it locally then force-pushed that, so I can put it in the auto queue |
Purpose / Description
When an alert dialog appears after entering incorrect credentials in the sync page, pressing the Enter key only selects the "OK" button instead of triggering it. This PR modifies the behavior so that pressing Enter directly triggers the positive button action, making the user experience more intuitive.
Fixes
Approach
Added an extension function setupEnterKeyHandler() to handle Enter key presses in AlertDialogs
Modified the existing show() extension function to apply this handler to all dialogs created through the builder pattern
The handler detects Enter key presses and simulates a click on the positive button if it exists and is enabled
How Has This Been Tested?
Manually verified with the following steps:
Without an AnkiWeb account on AnkiDroid
Attempted to sync
From the sync page, entered an email and wrong password
When error message appeared, pressed Enter key
🎥 Before the Fix
Click to expand
failure.webm
🎥 After the Fix
Click to expand
success.mp4
Before the fix, Enter key only selected the "OK" button requiring a second press.
After the fix, Enter key immediately dismisses the dialog.
Learning
The default behavior of AlertDialogs in Android is to only select but not trigger buttons when Enter is pressed. This is a common UI pattern that needs explicit handling to improve user experience
Checklist