MaterialDatePicker error: materialCalendarFullscreenTheme attribute to be set in your app theme
Asked Answered
R

2

14

What I did:

  1. Added implementation 'com.google.android.material:material:1.1.0' in dependencies
  2. Set Theme.MaterialComponents.Light.Bridge as parent to app theme
     <style name="AppTheme" parent="Theme.MaterialComponents.Light.Bridge">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
  1. Tried to show date picker in fragment.
MaterialDatePicker.Builder<Pair<Long, Long>> builder = MaterialDatePicker.Builder.dateRangePicker();

        Calendar now = Calendar.getInstance();
        now.set(Calendar.YEAR, 2020);
        now.set(Calendar.MONTH, 1);
        now.set(Calendar.DAY_OF_MONTH, 10);

        long first = now.getTimeInMillis();

        now.set(Calendar.YEAR, 2020);
        now.set(Calendar.MONTH, 5);
        now.set(Calendar.DAY_OF_MONTH, 20);

        long last = now.getTimeInMillis();

        builder.setSelection(new Pair<>(first, last));

        MaterialDatePicker<Pair<Long, Long>> picker = builder.build();

        picker.show(fragmentActivity.getSupportFragmentManager(), "RangePicker");

When I ran the code, got this error

java.lang.IllegalArgumentException: com.google.android.material.datepicker.MaterialDatePicker
requires a value for the com.example:attr/materialCalendarFullscreenTheme attribute to be set
in your app theme. You can either set the attribute in your theme or update your theme to
inherit from Theme.MaterialComponents (or a descendant).
Rioux answered 26/3, 2020 at 15:28 Comment(0)
R
31

Just add the following to your app theme.

<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>

After adding

<style name="AppTheme" parent="Theme.MaterialComponents.Light.Bridge">
   <!-- Customize your theme here. -->
   <item name="colorPrimary">@color/colorPrimary</item>
   <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
   <item name="colorAccent">@color/colorAccent</item>

   <!-- Add these -->
   <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>
</style>
Rioux answered 26/3, 2020 at 15:28 Comment(3)
Alternatively, you should migrate to the new Theme.MaterialComponents.* themes without the Bridge suffix which should automatically include the themes and styles needed.Dilapidate
Using either of the above themes made my Tab bar lose its effects and the non-selected tab text invisible. Not good. Instead, extend your AppTheme from Theme.MaterialComponents.DayNight.DarkActionBar.Bridge or, light instead of DayNight...Pendentive
materialCalendarStyle should be attr, not itemShu
U
0

There is a simple solution ! Just make sure the theme.xml is the same theme that you used in manifest file part theme. In android manifest: android:theme="@style/Theme.MaterialComponents.DayNight.NoActionBar"> in theme.xml:

Used answered 8/1, 2022 at 14:12 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Gilmer

© 2022 - 2024 — McMap. All rights reserved.