Solution 1 :
just add this line in onCreate method:
this.setFinishOnTouchOutside(false);
Solution 2 :
Remove the first line keep only dialog.setCancelable(false);
Solution 3 :
Use this code to prevent background touch such as dialogues.
getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
Use this code to make the background touch impossible.
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
Thank you for your answers. Have a nice day!
Problem :
If we click the background of the dialog and click the back key, the dialog will not be canceled.
setCanceledOnTouchOutside(false);
setCancelable(false);
However, while the dialog is not canceled, there is a phenomenon that the click in the background in the buttons and the check box are clicked.
Is there a way to solve this problem?
Comments
Comment posted by Dreamer
Your question is not clear!!
Comment posted by Mouaad Abdelghafour AITALI
From your comment, your issue is when you open your dialog, the background (current activity) components are still clickable, right? Try to wrap all of them with a
Comment posted by user15043386
getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE); I solved it by putting this sentence in. Thank you for your reply.
Comment posted by user15043386
This code does not close the dialog, but the check box or button in the background is still clicked.
Comment posted by Mado
my bad, actually this is weird, can you provide the code in your main activity/fragments.
Comment posted by Mado
if the dialog is in activity or fragment, when the Alert dialog is popped up on screen, Activity/fragment will be onPuase() state the buttons should not be clickable till the dialog is dismissed.
Comment posted by user15043386
getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE); I solved it by putting this sentence in. Thank you for your reply.
Comment posted by Mado
great, thank you for sharing the solution, I would suggest you to add this answer in the answer section, maybe it will help someone, cheers lad!
Comment posted by user15043386
I think that sentence is a code that cannot be canceled when the back key is used. What I want is to block the click of a check box or button on the background of the dialog.