Nabukodonosor
Nabukodonosor

Reputation: 699

AlertDialog...no OK button, only Cancel

I want to show a alert dialog with OK and Cancel button but I only get Cancel. When I comment out cancel button i then get OK button. Weird. Anyway, here's the code:

final AlertDialog ukucajIme = new AlertDialog.Builder(Kviz.this).create();
            ukucajIme.setTitle("Insert your name");
            final EditText input = new EditText(Kviz.this);

            ukucajIme.setView(input);

            ukucajIme.setButton("Ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                  Editable ukucanoIme = input.getText();
                  finish();
                  }
                });
            ukucajIme.setButton("Cancel", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int whichButton) {
                     ukucajIme.dismiss();
                     finish();
                  }
                  });
            ukucajIme.show();

And question number two: whenever I try to use something like ukucajIme.setPositiveButton it gives me an error and says to change it to setButton. Why is that?

Upvotes: 1

Views: 1624

Answers (4)

Nabukodonosor
Nabukodonosor

Reputation: 699

OK, here's complete code. I just changed some variables.

AlertDialog.Builder builder = new AlertDialog.Builder(Kviz.this);
                     builder.setTitle("Ukucaj svoje ime");
                     final EditText input = new EditText(Kviz.this);
                     builder.setView(input);

                     builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                     public void onClick(DialogInterface dialog,int which) {
                         Editable ukucanoIme = input.getText();
                         finish();
                     }
                     });


                     builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                         public void onClick(DialogInterface dialog, int which) {
                              dialog.dismiss();
                              finish();
                         }
                     });
                     builder.show();
                     AlertDialog dialog = builder.create();

Upvotes: 0

JustDanyul
JustDanyul

Reputation: 14054

Try using the methods setPositiveButton() and setNegativeButton() on the AlertDialogBuilder like this

AlertDialog.Builder builder = new AlertDialog.Builder(Kviz.this);

builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
    Editable ukucanoIme = input.getText();
    finish();
});


builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
         dialog.dismiss();
         finish();
         dialog.cancel();
    }
});

AlertDialog dialog = builder.create();

Upvotes: 5

Ajay S
Ajay S

Reputation: 48612

Use like this.

Create alert dialog with two button. setPositiveButton() is used to create a positive button in alert dialog and setNegativeButton() is used to invoke negative button to alert dialog.

AlertDialog.Builder alertDialog = new AlertDialog.Builder(Kviz.this);
ukucajIme.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
       Editable ukucanoIme = input.getText();
       finish();
    }
});

// Setting Negative "Cancel" Button
ukucajIme.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
        finish();
        dialog.cancel();
    }
});

ukucajIme.show();  // Showing Alert Message

Upvotes: 1

dymmeh
dymmeh

Reputation: 22306

An alert dialog does not contain setNegativeButton or setPositiveButton. Instead use:

ukucajIme.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
    Editable ukucanoIme = input.getText();
    finish();
});


ukucajIme.setButton(AlertDialog.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
     ukucajIme.dismiss();
     finish();
});

Upvotes: 1

Related Questions