1

I'm programmatically adding a PopupView which contains an EditText field to my Activity, which is vertically and horizontally centered on the screen. When the keyboard opens, I want the PopupView to move up, so it is still centered on the visible screen/activity part.

My code:

EditText e = new EditText(super.getContext());

PopupWindow popup = new PopupWindow(e, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
popup.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
popup.setOutsideTouchable(true);
popup.setFocusable(true);
popup.showAtLocation(this, Gravity.CENTER, 0, 0);

I've tried many things with windowSoftInputMode for the Activity; I've tried to setSoftInputMode(mode) on the popup - but none of my approaches have worked. Neither my layout nor the Popup change their position when the keyboard opens. (I only want my popup but not the layout to change, though, just pointing it out).

Also the code is placed in a LinearLayout class, in case you are wondering why I'm using this as a View.

Tobias Baumeister
  • 2,107
  • 3
  • 21
  • 36

2 Answers2

2

Easier to get Android to do all the heavy lifting for you.

Just use:

popup.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
HughHughTeotl
  • 5,439
  • 3
  • 34
  • 49
0

So after much research, I finally found a way to accomplish that.

The code for creating the PopupWindow and making it being displayed in the vertical and horizontal center stays the same:

PopupWindow popup = new PopupWindow(
         popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
/** ... **/
popup.showAtLocation(this, Gravity.CENTER, 0, 0);

Then the only thing you need is a Listener for the Keyboard (or more general: For Window Height changes). This was actually easier than I thought - and it didn't require any special access like an Activity-object or similar. Even in my independent View-class which only knows the Context (which I didn't want to cast), I was able to accomplish that. Everything you need is only one View-object which has already been added to the layout.

// You can call this method on any view that is added to the layout:
final View root = this.getRootView();
root.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    public void onGlobalLayout() {
        Rect r = new Rect();
        root.getWindowVisibleDisplayFrame(r);

        // Calculate the difference between the original height and the new height
        int heightDiff = r.height() - root.getHeight();

        // Now update the Popup's position
        // The first value is the x-axis, which stays the same.
        // Second value is the y-axis. We still want it centered, so move it up by 50% of the height
        // change
        // The third and the fourth values are default values to keep the width/height
        popup.update(0, heightDiff / 2, -1, -1);
    }
});

For reference: Listening to window height changes

Only downside:
This solution may not work when you add a PopupView while the Keyboard is already opened. But in my case, this isn't an expectable scenario anyway.

Community
  • 1
  • 1
Tobias Baumeister
  • 2,107
  • 3
  • 21
  • 36