Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show PopupWindow above all window for target android 26

I want to show PopupWindow above all window (example: another popupwindow, dialog, another activity screen) without request SYSTEM_ALERT_WINDOW so I use WindowManager.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 working well in all android version if I set targetSdkVersion < 26 .

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

I see that TYPE_TOAST is deprecated in sdk 26 and they suggest to use TYPE_APPLICATION_OVERLAY. However, when I use TYPE_APPLICATION_OVERLAY, AndroidStudio show TYPE_APPLICATION_OVERLAY required api 26. Therefore, TYPE_APPLICATION_OVERLAY only work well with device api 26, for device api < 26, it will crash ( even I have enabled Display/Draw over other app permission) enter image description here

Is there any alternative way to make TYPE_TOAST work with target api 26? Any help or suggestion would be great appreciated.

like image 425
Linh Avatar asked Oct 20 '17 07:10

Linh


1 Answers

There appears to be a bug with API 25. The bug is that if you change the target API to be 26, API 25 devices will no longer be able to use the TYPE_TOAST parameter, even though it is allowable in API 25.

The only way to fix this behavior is to have the user enable overlay permissions in Settings for your app. API 25 and 26 overlays will then function as expected.

like image 57
AutoM8R Avatar answered Oct 07 '22 19:10

AutoM8R