Alex Irabor
Alex Irabor

Reputation: 401

Creating an android DialogFragment in eclipse

Hello i am trying to create a DialogFragment in Android. The rest of the code works well- i think; except for when i call the AlertDialog.Builder() method, i get an error that says:"the method Builder is undefined for the type AlertDialog. here is my entire code, i hope that someone can help me fix it. Here is my code below:

`

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;

public class DialogFragmentEx extends DialogFragment {

    static DialogFragmentEx newInstance(String title) {
        DialogFragmentEx fragment = new DialogFragmentEx();
        Bundle args = new Bundle();
        args.putString("title", title);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        String title = getArguments().getString("title");

        return AlertDialog.Builder(getActivity())
                .setIcon(R.drawable.ic_launcher)
                .setTitle(title)
                .setPositiveButton("OK",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            ((DialogFragmentExampleActivity)
                            getActivity()).doPositiveClick();
                    }
                })
                .setNegativeButton("Cancel",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            ((DialogFragmentExampleActivity) getActivity()).doNegativeClick();
                        }
                }).create();
    }
}

`

Upvotes: 1

Views: 131

Answers (2)

Xcihnegn
Xcihnegn

Reputation: 11597

You forgot initiating Builder, so change your codes to:

return new AlertDialog.Builder(getActivity())
       .setIcon(R.drawable.ic_launcher)
         ... ...

       ).create();

Hope this help!

Upvotes: 0

Jitender Dev
Jitender Dev

Reputation: 6925

You don't need to build your Dialog when you are extending to DialogFragment. Your code is good enough.

You only need to use below code to display your dialog.

DialogFragmentEx alertdFragment = new DialogFragmentEx ();
// Show Alert DialogFragment
alertdFragment.show(context, "Alert Dialog Fragment");

Check this Tutorial for more information.

Upvotes: 1

Related Questions