Search code examples
androidmaterial-components-androidandroid-datepicker

How to change the dialog's button colors for a Material Date picker


I'm using Material components to create a DatePicker. From what I understand, colorPrimary is used to determine the color of the section at the top of the dialog, as well as the colors for the text on the buttons, specifically CANCEL and OK.

Going through the documentation, I can't seem to find a way to change just the color of the buttons at the bottom ? Is it possible to do that ?

current code:

<style name="ThemeOverlay.App.DatePicker" parent="@style/ThemeOverlay.MaterialComponents.MaterialCalendar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorOnPrimary">@android:color/white</item>
    <item name="shapeAppearanceSmallComponent">@style/ShapeAppearance.App.SmallComponent</item>
    <item name="shapeAppearanceMediumComponent">@style/ShapeAppearance.App.MediumComponent
    </item>
    <item name="materialCalendarHeaderTitle">@style/MyHeaderTitle</item>
    <item name="colorOnPrimarySurface">@android:color/white</item>
</style>

<style name="MyHeaderTitle" parent="Widget.MaterialComponents.MaterialCalendar.HeaderTitle">
    <item name="android:textColor">@android:color/white</item>
</style>

<style name="ThemeMaterialCalendarTextButton" parent="Widget.MaterialComponents.Button.TextButton.Dialog.Flush">
    <item name="android:textColor">@android:color/white</item>
    <item name="iconTint">@android:color/white</item>
</style>

<style name="ShapeAppearance.App.SmallComponent" parent="ShapeAppearance.MaterialComponents.SmallComponent">
    <item name="android:textSize">24sp</item>
    <item name="cornerSize">16dp</item>
</style>

<style name="ShapeAppearance.App.MediumComponent" parent="ShapeAppearance.MaterialComponents.MediumComponent">
    <item name="android:textSize">30sp</item>
    <item name="cornerSize">16dp</item>
</style>

My Dialog


Solution

  • You can use:

    builder.setTheme(R.style.MaterialCalendarTheme)
    

    and then define:

      <style name="MaterialCalendarTheme" parent="ThemeOverlay.MaterialComponents.MaterialCalendar">
    
        <!-- Buttons -->
        <item name="buttonBarPositiveButtonStyle">@style/TextButton</item>
        <item name="buttonBarNegativeButtonStyle">@style/TextButton</item>
      </style>
      <style name="TextButton" parent="Widget.MaterialComponents.Button.TextButton.Dialog">
        <item name="android:textColor">@color/...</item>
        <item name="backgroundTint">@color/...</item>
      </style>
    

    enter image description here