Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding positive / negative Button to DialogFragment's Dialog

I've already written a DialogFragment. Now I've realized that I want it to have a positive and a negative button just like an AlertDialog. How can I achieve such a thing while maintaining the code that I've written?

public class DoublePlayerChooser extends DialogFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);        

    setStyle(DialogFragment.STYLE_NORMAL,0);



}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    return new AlertDialog.Builder(getActivity())
            .setTitle("title")
            .setPositiveButton("OK",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        // do something...
                    }
                }
            )
            .setNegativeButton("Cancel",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        dialog.dismiss();
                    }
                }
            )
            .create();
}



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.doubleplayerchooser, container, false);
    getDialog().setTitle("Enter Players");

    firstPlayerPicker =  (ImageButton) v.findViewById(R.id.imageButton1);
    firstPlayerPicker.setOnClickListener(new OnClickListener() {
        public void onClick(final View v){

            callContactPicker(1);

        }       
    });

    secondPlayerPicker =  (ImageButton) v.findViewById(R.id.ImageButton01);
    secondPlayerPicker.setOnClickListener(new OnClickListener() {
        public void onClick(final View v){

            callContactPicker(2);

        }       
    });

    loadFromFile =  (ImageButton) v.findViewById(R.id.imageButton2);
    loadFromFile.setOnClickListener(new OnClickListener() {
        public void onClick(final View v){



        }       
    });

    firstTextfield =  (EditText) v.findViewById(R.id.editText1);
    secondTextfield =  (EditText) v.findViewById(R.id.EditText01);

    firstImage = (ImageView) v.findViewById(R.id.imageView1);
    secondImage = (ImageView) v.findViewById(R.id.ImageView01);



    return v;
}
like image 757
Libathos Avatar asked Sep 03 '13 20:09

Libathos


3 Answers

This is how I figured it out. I erased the onCreateView and altered the onCreateDialog. This link actually had the answer so all the credit should go there. I've just posted it just in case anyone bumps in this question first.

    @Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    AlertDialog.Builder b=  new  AlertDialog.Builder(getActivity())
    .setTitle("Enter Players")
    .setPositiveButton("OK",
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                // do something...
            }
        }
    )
    .setNegativeButton("Cancel",
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                dialog.dismiss();
            }
        }
    );

    LayoutInflater i = getActivity().getLayoutInflater();

    View v = i.inflate(R.layout.doubleplayerchooser,null);

    firstPlayerPicker =  (ImageButton) v.findViewById(R.id.imageButton1);
    firstPlayerPicker.setOnClickListener(new OnClickListener() {
        public void onClick(final View v){

            callContactPicker(1);

        }       
    });

    secondPlayerPicker =  (ImageButton) v.findViewById(R.id.ImageButton01);
    secondPlayerPicker.setOnClickListener(new OnClickListener() {
        public void onClick(final View v){

            callContactPicker(2);

        }       
    });

    loadFromFile =  (ImageButton) v.findViewById(R.id.imageButton2);
    loadFromFile.setOnClickListener(new OnClickListener() {
        public void onClick(final View v){



        }       
    });

    firstTextfield =  (EditText) v.findViewById(R.id.editText1);
    secondTextfield =  (EditText) v.findViewById(R.id.EditText01);

    firstImage = (ImageView) v.findViewById(R.id.imageView1);
    secondImage = (ImageView) v.findViewById(R.id.ImageView01);


    b.setView(v);
    return b.create();
}
like image 118
Libathos Avatar answered Oct 30 '22 17:10

Libathos


You have to override the DialogFragments onCreateDialog(...) method:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    return new AlertDialog.Builder(getActivity())
            .setTitle("title")
            .setPositiveButton("OK",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        // do something...
                    }
                }
            )
            .setNegativeButton("Cancel",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        dialog.dismiss();
                    }
                }
            )
            .create();
}

Taken from here: Android: disable DialogFragment OK/Cancel buttons

According to the error message you are getting ("request feature must be called...") I would recommend:

Don't call setContentView() before requestFeature() in your Activity or wherever it is you are calling it.

Furthermore:

Dont call setStyle(...) inside the onCreate().

Call it where you create your Fragment.

YourDialogFragment f = new YourDialogFragment(Context);
f.setStyle(...);
// and so on ...
like image 32
Philipp Jahoda Avatar answered Oct 30 '22 18:10

Philipp Jahoda


The clearest way.

// Your own onCreate_Dialog_View method
public View onCreateDialogView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.your_layout, container); // inflate here
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    // do something with 'view'
}

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // setup dialog: buttons, title etc
    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity())
            .setTitle(R.string.dialog_fragment_author_title)
            .setNegativeButton(R.string.dialog_fragment_author_close,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            dialog.dismiss();
                        }
                    }
            );

    // call default fragment methods and set view for dialog
    View view = onCreateDialogView(getActivity().getLayoutInflater(), null, null);
    onViewCreated(view, null);
    dialogBuilder.setView(view);

    return dialogBuilder.create();
}
like image 4
Alexandr Avatar answered Oct 30 '22 17:10

Alexandr