Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want to show OK and cancel button in my alert dialog? [duplicate]

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();} 
like image 361
user2033660 Avatar asked Feb 22 '13 09:02

user2033660


People also ask

How do I turn off alerts in dialog?

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.

Which types of button can be set for alert dialog?

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.

How many buttons can an alert dialog display?

AlertDialog. A dialog that can show a title, up to three buttons, a list of selectable items, or a custom layout.

How do you dismiss dialog with click on outside of dialog?

You can use dialog. setCanceledOnTouchOutside(true); which will close the dialog if you touch outside of the dialog. Window window = this.


2 Answers

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();
like image 82
Nicolas Avatar answered Sep 30 '22 10:09

Nicolas


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);
like image 25
Shiv Avatar answered Sep 30 '22 10:09

Shiv