DatePicker headerTextColor: cannot resolve attribute

1.9k views Asked by At

I want to apply some style to DatePicker. In platform's attrs.xml we can see following attributes for DatePicker styleable:

<declare-styleable name="DatePicker">
    ...
    <!-- The text color for the selected date header text, ex. "2014" or
         "Tue, Mar 18". This should be a color state list where the
         activated state will be used when the year picker or day picker is
         active.-->
    <attr name="headerTextColor" format="color" />

    <!-- The background for the selected date header. -->
    <attr name="headerBackground" />
    ...
</declare-styleable>

While I can refer to android:headerBackground, unexpectedly I cannot do that for android:headerTextColor attribute. So following code in styles.xml:

<style name="MyDatePickerStyle" parent="@android:style/Widget.Material.DatePicker">
  <item name="android:headerBackground">@color/red</item>
  <item name="android:headerTextColor">@color/red</item>
</style>

prompts with error, that android:headerTextColor cannot be resolved.

enter image description here

But I can clearly see Widget.Material.DatePicker overriding that attribute. Interestingly that chunk of code is preceded with Attributes for new-style DatePicker comment, which may somehow lead to the cause of this behavior.

What maybe the cause of this behavior and how to override that attribute?

Running on Android Studio 2.3, minSdkVersion 23, buildToolsVersion 25.0.3, compileSdkVersion & targetSdkVersion 23, invalidated caches and cleaned project.


As you can see in R.attr docs, there is this text behind some attributes:

This constant was deprecated in API level 23. Use headerTextColor instead.

Which means, that this attribute should be exposed to public API, but somehow it is being stripped and AAPT cannot access it.

Opened an issue at bug tracker.

3

There are 3 answers

4
Amjad Khan On

Try to use this

Style.xml

 <style name="DatePicker">
        <item name="android:headerBackground">@color/colorPrimaryDark</item>
        <item name="headerTextColor">@color/colorAccent</item>
 </style>

Values folder attr.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="DatePicker">
        ...
        <!-- The text color for the selected date header text, ex. "2014" or
             "Tue, Mar 18". This should be a color state list where the
             activated state will be used when the year picker or day picker is
             active.-->
        <attr name="headerTextColor" format="color" />
    </declare-styleable>
</resources>
2
Tin Tran On

style attribute "android:attr/headerTextColor" is private

AAPT says that the attribute is private. Probably they are missing a @hide in attrs.xml

0
Mayu On

It's been a while but since this hasn't been resolved yet, I worked around this by getting the header's view id and then setting the text color directly programmatically, after I call picker.show() in the fragment like this. Hopefully this will help someone that stumbles on this question.

int yearSpinnerId = Resources.getSystem().getIdentifier("date_picker_header_year", "id", "android");
AppCompatTextView year = picker.findViewById(yearSpinnerId);
int headerTextId = Resources.getSystem().getIdentifier("date_picker_header_date", "id", "android");
AppCompatTextView selectedDate = picker.findViewById(headerTextId);
year.setTextColor(getResources().getColor(R.color.white_FFFFFF));
selectedDate.setTextColor(getResources().getColor(R.color.white_FFFFFF));