androidandroid-viewandroid-windowmanagerandroid-window

Get window token of the window to which decorview is attached


How to get the window token of the window to which decorview is attached?


Solution

  • If I have understood your question correctly, then this is my answer:

    
        getWindow().getDecorView().addOnAttachStateChangeListener(
            new View.OnAttachStateChangeListener() {
              @Override public void onViewAttachedToWindow(View v) {
                getWindow().getDecorView().removeOnAttachStateChangeListener(this);
                IBinder binder = v.getWindowToken();
              }
    
              @Override public void onViewDetachedFromWindow(View v) {
    
              }
            });