Theme not applying to DialogFragment on Android

I just lost a lot of time to this, but I finally found a way to do this entirely in xml.

In an application theme, Dialogs are actually themed separately. So to style all DialogFragments with green buttons and green EditText hints, you would make a style like this:

<style name="DialogTheme" parent="@android:style/Theme.Holo.Light.Dialog">
    <item name="android:buttonStyle">@style/button_green</item>
    <item name="android:textColorHint">@color/green</item>
</style>

Then add this theme to your application theme as the dialogTheme

<style name="MyTheme" parent="android:Theme.Holo.Light">
    <item name="android:dialogTheme">@style/DialogTheme</item>
</style>

Many thanks to whoever wrote this post for showing me the path to what I'd been searching for!


You shoudn't use the AlertDialog.Builder(Context, int) constructor with the support library because it is only available since API 11.

To setup a theme to your dialog, use instead a ContextThemeWrapper like this:

ContextThemeWrapper context = new ContextThemeWrapper(getActivity(), android.R.style.Theme_Holo_Dialog_NoActionBar);
AlertDialog.Builder builder = new AlertDialog.Builder(context);

I believe you need to set the theme on the actual Dialog and not the Fragment

Use this constructor to create your AlertDialog:

 AlertDialog.Builder(Context context, int theme)

ie

 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), theme)