2

i have an AlertDialog on my Android app. When i show to AlertDialog i want to disable only when users click "OKAY" button. Because i reset the screen when users clicked "OKAY" button.

My problem is when i click somewhere on screen outside of AlertDialog, dialog is closing but i can not clean the screen.

This is my code;

AlertDialog.Builder builder = new AlertDialog.Builder(GameOnePlayer.this, R.style.AlertDialogTheme);
//builder.setCancelable(true);
View view = LayoutInflater.from(GameOnePlayer.this).inflate(
        R.layout.layout_winner_dialog,
        (ConstraintLayout)findViewById(R.id.layoutAlertDialogContainer)
);
builder.setView(view);

final AlertDialog alertDialog = builder.create();
//alertDialog.setCanceledOnTouchOutside(false);

view.findViewById(R.id.buttonAlertDialog).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        hideSystemUI();
        clearScreen();
        alertDialog.dismiss();
    }
});

if(alertDialog.getWindow() != null){
    alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
}

alertDialog.show();

I tried alertDialog.setCanceledOnTouchOutside(false); but it did'not work.

<style name="AlertDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
    <item name="android:windowCloseOnTouchOutside">false</item>
</style>

This is my style.xml

What do I have to do for this? Thanks.

Edit: I tried these advices but did not work.

builder.setCancelable(false);
alertDialog.setCancelable(false);
alertDialog.setCanceledOnTouchOutside(false);

Edit2: Hi i found the solution on the other post. Just added these line in the onCreate method.

this.setFinishOnTouchOutside(false);

Many thanks for all you helpers.

CoolMind
  • 26,736
  • 15
  • 188
  • 224
ccanozerr
  • 23
  • 7

3 Answers3

2
  alertDialog.setCancelable(false);
  alertDialog.setCanceledOnTouchOutside(false);
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
0
builder.setCancelable(false)

Put this line below the "builder.setView(view)" line so your dialog is not close when you touch outside the dialog or when you press back button your dialog is not closed.

0

You can add OnCancelListener to your dialog to listen Dialog cancellation and perform the screen reset.

      builder.setOnCancelListener { 
                 //call your method to reset the screen
               }
Alpha 1
  • 4,118
  • 2
  • 17
  • 23