I need to programmatically create a DialogFragment layout, but I don’t know how to proceed. I cannot use xml layout because the DialogFragment will be a part of closed-source JAR file.
Normal Dialogs accept an Activity in their constructor, so it is then possible to instantiate a new Layout like this: layout = new LinearLayout(this);. DialogFragments, on the other hand, do not usually take the Activity as a parameter, so I don’t know how to perform this first step.
- How should I create the layout?
- Is it OK to ask for an Activity in the constructor?
- Is creating a layout of DialogFragment any different from creating a layout of normal Dialog?
I will be grateful for any other advice regarding manual DialogFragment design.
How ever you want it to look. If you can’t/don’t want to use a layout file that you inflate and return from the
onCreateViewmethod you’ll have to build the entire view hierarchy of your new dialog in code.There is no need for this, the
Fragmentwill get a reference to anActivity, you’ll have a reference to that context by usinggetActivity().No, it’s not different. The documentation for the
DialogFragmenthas a great example on how to build a customDialogFragment, you should check it out.