rupps rupps - 1 year ago 145
Android Question

AppCompatDialog: Impossible to change Text Color

I am struggling with changing the TEXT color of AppCompat DialogFragments.

My App uses a DARK theme (

), but for dialogs I want a LIGHT theme. I am using Build Tools, Support Library and compileSdkVersion to 25, shall it matters.

I am able to change everything else on the dialog (title, background, window background) but not the primary and accented text colors, that keep on using the (white) settings for the dark theme, resulting in white text over white background.

I've tried dozens of solutions in similar questions here in SO, like:

1) The easy one: On the styles.xml:

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<item name="alertDialogTheme">@style/AppCompatAlertDialogStyle</item>
<item name="android:alertDialogTheme">@style/AppCompatAlertDialogStyle</item>

<style name="AppCompatAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<!-- ignored !!!! -->
<item name="colorPrimary">#ff0000</item>
<!-- ignored !!!! -->
<item name="colorPrimaryDark">#ff0000</item>
<!-- ignored !!!! -->
<item name="colorAccent">#ff0000</item>
<!-- ignored !!!! -->
<item name="android:textColorPrimary">#F040FF</item>
<!-- ignored !!!! -->
<item name="android:textColor">#F040FF</item>


With this solution, the background and buttons style from
IS APPLIED, but not the text colors as you can see in the screenshot:

enter image description here

2) Manually specifying the style on the AlertDialog Creation:

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(),;
LayoutInflater inflater = getActivity().getLayoutInflater();
View hostView = mHostView = inflater.inflate(layoutId, null);

Same problem. Light background, Light text.

3) Using a ContextWrapper:

ContextThemeWrapper ctw = new ContextThemeWrapper(getActivity(),;
AlertDialog.Builder builder = new AlertDialog.Builder(ctw);

Nothing :( Same thing happens

4) Manually specifying other exotic constants I came across the many posts here in SO, like


This was just a desperate attempt, but anyways the text is not changed

5) Specifying the style in the fragment rather than on the dialog

Dialog_Meta newFragment = new Dialog_Meta();
newFragment.setStyle(DialogFragment.STYLE_NORMAL,;, TAG);

I used this solution time ago in a very old API version, can't remember what was the problem, but anyways, doesn't solve the present issue :(

Can anybody tell me what's going on?

Answer Source

The problem here is the custom View you're setting on the AlertDialog. Though you've set a certain theme for AlertDialogs in general, that View is being inflated with the Activity's theme, which doesn't have those overridden color attribute values.

One option is to create a ContextThemeWrapper around the Activity's Context with the custom, and obtain the LayoutInflater.from() that.

However, the simplest solution for this particular situation seems to be setting the android:theme attribute on the root View of the Dialog's layout. For example:

<LinearLayout xmlns:android=""
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download