nonzaprej
nonzaprej

Reputation: 1600

Display PopupWindow while Dialog is showing

Premise: with "on top of" I mean "overlaying", with a "higher z-index", so to speak; with "above" I mean "visually above, on the screen".

I guess the question would be "how to display a PopupWindow on top of the keyboard when a Dialog is showing?". Following, the situation...

I'm using this library to have Whatsapp-style emojicons in my app. The way it shows the emojicons in place of the soft-keyboard is by displaying a PopupWindow on top of it, so that it covers it completely.

To create the emojicon's popup, I need to pass a View to the constructor. I tried passing two different views and it doesn't work with either of them. Here's what happens:

Result with Activity's root view

Why does it appear there instead of on top of the keyboard? I really don't understand.

I already tried by changing the windowSoftInputMode and setting the PopupWindow's "InputMethodMode" to PopupWindow.INPUT_METHOD_NOT_NEEDED, but the results are always the same.

To create and show the popup, the library simply calls

public class EmojiconsPopup extends PopupWindow ... {

    ...

    setSize(LayoutParams.MATCH_PARENT, keyBoardHeight);
    ...
    showAtLocation(rootView, Gravity.BOTTOM, 0, 0);

    ...
}

when the keyboard is open.

You can see the library's source code from the link I wrote at the beginning, and here's an open issue about this problem, in case it may contain additional useful info.

Thanks for the help.

Upvotes: 4

Views: 3501

Answers (2)

nonzaprej
nonzaprej

Reputation: 1600

So, I found a solution. To make a PopupWindow overlay the keyboard when a dialog is open, when calling the showAtLocation(View rootView, int gravity, int x, int y) method to show the PopupWindow instead of passing "0, 0" as the coordinates we have to pass, for example, "0, -5000". This way, with a low enough integer as the y coordinate, the popup will be displayed correctly at the bottom of the screen instead of at the bottom of the dialog's root view.

The view to pass as the first parameter must be the dialog's root view, of course.

Remember that if you called setClippingEnabled(false) on the popup, when you call showAtLocation with such a low y coordinate, it will disappear from the screen. If the popup must stay whole inside the screen, isClippingEnabled() must return true.

Upvotes: 2

AguThadeus
AguThadeus

Reputation: 330

I was just thinking android may have more root views than the one used to display your content, you may want to try using the view provided by:

 getWindow().getDecorView()

If that fails then try getting a reference to your root View (the one with your contents) and then:

View myRootView=findViewById(R.id.myroot);  myRootView.getRootView();

Again if not successful, you may further try getting the root View of the above view going back one by one until it returns no more root views, i.e:

myRootView=myRootView.getRootView();
//if it fails go up in hierarchy again, remember to test for null

myRootView=myRootView.getRootView();
//etc..

I assume going up the hierarchy once or twice might give something

Upvotes: 0

Related Questions