-1

I am trying to show an AlertDialog which could be dismissed with back button or touching outside the dialog. There are so many solutions over there and I know which the right one because I used it some time before. But now I can't achieve it. Here is my code:

AlertDialog alertDialog = new AlertDialog.Builder(activity, R.style.AppTheme_Dark_Dialog).create();
//Some setup of the dialog...
...
//This works when I press the back button
alertDialog.setCancelable(true); 
//This should be the right answer of my problem
alertDialog.setCanceledOnTouchOutside(true); 
alertDialog.show();

As I know, it's enough by calling this method with a true parameter value:

alertDialog.setCanceledOnTouchOutside(true);

But won't work. Can someone enlighten me please!?


Edit: some testing

As Ugas mentioned, I changed the device for testing, and it worked.

  • Android 5.1 (API 22): Not working
  • Android 9 (API 28): Working as expected
IgniteCoders
  • 4,834
  • 3
  • 44
  • 62

1 Answers1

-2

Remove

alertDialog.setCanceledOnTouchOutside(true);

keep other code as it is and check.