SpecialSnowflake
SpecialSnowflake

Reputation: 995

DialogFragment onCreateView not returning custom layout view?

I'm currently trying to use a custom layout with my DialogFragment, however it seems I'm doing something wrong. This is what my class currently looks like:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    setRetainInstance(true);
    Log.d(TAG, "onCreateDialog");
    return new AlertDialog.Builder(getActivity())
            .setPositiveButton(android.R.string.ok, passDataListener)
            .setNegativeButton(android.R.string.cancel, null)
            .setCancelable(true)
            .create();
}

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    Log.d(TAG, "onCreateView");
    setTitleFromBundle();
    return inflater.inflate(R.layout.dialog_edittext, container);


}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    mEditText = (EditText) view.findViewById(R.id.dialog_edittext);
    tvUnits = (TextView) view.findViewById(R.id.dialog_units);

    setMaxCharsFromBundle();
    setTextFromBundle();
    setHintFromBundle();
    setInputTypeFromBundle();
    setUnitsTextViewFromBundle();
}

The positive/negative buttons show (along with the title) however, my layout does not.

Upvotes: 0

Views: 2777

Answers (3)

Shubham
Shubham

Reputation: 531

don't need to override onCreateView()

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
setRetainInstance(true);
Log.d(TAG, "onCreateDialog");
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
return new AlertDialog.Builder(getActivity())
        .setView(inflater.inflate(R.layout.dialog_edittext, null))
        .setPositiveButton(android.R.string.ok, passDataListener)
        .setNegativeButton(android.R.string.cancel, null)
        .setCancelable(true)
        .create();
}

Upvotes: 2

MatPag
MatPag

Reputation: 44813

You should implement only one method between onCreateView and onCreateDialog, not both of them.

  • onCreateView : to supply the content of the dialog
  • onCreateDialog : to create an entirely custom dialog, such as an AlertDialog, with its own content

Everything is described in the documentation here

For a complete guide you can see https://guides.codepath.com/android/Using-DialogFragment

Upvotes: 0

beeb
beeb

Reputation: 1615

You can't use both methods onCreateDialog and onCreateView at the same time. You have to choose if you want to show a "basic dialog" then overwrite onCreateDialog. If you want to show a "custom dialog" then overwrite onCreateView. Note that you can also overwrite onCreateDialog and set your own layout with youralertdialog.setView().

Upvotes: 0

Related Questions