Reputation: 779
I have an AlertDialog without buttons. How do I dismiss the dialog when clicking on it?
Upvotes: 1
Views: 3686
Reputation: 325
If you want your custom Dialog to be dismissable by clicking on the dialog and outside the dialog use following code:
private void showDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = (LayoutInflater)getSystemService (Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.your_custom_dialogLayout, null);
builder.setView(v);
builder.setCancelable(true);//Dialog dismissed by click outside
final AlertDialog dialog = builder.create();
v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss(); //Dialog dismissed by click on dialogs content
}
});
dialog.show();
}
Upvotes: 0
Reputation: 53
This closes the dialog on users touch outside the dialog, so no buttons needed:
dialog.setCanceledOnTouchOutside(true);
Upvotes: 1
Reputation: 4842
what I can think about this problem is to set your own view to that dialog, and then you can set a onClickListener to that view, so you can deal with the click event. Below is my code:
@Override
protected Dialog onCreateDialog(int id, Bundle args) {
AlertDialog.Builder builder=new Builder(this);
builder.setTitle("title");
LayoutInflater inflater=(LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view=inflater.inflate(R.layout.firstview, null);//inflate your own view
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
YourActivity.this.dismissDialog(dialog_ID);//dismiss the dialog
}
});
builder.setView(view);//set your custom view to your dialog
return builder.create();
}
Upvotes: 3
Reputation: 949
you have to create custom dialog for it. see the below link it will help you
http://iserveandroid.blogspot.in/2010/11/how-to-dismiss-custom-dialog-based-on.html
you also closed dialog after specific time. see the below link.
http://xjaphx.wordpress.com/2011/07/13/auto-close-dialog-after-a-specific-time/
Upvotes: 0