Reputation: 4427
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?
Upvotes: 8
Views: 9584
Reputation: 1441
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();
Upvotes: 0
Reputation: 21639
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
Upvotes: 0
Reputation: 30132
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);
.
Upvotes: 14
Reputation: 10993
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 View
s 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
.
Upvotes: 3
Reputation: 18725
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.
Upvotes: 1
Reputation: 3903
You can always create your own Dialog activity and call finish() when the user clicks the area that you want to close your Dialog.
Upvotes: 0