Android Custom Keyboard - Preview View Constrained To Parent Layout
Solution 1:
The preview key is actually a PopupWindow
created in the constructor for KeyboardView
. (See the code here).
The problem that you are seeing is because the PopupWindow
is being clipped by its parent. If clipping can be disabled for the PopupWindow
then the preview key will be able to "pop" outside of the keyboard view. You can see that this approach works if you set a breakpoint at the above-referenced code and execute the following code:
mPreviewPopup.setClippingEnabled(false)
See setClippingEnabled in the documentation for PopupWindow
. By default, clipping is enabled.
setClippingEnabled
void setClippingEnabled (boolean enabled)
Allows the popup window to extend beyond the bounds of the screen. By default the window is clipped to the screen boundaries. Setting this to false will allow windows to be accurately positioned.
It says "screen," but it applies to the keyboard window as well.
The remaining question is: How to get clipping disabled? Unfortunately, mPreviewPopup
is a private variable. Reflection will give access but is not ideal. I have not found another way to disable clipping for this private PopupWindow
, so this is just pointing a way to a resolution and not the resolution itself.
Update: I took another look at what is going on. Here is what I found with the various API levels.
APIs 17-21: Key preview is allowed to pop outside of the boundaries of the keyboard. APIs 22,23,25-26: The key preview is constrained to the boundaries of the keyboard but does display in its entirety. This is what the OP was seeing. API 24: Key preview is constrained to the boundaries of the keyboard but is otherwise clipped. (Broken)
So, the change occurred with API 22. The only substantive difference I see between API 21 and API 22 is support for the FLAG_LAYOUT_ATTACHED_IN_DECOR flag. (Also see setAttachedInDecor.) This code does deal with the placement of the popup window, so it may be related to this problem. (I no longer believe that this is true.)
I also found this which also may be related. (Maybe...)
setClippingEnabled()
works on API 22-26 to permit the preview key to pop outside the boundaries of the keyboard layout. Other than using reflection, I don't see a way to correct the problem.
This may qualify for a bug report although the new constrained behavior may be the expected behavior.
Here is a video of API 26 exhibiting the problem, then I set the mClippingEnabled
flag to false
in PopupWindow.java
and show the corrected behavior.
Solution 2:
For anyone coming here in the future, I recommend not using KeyboardView
. At least at the time of this writing (API 27), it hasn't been updated for a long time. The problem described in the question above is just one of its many shortcomings.
It is more work, but you can make your own custom view to use as a keyboard. I describe that process at the end of this answer.
When you create the popup preview window in your custom keyboard, you need to call popupWindow.setClippingEnabled(false)
in order to get the popup window to display above the keyboard for Android API 22+. (Thanks to this answer for that insight.)
Here is an example in context from one of my recent projects.
privatevoidlayoutAndShowPopupWindow(Key key, int xPosition){
popupWindow = newPopupWindow(popupView,
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
popupWindow.setClippingEnabled(false);// <-- let popup display above keyboardint location[] = newint[2];
key.getLocationInWindow(location);
int measureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
popupView.measure(measureSpec, measureSpec);
int popupWidth = popupView.getMeasuredWidth();
int spaceAboveKey = key.getHeight() / 4;
int x = xPosition - popupWidth / popupView.getChildCount() / 2;
int y = location[1] - popupView.getMeasuredHeight() - spaceAboveKey;
popupWindow.showAtLocation(key, Gravity.NO_GRAVITY, x, y);
// using popupWindow.showAsDropDown(key, 0, yOffset) might be// easier than popupWindow.showAtLocation()
}
Solution 3:
After spending hours on this issue, I was finally able to find a solution which I feel is reasonably un-hacky. In your custom InputMethodService
, maintain a reference to your KeyboardView, mKeyboardView
, and then override onStartInputView()
. Then, grab the KeyboardView's parent, apply some padding to its top boundary, and finally, call setPopupParent()
on the KeyboardView with this modified parent layout. Here's the code that I have:
@OverridepublicvoidonStartInputView(EditorInfo info, boolean restarting){
ViewGrouporiginalParent= (ViewGroup) mKeyboardView.getParent();
if (originalParent != null) {
originalParent.setPadding(0,LAYOUT_PADDING, 0, 0);
mKeyboardView.setPopupParent(originalParent);
}
}
You can filter the behavior of this overriden method according to the APIs on which this problem exists.
Kudos to Tang Ke for mentioning setPopupParent()
. You could try overriding a different method and using the same logic if you wanted, however, you cannot do it in onCreateInputView()
as the KeyboardView's parent will not have been created yet.
Post a Comment for "Android Custom Keyboard - Preview View Constrained To Parent Layout"