PopupWindow z ordering
Asked Answered
G

4

14

I play with menus using PopupWindow, which overlap EditText.

It works fine, except that my PopupWindow is overlapped by some items from EditText IME system (selection marks, Paste button).

My question is: how do I force z-ordering of my PopupWindow so that it appears above those decorations?

Here's image of what's happening. I need my PopupWindow (the menu) be drawn on top of everything, thus somehow tell WindowManager how to order windows. Thanks.

enter image description here

Grodin answered 15/6, 2012 at 20:56 Comment(2)
Good question. Unfortunately I think the answer is going to be that you can't really. If there is a way to do it I imagine that it may only work on stock android. The text selectors and context pop-ups are some of the things that manufacturers commonly introduce customized versions of on their hardware. Even if there is a way to do it, I would guess that it won't work for all devices types because of the customization that they've made to the EditText.Centi
Customizations are irrelevant, technically those things must be drawn somehow at Android level, I suspect they are kind of android.view.Window and use android.view.WindowManager.Grodin
G
9

Found anwer myself.

Those decorations are normal PopupWindow-s, managed by EditText.

Z-ordering of any Window is defined by WindowManager.LayoutParams.type, actually it defines purpose of Window. Valid ranges are FIRST_SUB_WINDOW - LAST_SUB_WINDOW for a popup window.

App typically can't change "type" of PopupWindow, except of calling hidden function PopupWindow.setWindowLayoutType(int) using Java reflection, and setting desired window type.

Result:

enter image description here

EDIT: Code that does that:

  Method[] methods = PopupWindow.class.getMethods();
  for(Method m: methods){
     if(m.getName().equals("setWindowLayoutType")) {
        try{
           m.invoke(getPopupWindow(), WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL);
        }catch(Exception e){
           e.printStackTrace();
        }
        break;
     }
  }
Grodin answered 15/6, 2012 at 23:3 Comment(1)
Could you please provide a code snippet, how you have done that?Mona
S
4
import android.support.v4.widget.PopupWindowCompat;

PopupWindowCompat.setWindowLayoutType(popupWindow, WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL);
Sweated answered 18/9, 2017 at 0:14 Comment(0)
R
2
public void compatibleSetWindowLayoutType(int layoutType) {
    if (Build.VERSION.SDK_INT >= 23) {
        setWindowLayoutType(layoutType);
    } else {
        try {
            Class c = this.getClass();
            Method m = c.getMethod("setWindowLayoutType", Integer.TYPE);
            if(m != null) {
                m.invoke(this, layoutType);
            }
        } catch (Exception e) {
        }
    }
}
Reichenberg answered 21/7, 2017 at 2:52 Comment(0)
O
0

Make are complement. The PopupWindowCompat.setWindowLayoutType API must be called before show popWindow.

Oxyacetylene answered 9/11, 2020 at 12:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.