Search code examples
androidandroid-alertdialogmaterial-components-androidmaterial-components

How to change MaterialAlertDialog text color properly?


I try to use widgets from Material Components only, but in many cases, it's not documented how styling can be achieved.

Let's consider MaterialAlertDialog.

Each time I want to show a dialog, I call such part of the code:

MaterialAlertDialogBuilder(context, R.style.Theme_MyApp_Dialog_Alert)
    .setTitle("Title")
    .setMessage("This is message.")
    .setPositiveButton(R.string.ok) { _, _ -> }
    .show()

As you can see, I'm using a custom theme.

<style name="Theme.MyApp.Dialog.Alert" parent="ThemeOverlay.MaterialComponents.MaterialAlertDialog">
    <!-- attributes here -->
</style>

The problem is that some of the attributes are not working. For example textColor. So the question is how to change the body or title text color in MaterialAlertDialog?

I use the newest version of Material Components - 1.1.0-alpha07.

PS.

I'm not sure which theme should be a parent. In Material Theme Builder they use @style/ThemeOverlay.MaterialComponents.Dialog.Alert which actually gives an "old" look of dialogs.


Solution

  • Change the style as below

    <style name="Theme.MyApp.Dialog.Alert" parent="ThemeOverlay.MaterialComponents.MaterialAlertDialog">
        <item name="materialAlertDialogTitleTextStyle">@style/MaterialAlertDialogText</item>
    </style>
    

    Create MaterialAlertDialogText style and set textColor

    <style name="MaterialAlertDialogText" parent="@style/MaterialAlertDialog.MaterialComponents.Title.Text">
        <item name="android:textColor">@color/yourTextColor</item>
    </style>