DatePicker headerTextColor: cannot resolve attribute
Asked Answered
C

3

11

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.

Chandra answered 20/5, 2017 at 17:19 Comment(3)
<item name=app:headerBackground">@color/red</item>Annabellannabella
Odd. I am seeing that same behavior. android:headerMonthTextAppearance stills works to override the appearance of the month. Unfortunately, android:headerYearTextAppearance also seems to be broken, so this would be only half of a work-around.Asmara
@Cheticamp, I also saw that behavior concerning android:headerYearTextAppearance. I stumbled to this issue when answering this question. Somehow, I could find seams to change year text appearance, but it was mystery for me why this attribute wasn't seen. It seems like a bug.Chandra
T
5

style attribute "android:attr/headerTextColor" is private

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

Talishatalisman answered 1/6, 2017 at 17:39 Comment(2)
Can you please clarify how exactly could you reproduce this error message? Are you using AAPT2? Secondly, I cannot see how this attribute should have been made private because they suggest using exactly this attribute instead of deprecated headerMonthTextAppearance, headerDayOfMonthTextAppearance and headerYearTextAppearance. See here.Chandra
I'm using Android Studio 3.0 to use AAPT2. You can also add android.enableAapt2=false to gradle.properties to use aapt2 in Android Studio 2.3. yeah I see headerTextColor should not be private, developer.android.com/reference/android/R.attr.html is not documenting the attribute too.Talishatalisman
O
1

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));
Ottillia answered 7/6, 2019 at 20:52 Comment(0)
P
-1

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>
Panorama answered 1/6, 2017 at 5:21 Comment(4)
No resource found that matches the given name: attr 'headerTextColor'Chandra
Sorry android:headerBackground color we can set and it will take inverse color of it and set to headerTextColor which is private attr. Thanks Tin TranPanorama
How do you know it is private? Secondly, I cannot see how it will take the inverse color of it. Assuming it takes, what if I do not want it to take that color?Chandra
<style name="TextAppearance.Material.DatePicker.YearLabel" parent="TextAppearance.Material"> <item name="textColor">@color/primary_text_secondary_when_activated_material_inverse</item> <item name="textSize">@dimen/date_picker_year_label_size</item> <item name="fontFamily">sans-serif-medium</item> </style>Panorama

© 2022 - 2024 — McMap. All rights reserved.