không giống như toast cái mà tự động close sau 1-2s, thì dialog default sẽ không tự động close và ko có bất kỳ 1 setting nào của nó tự động close.
trong trường hợp này, nếu chúng ta muốn tự động close dialog sau 1 khoảng thời gian thì chúng ta có thể sử dụng timer để xử lý điều này:

đây là source code của app:

package android.vn.autoclosedialog;

import java.util.timer;
import java.util.timertask;
import android.os.bundle;
import android.app.activity;
import android.app.alertdialog;
import android.view.view;
import android.widget.button;


public class mainactivity extends activity {

@override
public void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_main);

// get button
button btnshow = (button)findviewbyid(r.id.btnshowdialog);
btnshow.setonclicklistener(new view.onclicklistener() {

@override
public void onclick(view v) {
alertdialog.builder builder = new alertdialog.builder(v.getcontext());
builder.settitle("auto-closing dialog");
builder.setmessage("after 5 second, this dialog will be closed automatically!");
builder.setcancelable(true);

final alertdialog dlg = builder.create();

dlg.show();

final timer t = new timer();
t.schedule(new timertask() {
public void run() {
dlg.dismiss(); // when the task active then close the dialog
t.cancel(); // also just top the timer thread, otherwise, you may receive a crash report
}
}, 5000); // after 5 second (or 5000 miliseconds), the task will be active.

}
});
}

}


các bạn cũng có thể download toàn bộ source code của app tại đây:
autoclosedialog.rar
password giải nén là: http://android.vn