Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Layout for DialogFragment OnCreateView vs. OnCreateDialog

I'm trying to create a DialogFragment using my own Layout.

I've seen a couple different approaches. Sometimes the layout is set in OnCreateDialog like this: (I'm using Mono but I've gotten somewhat used to Java)

public override Android.App.Dialog OnCreateDialog (Bundle savedInstanceState) {     base.OnCreateDialog(savedInstanceState);     AlertDialog.Builder b = new AlertDialog.Builder(Activity);         //blah blah blah     LayoutInflater i = Activity.LayoutInflater;     b.SetView(i.Inflate(Resource.Layout.frag_SelectCase, null));     return b.Create(); } 

This first approach works for me... until I want to use findViewByID. so after a bit of googling I tried the second approach which involves overriding OnCreateView

So I commented out two lines of OnCreateDialog that set the Layout and then added this:

public override Android.Views.View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {     View v = inflater.Inflate(Resource.Layout.frag_SelectCase, container, false);         //should be able to use FindViewByID here...     return v; } 

which gives me a lovely error:

11-05 22:00:05.381: E/AndroidRuntime(342): FATAL EXCEPTION: main 11-05 22:00:05.381: E/AndroidRuntime(342): android.util.AndroidRuntimeException: requestFeature() must be called before adding content 

I'm stumped.

like image 261
gghuffer Avatar asked Nov 06 '12 18:11

gghuffer


People also ask

What is the difference between dialog & DialogFragment?

Dialog: A dialog is a small window that prompts the user to make a decision or enter additional information. DialogFragment: A DialogFragment is a special fragment subclass that is designed for creating and hosting dialogs.

What is DialogFragment in android?

Android DialogFragments. DialogFragment is a utility class which extends the Fragment class. It is a part of the v4 support library and is used to display an overlay modal window within an activity that floats on top of the rest of the content. Essentially a DialogFragment displays a Dialog but inside a Fragment.

When to use dialog fragment?

A DialogFragment can still optionally be used as a normal fragment, if desired. This is useful if you have a fragment that in some cases should be shown as a dialog and others embedded in a larger UI. Show activity on this post.

How do you show DialogFragment?

Showing the DialogFragment It is not necessary to manually create a FragmentTransaction to display your DialogFragment . Instead, use the show() method to display your dialog. You can pass a reference to a FragmentManager and a String to use as a FragmentTransaction tag.


1 Answers

I had the same exception with the following code:

public class SelectWeekDayFragment extends DialogFragment {      @Override     public Dialog onCreateDialog(Bundle savedInstanceState) {         return new AlertDialog.Builder(getActivity())         .setMessage("Are you sure?").setPositiveButton("Ok", null)         .setNegativeButton("No way", null).create();     }      @Override     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {         View view = inflater.inflate(R.layout.week_day_dialog, container, false);          return view;         } } 

You must choose to override only one of onCreateView or onCreateDialog in a DialogFragment. Overriding both will result in the exception: "requestFeature() must be called before adding content".

Important

For complete answer check the @TravisChristian comment. As he said, you can override both indeed, but the problem comes when you try to inflate the view after having already creating the dialog view.

like image 84
Xavier Egea Avatar answered Oct 28 '22 08:10

Xavier Egea