How to get the window instance from a view?
Asked Answered
C

3

10

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

Chetnik answered 30/5, 2017 at 7:43 Comment(0)
G
7

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()

Gumption answered 30/5, 2017 at 8:0 Comment(1)
yes thanks, but without too much surprise, you code gave me the window of the app instead of the window where the view sit in :( ie the window created by the windowManager when we call the method addViewChetnik
D
2

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.

Dereism answered 30/5, 2017 at 7:52 Comment(4)
Thanks Vishnu ... I wondering why it's not possible to retrieve the window instance? Actually i need it because i would like to see if it's possible to change it's z-order (#44232097)Chetnik
the z-order is recently added view addView() is on top. so if you remove a view and again call addView() on that same view it comes to top. Another way is to have single FrameLayout added to window manager and then add other views to this framelayout so that you manipulate the z-ordering of views inside FrameLayoutDereism
thanks vishnu, seam you understand. yes the z-order of the recently added view (via windowmanager) is on the top, however removing it to add it back make the view visually disappear and reappear that is not very convenient :(Chetnik
you can use View.bringToFront(); or View.bringchildToFront(View child); to reorder the z-indexDereism
L
-1

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

Laith answered 30/5, 2017 at 7:59 Comment(1)
thanks mageNative, but what i can do with getDisplay ?Chetnik

© 2022 - 2024 — McMap. All rights reserved.