Search code examples
androidandroid-fragmentsdialogfragment

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.


Solution

  • 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();
    }