prom85
prom85

Reputation: 17868

Android DialogFragment - using onCreateView AND adding dialog buttons to the dialog possible?

Because I want to use the FragmentManager in a dialog, I use onCreateView instead of onCreateDialog.

If using this approach, I'm able to set the title of the dialog, but I can't find out how to set dialog buttons. Normally I would do this in createDialog with an AlertDialog...

Question: Is it possible to use the onCreateView approach and use dialog buttons? Or can only do this, if I add the buttons manually to my view resource?

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    View v = inflater.inflate(R.layout.dialog_exercise_info, null);
    // I could include the dialog buttons in the view
    // but can I somehow add buttons to the dialog directly?
    return v;
}

@Override
protected Dialog createDialog(final Bundle savedInstanceState, Activity activity)
{
    Dialog dialog = new Dialog(getActivity(), getTheme());
    dialog.setTitle(R.string.title_create_exercise);
    return dialog;
}

Upvotes: 2

Views: 689

Answers (1)

ucsunil
ucsunil

Reputation: 7504

Yes and no... if you are not using the AlertDialog, then you have to manually create the dialog buttons in your view resource. By saying you have to manually create them in your view resource, I mean that you have to either put the in the xml layout file or you have to create and add them in code within onCreateView() which is acceptable according to your question.

The convenience methods used to directly set the buttons (positive and negative) in AlertDialog are not part of the DialogFragment. You could create your own convenience methods but in that case, you are going back to doing them manually.

Upvotes: 1

Related Questions