diff --git a/java/src/com/android/inputmethod/keyboard/PopupMiniKeyboardView.java b/java/src/com/android/inputmethod/keyboard/PopupMiniKeyboardView.java
index fa2aa874c1f42b0a6a36f6b9343141030692fe30..60d87f7892ea5b017114c09461b27d16e4508958 100644
--- a/java/src/com/android/inputmethod/keyboard/PopupMiniKeyboardView.java
+++ b/java/src/com/android/inputmethod/keyboard/PopupMiniKeyboardView.java
@@ -76,9 +76,11 @@ public class PopupMiniKeyboardView extends KeyboardView implements PopupPanel {
         final int pointX = (mConfigShowMiniKeyboardAtTouchedPoint) ? tracker.getLastX()
                 : parentKey.mX + parentKey.mWidth / 2;
         final int pointY = parentKey.mY;
-        final int miniKeyboardX = pointX - miniKeyboard.getDefaultCoordX()
-                - container.getPaddingLeft()
-                + parentKeyboardView.getPaddingLeft() + mCoordinates[0];
+        final int miniKeyboardLeft = pointX - miniKeyboard.getDefaultCoordX()
+                + parentKeyboardView.getPaddingLeft();
+        final int miniKeyboardX = Math.max(0, Math.min(miniKeyboardLeft,
+                parentKeyboardView.getWidth() - miniKeyboard.getMinWidth()))
+                - container.getPaddingLeft() + mCoordinates[0];
         final int miniKeyboardY = pointY - parentKeyboard.getVerticalGap()
                 - (container.getMeasuredHeight() - container.getPaddingBottom())
                 + parentKeyboardView.getPaddingTop() + mCoordinates[1];