0

this dialog has two options. I want to dismiss a dialog by touching background but I don't know how.

public void dialog(  long id) {
    final int     position=(int) id;
        final Dialog dialog = new Dialog(context);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);      

        dialog.setContentView(R.layout.dialog_checkbox);

        // set the custom dialog components - text, image and button

        Button yes = (Button) dialog.findViewById(R.id.yes);
        Button no = (Button) dialog.findViewById(R.id.no);

        yes.setTypeface(type);
        no.setTypeface(type);

        // if button is clicked, close the custom dialog
        yes.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

            String number=contactnumber.get(position);
            String name=contactname.get(position);
                 contactnumber.remove(position);
                 contactname.remove(position);
                 adapter.notifyDataSetChanged();
                 dialog.dismiss();
                 str="del";
                 delete(name, number);

            }});

        no.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {


                edit(position);
                dialog.dismiss();

                }});
dialog.show();
DisplayMetrics metrics = getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels;
dialog.getWindow().setLayout((6 * width)/7, LayoutParams.WRAP_CONTENT);





    }

I use this but not work

LinearLayout background = (LinearLayout) findViewById(R.id.back);
background.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        SetBackground();
     // here I want to dismiss it after SetBackground() method 
      OptionDialog.dismiss();
    }
});

The method SetBackground() is undefined for the type new View.OnClickListener()

user3136171
  • 37
  • 1
  • 7
  • call last line inside `OptionDialog.dismiss();` method ...... – sushildlh Aug 11 '16 at 06:25
  • I don't think you need to attach a click listener for the background. The dialog should close automatically when clicked outside of its bounds. – Shaishav Aug 11 '16 at 06:25

1 Answers1

0

You can use dialog.setCanceledOnTouchOutside(true); which will close the dialog if you touch outside of the dialog.

For more info look at this

How to dismiss the dialog with click on outside of the dialog?

Community
  • 1
  • 1
Shanto George
  • 994
  • 13
  • 26