1

I'm triggering an AlertDialog from my MainActivity and it works fine like this:

public void showCustomAlert(String text){

    final String alertText = text;

    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            AlertDialog.Builder myDialogBox = new AlertDialog.Builder(mContext);
            myDialogBox.setTitle("Alert");
            myDialogBox.setMessage(alertText);
            myDialogBox.setCancelable(false);
            myDialogBox.setPositiveButton("OK", new DialogInterface.OnClickListener(){

                  public void onClick(DialogInterface dialog, int whichButton) {
                         dialog.dismiss();
                  }

            });
            myDialogBox.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
            {
                  public void onClick(DialogInterface dialog, int which) {
                         dialog.dismiss();
                  }
            });
            AlertDialog alertDialog = myDialogBox.create();
            alertDialog.show();
       }
    });
}

The problem arises when I open another activity on top of MainActivity and trigger the AlertBox again: it get's behind that activity. When I close the activity, there it is: the AlertDialog is showing.

How can I show this AlertDialog always on top?

Note: this AlertDialog is triggered by a push notification listener on my MainActivity, not with a click listener.

fana
  • 374
  • 7
  • 17

2 Answers2

5

It is the context problem ,because dialog is using the same activity context , If this dialog has to be on Top always, it can be done with SYSTEM_ALERT_SERVICE like

public void showCustomAlert(String text){

    final String alertText = text;

    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            AlertDialog.Builder myDialogBox = new AlertDialog.Builder(mContext);
            myDialogBox.setTitle("Alert");
            myDialogBox.setMessage(alertText);
            myDialogBox.setCancelable(false);
            myDialogBox.setPositiveButton("OK", new DialogInterface.OnClickListener(){

                  public void onClick(DialogInterface dialog, int whichButton) {
                         dialog.dismiss();
                  }

            });
            myDialogBox.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
            {
                  public void onClick(DialogInterface dialog, int which) {
                         dialog.dismiss();
                  }
            });
            AlertDialog alertDialog = myDialogBox.create();
                  alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
            alertDialog.show();
       }
    });
}

Take care of "android.permission.SYSTEM_ALERT_WINDOW" permission. Hope this resolves your issue .

Connor Pearson
  • 63,902
  • 28
  • 145
  • 142
Renu Yadav
  • 316
  • 1
  • 6
  • This is correct, thanks. Just changed TYPE_SYSTEM_ALERT to TYPE_TOAST because it crashed the app in Android 6. Thanks! – fana Feb 06 '17 at 14:33
  • but if i change TYPE_SYSTEM_ALERT to TYPE_TOAST, i found the alertdialog will auto dismiss like toast. May i know how to solve it ? – vic3ai May 13 '20 at 01:46
-1

this worked for me (Kotlin) note that this Utilities.mainactivity is initialized as the main activity on start up so that it can be used in this function

fun messageBox(message: String, title: String) {
try {
    Utilities.mainActivity.runOnUiThread { //this is so that a nonui thread can show a message
        val dialog = AlertDialog.Builder(Utilities.mainActivity)
        dialog.setTitle(title)
            .setMessage(message)
            .setPositiveButton("Ok", DialogInterface.OnClickListener { dialoginterface, i -> })

        val alertDialog: AlertDialog = dialog.create()
        
        //this is done in an effort to make the dialog always appear on the topmost screen
        alertDialog.window.setType(WindowManager.LayoutParams.LAST_APPLICATION_WINDOW)
        alertDialog.show()
        alertDialog.getWindow().setGravity(Gravity.TOP);
    }
} catch (ex: Exception) {
    Log(ex.toString())
}
}
Nguyễn Văn Phong
  • 13,506
  • 17
  • 39
  • 56
  • There is not a good answer, no way it will work. The LAST_APPLICATION_WINDOW param is just to give the max value for a window type value. – DavidR May 11 '21 at 02:11