Embedded Android views can't bring up the keyboard
Problem
To make sure that Flutter can always bring up the keyboard I made the window of virtual display's presentation non focusable. As a result the embedded Android views cannot bring up the keyboard. We should figure out if there's some window trickery we can do to let the unfocused window bring up the keyboard. Alternatively see if it's possible to dynamically select the focused window when delivering input events.
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Enable Keyboard for Embedded Android Views in Flutter
The issue arises because making the virtual display's presentation window non-focusable prevents it from receiving input events, including those needed to trigger the keyboard. This means that while Flutter can display its UI, it cannot interact with embedded Android views that require keyboard input.
Awaiting Verification
Be the first to verify this fix
- 1
Modify Window Flags
Change the window flags for the virtual display's presentation to allow it to receive focus when needed. This can be done by setting the appropriate flags to enable focusability while still allowing Flutter to manage the UI.
javapresentationWindow.setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM); - 2
Implement Input Event Forwarding
Create a mechanism to forward input events from the Flutter layer to the embedded Android views. This can be achieved by overriding the dispatchTouchEvent method in the FlutterActivity to redirect touch events to the appropriate view.
java@Override public boolean dispatchTouchEvent(MotionEvent event) { if (embeddedView.isFocused()) { embeddedView.dispatchTouchEvent(event); return true; } return super.dispatchTouchEvent(event); } - 3
Dynamically Set Focus
Implement logic to dynamically set the focus to the embedded Android view when it is interacted with. This can be done by calling requestFocus() on the embedded view when it is tapped.
javaembeddedView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { v.requestFocus(); } return false; } }); - 4
Test Keyboard Functionality
After implementing the above changes, thoroughly test the keyboard functionality with the embedded Android views to ensure that the keyboard appears as expected when interacting with the views.
Validation
Confirm that the keyboard appears when tapping on the embedded Android views within the Flutter application. Test across various devices to ensure consistent behavior.
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep