1

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?

Nabukodonosor
  • 699
  • 1
  • 8
  • 20

4 Answers4

5

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();
JustDanyul
  • 13,813
  • 7
  • 53
  • 71
  • 1
    to be fair i never said that they where intended to be called on the actual dialog instance, I'll update my answer :) – JustDanyul Mar 07 '13 at 18:33
  • I get error for dismiss();. Add cast to 'builder'...(I changed it, you forgot, it was still my ukucajIme). And when I add cast to it, the alert windows doesn't show. – Nabukodonosor Mar 07 '13 at 19:05
  • ah that makes sence, i just copied your code from the actual event handlers :) change it to the appropriate instance names (you shouldn't call the dismiss on the builder btw, if that was what you meant). And another note, you shouldn't call both dismiss and cancel in the same block. Choose one -> http://stackoverflow.com/questions/3125647/what-is-the-difference-between-a-dialog-being-dismissed-or-canceled-in-android – JustDanyul Mar 07 '13 at 19:12
  • With your edited code nothing happens, alert window never popup. I've inserted builder.show(); but my editbox is gone. OK, this starts to become weird. – Nabukodonosor Mar 07 '13 at 20:01
  • It worked now. With some corrections. I will post bellow complete code. if someone else has this problem. Thanks – Nabukodonosor Mar 07 '13 at 20:08
1

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
Ajay S
  • 48,003
  • 27
  • 91
  • 111
  • `ukucajIme` is an AlertDialog, not an AlertDialog.Builder so it doesn't have these methods. – Sam Mar 07 '13 at 18:32
1

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();
});
dymmeh
  • 22,247
  • 5
  • 53
  • 60
  • +1 and to answer the author's second question directly, `setPositiveButton()` (etc) are methods in [AlertDialog.Builder](https://developer.android.com/reference/android/app/AlertDialog.Builder.html). – Sam Mar 07 '13 at 18:37
0

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();
Nabukodonosor
  • 699
  • 1
  • 8
  • 20