8

I have seen several posts on how to dismiss a dialog by clicking on the outside. But is there a way to get the same functionality by clicking the inside the dialog window?

Are there any listeners for the Dialog that would detect a tap on the Dialog Window?

Ahmed Faisal
  • 4,397
  • 12
  • 45
  • 74

6 Answers6

14

Overriding Dialog.onTouchEvent(...) catches any tap, anywhere on the screen. To dismiss the dialog by tapping anywhere:

Dialog dialog = new Dialog(this) {
  @Override
  public boolean onTouchEvent(MotionEvent event) {
    // Tap anywhere to close dialog.
    this.dismiss();
    return true;
  }
};

This snippet nullifies the need to call dialogObject.setCanceledOnTouchOutside(true);.

Jacob Marble
  • 28,555
  • 22
  • 67
  • 78
3

Presumably you want to detect a touch event anywhere within the bounds of a dialog. If you're creating a custom dialog (i.e. by assembling a set of Views into a layout View of some sort, and then setting the parent View as the dialog's main content view using .setContentView()) then perhaps you could simply set an onTouch listener to that content parent View. Furthermore, you could grab hold of views using mDialog.findViewById(), so if for example you were using an AlertDialog, perhaps you could determine somehow what resource ID to use to grab hold of its main layout View.

Trevor
  • 10,903
  • 5
  • 61
  • 84
1

If you have a Layout in your Dialog, you could get a reference to that as view, and put a onClickListener on that. So assuming your dialog has a custom layout, and view for the entire dialog, get a reference to that.

For instance, assuming a dialog has a LinearLayout named mainll, that contains your custom views, you would:

LinearLayout ll - (LinearLayout) findViewById(R.id.mainll);
ll.setOnClickListener(...) { onClick()... }

Then anytime anything is clicked within the LinearLayout, it will register a click event.

Booger
  • 18,579
  • 7
  • 55
  • 72
0

Here is an example which explains how to handle onTouch events within the dialog. The trick is in creating a custom listener.

http://about-android.blogspot.co.uk/2010/02/create-custom-dialog.html

Lumis
  • 21,517
  • 8
  • 63
  • 67
0
here i have taken my close icon ,if u need u can take anything like button

first of all u have implement to the class

class somethingclass Dialog implements View.OnClickListener

then set the event for particular 

      icon_close.setOnClickListener(this);

then override the class function

    @Override
    public void onClick(View v) {
        if(R.id.icon_close==v.getId()){
            dismiss();
        }else
}


Note:  if passible u can give dilaog.dismiss();
Issac Balaji
  • 1,441
  • 1
  • 13
  • 25
0

You can always create your own Dialog activity and call finish() when the user clicks the area that you want to close your Dialog.

Paul Nikonowicz
  • 3,883
  • 21
  • 39
  • Thank you for the answer. But I want to have the Dialog hover over my current activity (there is a transparent area over & beneath the Dialog from which my current activity is displayed). TO eloborate, the Dialog is displayed when a button in my activity is clicked. I'm able to dismiss the Dialog by tapping on the region outside the Dialog using dialog.setCanceledOnTouchOutside(true); According to the reks, I can't use any buttons on the Dialog! Hence, I'm trying to search for a way that would dismiss the Dialog when it is clicked. – Ahmed Faisal Feb 07 '12 at 21:30
  • You can use any layout you want with an activity; including one that is transparent. It will look just like a dialog box and you can make it behave any way that you want. If you really want something that looks and behaves like a modal dialog box but with custom dismiss logic, this will work. – Paul Nikonowicz Feb 07 '12 at 21:33