I want to show ok and cancel button in my alert dialog.I tried many solutions but didnt succeede..guide me plese..thanks
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Check Your Internet Connection!! you are not connected to the Internet..");
AlertDialog alert = builder.create();
alert.show();}
AlertDialog generally consists of the main title, the message, and two buttons, technically termed as a positive button and a negative button. Both positive and negative buttons can be programmed to perform various actions. By default, the negative button lets close the AlertDialog without any additional lines of code.
They are Positive, Negative and Neutral action buttons. An alert dialog can have maximum three action buttons. If you want the user to accept the action, use Positive action button. It is normally displayed as OK/YES.
AlertDialog. A dialog that can show a title, up to three buttons, a list of selectable items, or a custom layout.
You can use dialog. setCanceledOnTouchOutside(true); which will close the dialog if you touch outside of the dialog. Window window = this.
AlertDialog.Builder adb = new AlertDialog.Builder(this);
adb.setView(alertDialogView);
adb.setTitle("Title of alert dialog");
adb.setIcon(android.R.drawable.ic_dialog_alert);
adb.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
EditText et = (EditText)alertDialogView.findViewById(R.id.EditText1);
Toast.makeText(Tutoriel18_Android.this, et.getText(),
Toast.LENGTH_SHORT).show();
}
});
adb.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
adb.show();
protected final Dialog onCreateDialog(final int id) {
Dialog dialog = null;
switch (id) {
case DIALOG_ID:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(
"some message")
.setCancelable(false)
.setPositiveButton("ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
//to perform on ok
}
})
.setNegativeButton("cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
//dialog.cancel();
}
});
AlertDialog alert = builder.create();
dialog = alert;
break;
default:
}
return dialog;
}
you can call this from anywhere like:
showDialog(DIALOG_ID);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With