4

I am showing a dialog box on top of other applications. it is working till i use WindowManager.LayoutParams.TYPE_SYSTEM_ALERT

As i am changing my target version to 26, i should not use TYPE_SYSTEM_ALERT, So i used WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY

after that my dialog is not visible. What else i have to do? Any suggestion.

Reference link: https://developer.android.com/about/versions/oreo/android-8.0-changes#cwt

Code:

    final Dialog dialog = new Dialog(getApplicationContext());

    dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.permission);
    dialog.setCancelable(true);

    dialog.show();
Milad Bahmanabadi
  • 946
  • 11
  • 27
simple
  • 159
  • 1
  • 9

2 Answers2

3

For oreo and above devices we have to use the TYPE_APPLICATION_OVERLAY, for below we have to use TYPE_SYSTEM_ALERT

          final Dialog dialog = new Dialog(getApplicationContext());
          if(oreoAndAbove) {
              dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
          } else {
              dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
          }
          dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
          dialog.setContentView(R.layout.permission);
          dialog.setCancelable(true);

          dialog.show();
simple
  • 159
  • 1
  • 9
2

You may miss request permission for your app.Before show window check it. If app has no permission then request it.

1.check permission by:

public boolean checkPermission(Context context){
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        return Settings.canDrawOverlays(context);
    } else {
        return true;
    }
}

2.do request permission by:

context.startActivity(new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + context.packageName)))
aolphn
  • 2,950
  • 2
  • 21
  • 30