Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DatePicker headerTextColor: cannot resolve attribute

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.

like image 662
azizbekian Avatar asked May 20 '17 17:05

azizbekian


2 Answers

style attribute "android:attr/headerTextColor" is private

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

like image 105
Tin Tran Avatar answered Oct 14 '22 13:10

Tin Tran


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));
like image 35
Mayu Avatar answered Oct 14 '22 13:10

Mayu