Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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).
like image 581
bikram Avatar asked Mar 26 '20 15:03

bikram


People also ask

How to customize an existing material theme in Android?

The Material Components for Android library includes a module that allows you to easily customize an existing Material Theme. It provides you with a set of XML files (color.xml/night/color.xml, type.xml and shape.xml) which include all of the necessary baseline theme attributes mentioned in this article.

How do I set the materialcalendartheme globally?

While the posted answer totally work there seems to be no need to set the materialCalendarTheme globally - you can just set it to via the MaterialDatePicker.Builder and setTheme (int themeResId) method. Following an example how they do it in the Material Design Catalog App.

What are the new material components themes?

The Material Components themes introduce new attributes that can be used to style elements on a global scale. These can be grouped into three main subsystems: color, typography and shape.

How can I add hex color to my Android app theme?

These colors can be added to your app theme like so: Note 1: Hex color codes are not currently supported for android:colorBackground, hence why a color resource was used. Note 2: Use android:statusBarColor and android:navigationBarColor attributes to theme system bars.


1 Answers

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>
like image 65
bikram Avatar answered Nov 17 '22 00:11

bikram