androidandroid-layoutdialogandroid-stylesandroid-windowmanager

Created a custom layout dialog, but not able to dismiss dialog when clicked outside dialog window


        **Please help me out where I have done wrong**
  

I am not able to dismiss dialog when clicked on the outside area on my screen only dismissed when I press backpress in mobile keys.

        Dialog dialog = new Dialog(mContext, android.R.style.Theme_Translucent_NoTitleBar);
        dialog.setContentView(R.layout.dialog_child_product_two);
        WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
        lp.dimAmount = 0.7f; // Dim level. 0.0 - no dim, 1.0 - completely opaque
        dialog.getWindow().setAttributes(lp);
        dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        dialog.setCancelable(false);
        dialog.setCanceledOnTouchOutside(true);
        
        *// Views inflated in my dialog to perform some actions on keypress*
        Button btnDone = (Button) dialog.findViewById(R.id.txt_done);
       
        btnDone.setOnClickListener(view -> {
            // Do some work here after some action performed
            dialog.dismiss();
        });
        dialog.show();

Solution

  •     Dialog dialog = new Dialog(mContext);
    

    I have tried removing the style from this dialog and added the following :

    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
    

    Or

    dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
    

    Both working fine and able to dismiss the dialog on the outside area click.