Show popup above whole window for target android 26

I want to show PopupWindowabove the whole window (example: another popup, dialog, another activity screen) without a request SYSTEM_ALERT_WINDOW, so I useWindowManager.LayoutParams.TYPE_TOAST

public void showSimplePopupWindow() {
    final View popupView = layoutInflater.inflate(R.layout.popup_layout_2, null);

    final PopupWindow popupWindow = new PopupWindow(popupView);
    ...config popup window...

    PopupWindowCompat.setWindowLayoutType(popupWindow, WindowManager.LayoutParams.TYPE_TOAST);
    popupWindow.showAsDropDown(findViewById(R.id.button_show_popup_window));
}

It works well in all versions of Android if I install targetSdkVersion < 26.

Currently, if I save the code above and update the target targetSdkVersionto 26, then it will crash with api 25-26 device with the exceptionandroid.view.WindowManager$BadTokenException: Unable to add window -- token android.view.ViewRootImpl$W@859d91f is not valid; is your activity running?

I see that is TYPE_TOASTout of date sdk 26, and they suggest using TYPE_APPLICATION_OVERLAY. However, when I use it TYPE_APPLICATION_OVERLAY, it AndroidStudioshows the TYPE_APPLICATION_OVERLAYrequired api 26. Therefore, it TYPE_APPLICATION_OVERLAYworks well with the api 26 device, for the api device <26, it will fail (even I turned on the resolution Display/Draw over other app) enter image description here

Is there an alternative way to do the TYPE_TOASTjob with the target api 26? Any help or suggestion would be greatly appreciated.

+6
source share
1 answer

FROM

, , .

0

All Articles