I want an AppCompat-styled dialog - background, title, button position, button tint etc. - containing a Fragment.
With a custom DialogFragment
onCreateDialog()
can return an AppCompatDialog
, then in onCreateView()
a new layout can be inflated and childFragmentManager
can be used to replace a placeholder view with the Fragment. But the resulting dialog is otherwise an empty box with no buttons. They can be manually added to the dialog's layout but they get no styling from AppCompat.
The same applies if you make the target Fragment extend DialogFragment
.
To get AppCompat buttons you can use AlertDialog.Builder
, and use .setView()
to set the layout, but then I can't find a way to set the Fragment as the content. If you try childFragmentManager.beginTransaction().replace()
from onCreateDialog()
then you get:
java.lang.IllegalStateException: Fragment does not have a view
This seems to be because, even after using builder.setView()
or dialog.create()
then dialog.setView()
, onCreateView()
and getView()
always return null.
The second answer here seems to cover this quite comprehensively but doesn't suggest a favourable solution: https://stackoverflow.com/a/21418952/2645845
If you override onCreateView()
or getView()
to return the View you inflated then you get:
android.util.AndroidRuntimeException: Window feature must be requested before adding content
at android.support.v7.app.AppCompatDelegateImplV7.throwFeatureRequestIfSubDecorInstalled(AppCompatDelegateImplV7.java:1479)