I have the following code which displays a dialog box to the user if no network connection is detected.
private void createNoNetworkDialog() {
LayoutInflater inflater = LayoutInflater.from(this);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
View view = inflater.inflate(R.layout.offline_mode_dialog,null);
builder.setView(view);
builder.show();
}
There are two buttons in this dialog which have methods defined for their onClick actions. I would like to close the dialog pop-up after either of these button is pressed. Any ideas??
Yes,call
dismiss()from theListener‘sonClicksince theDialogInterfacereference is passed, which allows for dismissal.Eg
Or if your buttons are inside the layout, show the dialog and keep a reference to it (
final AlertDialog dialog = builder.show()). Then usedialog.findViewById()to find the respective buttons. Assign a normalView.OnClickListenerand in it calldismiss()using the dialog reference you’re holding.