How to dismiss a Dialog in Android by clicking it inside?
Asked Answered
M

6

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?

Mousebird answered 7/2, 2012 at 21:18 Comment(0)
E
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);.

Erinn answered 1/4, 2012 at 21:57 Comment(0)
S
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.

Scribner answered 7/2, 2012 at 21:27 Comment(0)
B
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.

Bebop answered 7/2, 2012 at 21:25 Comment(0)
A
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.

Algonquian answered 7/2, 2012 at 21:21 Comment(2)
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.Mousebird
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.Algonquian
I
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

Isidora answered 5/10, 2012 at 13:52 Comment(0)
H
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();
Hambletonian answered 20/8, 2014 at 11:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.