Do I want to show the OK and Cancel buttons in the notification dialog?

I want to show ok and cancel the button in my alert dialog box. I tried many solutions, but didnt successe..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();} 
+5
source share
3 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();
+38
source

The following code will create a simple one-button dialog box. The following code setTitle()uses the method to set the Title to alert dialog. setMessage()used to set the message dialog box. setIcon()- set the icon in the notification dialog box

    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();
+6
source
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;
}

, :

      showDialog(DIALOG_ID);
+4

All Articles