Skip to content Skip to sidebar Skip to footer

I Want To Show Ok And Cancel Button In My Alert Dialog?

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

Solution 1:

AlertDialog.Builderadb=newAlertDialog.Builder(this);
adb.setView(alertDialogView);
adb.setTitle("Title of alert dialog");
adb.setIcon(android.R.drawable.ic_dialog_alert);
adb.setPositiveButton("OK", newDialogInterface.OnClickListener() {
    publicvoidonClick(DialogInterface dialog, int which) {
        EditTextet= (EditText)alertDialogView.findViewById(R.id.EditText1);
        Toast.makeText(Tutoriel18_Android.this, et.getText(),
                Toast.LENGTH_SHORT).show();
    }
});
adb.setNegativeButton("Cancel", newDialogInterface.OnClickListener() {
    publicvoidonClick(DialogInterface dialog, int which) {
        finish();
    }
});
adb.show();

Solution 2:

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

AlertDialogalertDialog=newAlertDialog.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", newDialogInterface.OnClickListener() {
            publicvoidonClick(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();

Solution 3:

protectedfinal Dialog onCreateDialog(finalint id) {
    Dialogdialog=null;
    switch (id) {
    case DIALOG_ID:
        AlertDialog.Builderbuilder=newAlertDialog.Builder(this);
        builder.setMessage(
                "some message")
                .setCancelable(false)
                .setPositiveButton("ok",
                        newDialogInterface.OnClickListener() {
                            publicvoidonClick(DialogInterface dialog,
                                    int id) {
                                //to perform on ok


                            }
                        })
                .setNegativeButton("cancel",
                        newDialogInterface.OnClickListener() {
                            publicvoidonClick(DialogInterface dialog,
                                    int id) {

                                //dialog.cancel();
                            }
                        });
        AlertDialogalert= builder.create();
        dialog = alert;
        break;

    default:

    }
    return dialog;
}

you can call this from anywhere like:

showDialog(DIALOG_ID);

Post a Comment for "I Want To Show Ok And Cancel Button In My Alert Dialog?"