material-components / material-components-android

Modular and customizable Material Design UI components for Android
Apache License 2.0
16.43k stars 3.08k forks source link

[MaterialDatePicker] Unable to Change Current Date's Style #3439

Closed dkiktenko closed 1 year ago

dkiktenko commented 1 year ago

Description: With the source code provided below, the current date's style remains unchanged [compared to default]

Expected behavior: Outline color, width changes from default

Source code: In a module's styles.xml file, I have the following defined:

  <style name="SomeFragmentDatePicker" parent="ThemeOverlay.Material3.MaterialCalendar">
        <item name="materialCalendarStyle">@style/Widget.MaterialComponents.MaterialCalendar</item>
        <item name="materialCalendarFullscreenTheme">@style/ThemeOverlay.MaterialComponents.MaterialCalendar.Fullscreen</item>
        <item name="materialCalendarTheme">@style/ThemeOverlay.MaterialComponents.MaterialCalendar</item>
        <item name="dayTodayStyle">@style/SomeFragmentDatePicker.Today</item>
    </style>

    <style name="SomeFragmentDatePicker.Today" parent="@style/Widget.MaterialComponents.MaterialCalendar.Day.Today">
        <item name="android:tint">@color/pink</item>
        <item name="itemStrokeWidth">5dp</item>
    </style>

In my fragment:

  if (datePickerDialog == null) {
      datePickerDialog =
          MaterialDatePicker.Builder.datePicker()
              .setTheme(R.style.SomeFragmentDatePicker)
              .setSelection(MaterialDatePicker.todayInUtcMilliseconds())
              .build();
      datePickerDialog.addOnPositiveButtonClickListener(
          selection -> {
            // TODO
          });
    }
    datePickerDialog.show(getParentFragmentManager(), "SomeFragment");

NOTE: To reproduce, after dialog opens, change the selected date from any other date than today (since current default is that dialog opens with today selected).

Android API version: Android API 28 and 33

Material Library version: Material Android Library version 1.8.0

Device: Pixel 4, Pixel 6

afohrman commented 1 year ago

dayTodayStyle is not a theme attribute, it's meant to be set in the date picker style, so it wouldn't take effect when overridden in a theme overlay. Overriding dayTodayStyle in a custom style should work. See the recommended approach outlined in this issue.

Also, I'm not sure what exact colors you're trying to set in your override of dayTodayStyle, but you should probably set itemTextColor and itemStrokeColor directly.