I have added the view by using WindowManager
like this:
windowManager.addView(view, view.getViewParams());
Before and after this line I check view.getWindowToken()
and it is always null
.
Could you explain why view.getWindowToken()
is null
and when it becomes not null
?
When you perform windowManager.addView(...)
the view does not immediately get attached to window. Rather you are posting an action, that will take place some time later.
In order to wait enough so that above mentioned "some time" passes, you can post an action to the view:
view.post(new Runnable() {
public void run() {
// now view is attached to window
view.getWindowToken();
}
});