How to get the window instance from a view?

9.7k views Asked by At

i add a view in the windowManager via mWindowManager.addview(). Now i would like to know if it's possible to get the window instance. their is myView.getWindowID() and myView.getWindowToken() but i can't find a way to retrieve from it the window instance

3

There are 3 answers

4
Vishnu Prasad On

You cannot get a window instance from View or WindowManager. But you can get the Display to which the View's window is been attached by calling this method myView.getDisplay()

Edited -you can use View.bringToFront(); or View.bringChildToFront(View child); to reorder the z-index of views.

1
MageNative On

myView.getDisplay() you can use this method to display the View.

1
alijandro On

If your View has been attached to Activity, you can do like this.

View view; // your view
if (view.getContext() instanceof Activity) {
    Window window = ((Activity) view.getContext()).getWindow();
}

After API 19, there is a convenient method to check, view.isAttachedToWindow()