14

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();} 
user2033660
  • 223
  • 1
  • 3
  • 9

3 Answers3

50
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();
Vukašin Manojlović
  • 3,717
  • 3
  • 19
  • 31
Nicolas
  • 5,249
  • 3
  • 19
  • 20
  • Nicolas> How would you change background color of this dialog box? Or an y other simple way to make it look a bit beautiful? – Jasper Sep 29 '15 at 08:59
  • The *negative* event can also just be set to `null` : http://stackoverflow.com/a/25504486/756976 – skofgar Dec 16 '15 at 18:04
  • 1
    Better use `android.R.string.ok` and `android.R.string.cancel` instead of hard-coded strings "OK" and "Cancel" – Vivek Apr 06 '19 at 08:28
7

The following code will create a simple alert dialog with one button. In the following code setTitle() method is used for set Title to alert dialog. setMessage() is used for setting message to alert dialog. setIcon() is to set icon to alert dialog

    AlertDialog alertDialog = new AlertDialog.Builder(
                    AlertDialogActivity.this).create();

    // Setting Dialog Title
    alertDialog.setTitle("Alert Dialog");

    // Setting Dialog Message
    alertDialog.setMessage("Welcome to AndroidHive.info");

    // Setting Icon to Dialog
    alertDialog.setIcon(R.drawable.tick);

    // Setting OK Button
    alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
            // Write your code here to execute after dialog closed
            Toast.makeText(getApplicationContext(), "You clicked on OK", Toast.LENGTH_SHORT).show();
            }
    });

    // Showing Alert Message
    alertDialog.show();
MuraliGanesan
  • 3,233
  • 2
  • 16
  • 22
5
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);
Shiv
  • 4,569
  • 4
  • 25
  • 39