Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Added robust keyboard visibility detection using WindowInsetsCompat. #17917

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Scapesfear
Copy link
Contributor

@Scapesfear Scapesfear commented Feb 4, 2025

Purpose / Description

Ensures that when the keyboard is open, pressing back first closes the keyboard instead of collapsing the search view, using robust keyboard visibility detection with WindowInsetsCompat

Fixes

Approach

Implemented WindowInsetsCompat to accurately detect keyboard visibility. On back press, if the keyboard is open, it is hidden first instead of collapsing the search view..

How Has This Been Tested?

Samsun Galaxy F23 5G
https://github.com/user-attachments/assets/cc33384c-768f-4bf3-b82b-935c2019f68c

Checklist

Please, go through these checks before submitting the PR.

  • You have a descriptive commit message with a short title (first line, max 50 chars).
  • You have commented your code, particularly in hard-to-understand areas
  • You have performed a self-review of your own code
  • UI changes: include screenshots of all affected screens (in particular showing any new or changed strings)
  • UI Changes: You have tested your change using the Google Accessibility Scanner

Copy link

@MrTheMech MrTheMech left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @Scapesfear ,

I tested the implementation, and it works perfectly! 🚀 The fix effectively prevents the keyboard from popping up when returning to searched cards.

One small improvement: Instead of naming the variable isCardBrowserReloaded, we can replace it with shouldClearSearchFocus for better readability and clarity.

I've attached a video of my test:
https://github.com/user-attachments/assets/5d2f647d-0b79-4b02-a3ae-65e789acff98

@Scapesfear
Copy link
Contributor Author

One small improvement: Instead of naming the variable isCardBrowserReloaded, we can replace it with shouldClearSearchFocus for better readability and clarity.

Hi @MrTheMech, thanks for suggestion, actually i named the variable so, because when we come back from any activity to the caller, the caller gets reloaded and thus I named it isCardBrowserReloaded.

@Scapesfear Scapesfear changed the title Fix: Prevents keyboard popup when returning from Note Editor to searched cards Fix: Added robust keyboard visibility detection using WindowInsetsCompat. Feb 9, 2025
Copy link
Member

@mikehardy mikehardy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like it should work - one curiosity (that may result in no change...) about choice of method to close the soft keyboard

Comment on lines +588 to +589
val imm = view.context.getSystemService(Context.INPUT_METHOD_SERVICE) as? InputMethodManager
imm?.hideSoftInputFromWindow(view.windowToken, 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity, why not use this since we are already using the WindowInsetsCompat: https://developer.android.com/reference/kotlin/androidx/core/view/SoftwareKeyboardControllerCompat#hide()

It may be that using the InputMethodManager directly is the best way, but I'm curious if that was a considered decision or not, and if so, why using InputMethodManager vs the Compat is best

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @mikehardy,

hide() is essentially an abstraction over the InputMethodManager approach. Here's the snippet that shows what it does under the hood:

@Override
void hide() {
    if (mView != null) {
        ((InputMethodManager) mView.getContext()
                .getSystemService(Context.INPUT_METHOD_SERVICE))
                .hideSoftInputFromWindow(mView.getWindowToken(), 0);
    }
}

I opted for the InputMethodManager method because when I searched for ways to hide the keyboard, this was the most popular solution. I tested it, and it reliably works across various Android versions.

Also, note that the alternative approach using

view.windowInsetsController?.hide(WindowInsets.Type.ime())

is deprecated.

@mikehardy mikehardy added Needs Author Reply Waiting for a reply from the original author Needs Review labels Feb 16, 2025
@Scapesfear Scapesfear requested a review from mikehardy February 18, 2025 01:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Author Reply Waiting for a reply from the original author Needs Review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Pressing back to dismiss keyboard closes search
3 participants