@@ -148,19 +148,33 @@ private void showPopupKeysPanelInternal(final View parentView, final Controller
148148 + getPaddingBottom ();
149149
150150 parentView .getLocationInWindow (mCoordinates );
151+ final int containerY = y + CoordinateUtils .y (mCoordinates );
152+ container .setY (containerY );
153+
154+ // This is needed for cases where there's also a text popup above this keyboard
155+ final int panelMaxX = parentView .getMeasuredWidth () - getMeasuredWidth ();
156+ var panelFinalX = Math .max (0 , Math .min (panelMaxX , x ));
157+ var center = panelFinalX + getMeasuredWidth () / 2 ;
158+ var layoutGravity = center < pointX - getKeyboard ().mMostCommonKeyWidth / 2 ?
159+ Gravity .RIGHT : center > pointX + getKeyboard ().mMostCommonKeyWidth / 2 ? Gravity .LEFT : Gravity .CENTER_HORIZONTAL ;
160+
161+ int containerAdjustedX = x ;
162+ if (getMeasuredWidth () < container .getMeasuredWidth ()) {
163+ containerAdjustedX = layoutGravity == Gravity .LEFT ? panelFinalX : layoutGravity == Gravity .RIGHT
164+ ? panelFinalX + getMeasuredWidth () - container .getMeasuredWidth ()
165+ : panelFinalX + (getMeasuredWidth () - container .getMeasuredWidth ()) / 2 ;
166+ }
167+
151168 // Ensure the horizontal position of the panel does not extend past the parentView edges.
152- final int maxX = parentView .getMeasuredWidth () - container .getMeasuredWidth ();
153- var finalX = Math .max (0 , Math .min (maxX , x ));
154- final int panelX = finalX + CoordinateUtils .x (mCoordinates );
155- final int panelY = y + CoordinateUtils .y (mCoordinates );
156- container .setX (panelX );
157- container .setY (panelY );
158-
159- mOriginX = finalX + container .getPaddingLeft ();
160- mOriginY = y + container .getPaddingTop ();
161- var center = panelX + getMeasuredWidth () / 2 ;
162- // This is needed for cases where there's also a long text popup above this keyboard
163- controller .setLayoutGravity (center < pointX ? Gravity .RIGHT : center > pointX ? Gravity .LEFT : Gravity .CENTER_HORIZONTAL );
169+ int containerMaxX = parentView .getMeasuredWidth () - container .getMeasuredWidth ();
170+ int containerFinalX = Math .max (0 , Math .min (containerMaxX , containerAdjustedX ));
171+ int containerX = containerFinalX + CoordinateUtils .x (mCoordinates );
172+ container .setX (containerX );
173+ setTranslationX (panelFinalX - containerFinalX );
174+ controller .setLayoutGravity (layoutGravity );
175+
176+ mOriginX = panelFinalX ;
177+ mOriginY = y + container .getPaddingTop () + (int ) getY ();
164178 controller .onShowPopupKeysPanel (this );
165179 final PopupKeysKeyboardAccessibilityDelegate accessibilityDelegate = mAccessibilityDelegate ;
166180 if (accessibilityDelegate != null
0 commit comments