Mike
Mike

Reputation: 6839

trouble setting dialog positive button

I create my Dialog as:

// custom dialog
Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.add_taste_dialog);       
dialog.setTitle("Add Taste");

then I try and set the positive button with:

dialog.setPositiveButton(R.string.addtaste, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {




            }
        });

Eclipse give me this error:

The method setPositiveButton(int, new DialogInterface.OnClickListener(){}) is undefined for the type Dialog

I was following the android developers references here:

http://developer.android.com/guide/topics/ui/dialogs.html

Upvotes: 0

Views: 1096

Answers (2)

Bhoomika Brahmbhatt
Bhoomika Brahmbhatt

Reputation: 7415

If u r using custom xml layout for dialog. then why don't u not putting Positive button in your custom layout? Just put button in dialog xml file and do the stuff on its click event.

Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
        // if button is clicked, close the custom dialog
        dialogButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

Upvotes: 1

AbsentMoniker
AbsentMoniker

Reputation: 35

As the error message indicates, the setPositiveButton method is not defined for the Dialog class. However, it is defined for the AlertDialog.Builder class:

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Add Taste")
builder.setPositiveButton(R.string.addtaste, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
          //code goes here
        }
}
AlertDialog dialog = builder.create()

If the layout you provide to your dialog is more than a standard AlertDialog can accomodate, you can use your current code combined with the dialog class's findViewById(int id) method to find your button, provided that you have included a button in the layout you add. Otherwise, you can add a button using the addContentView(View view, ViewGroup.LayoutParams params) method.

Here is the reference for the dialog class to investigate those methods: http://developer.android.com/reference/android/app/Dialog.html

Good luck!

Upvotes: 0

Related Questions