Android: How to change the selected date color in a DatePicker element?

I am new to Android programming and I want to use an element DatePickerin my layout. I can use it successfully, but I cannot figure out how to change its selected date color. I read a lot of threads on this site and they talk about color changing in DatePickerDialog. I tried to follow similar approaches for an element DatePicker, but could not do it. I can change the date textColorusing the attribute calendarTextColor. My code for the layout file is as follows:

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center">

            <DatePicker
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/datePicker"
                android:datePickerMode="calendar"
                android:spinnersShown="false"
                android:layout_gravity="center"

                android:layout_marginTop="-52dp"
                android:layout_marginBottom="-20dp"
                style="@style/MyDatePickerStyleTheme"
                />

</LinearLayout>

And here is the corresponding styles.xml code:

<style name="MyDatePickerStyleTheme" parent="@android:style/Theme.Holo.Light.Dialog">
    <item name="colorAccent"> #00ff00 </item>

    <item name="android:datePickerStyle">@style/MyDatePickerStyle</item>
    <item name="android:colorPrimary"> #00ff00 </item>
    <item name="android:colorPrimaryDark"> #00ff00 </item>
    <item name="android:colorAccent"> #00ff00 </item>
    <item name="android:backgroundTint"> #00ff00 </item>
    <!-- <item name="android:calendarTextColor"> #00ff00 </item> -->


</style>

<style name="MyDatePickerStyle">
    <item name="android:calendarTextColor"> #00ff00 </item>
</style>

Some additional points that I would like to add:

  • Attribute
  • android:calendarTextColor , MyDatePickerStyleTheme, MydatePickerStyle.
    , .
  • Android (Lollipop Marshmallow) .
  • , .

.

+4
2
<style name="MyDatePickerDialogTheme" parent="Theme.AppCompat.Light.Dialog">
<!-- this is new -->
<item name="colorAccent">@color/accent</item>

<item name="android:datePickerStyle">@style/MyDatePickerStyle</item>
<item name="android:colorAccent">@color/primDark</item>

<style name="MyDatePickerStyle" parent="@android:style/Widget.Material.Light.DatePicker">
<item name="android:headerBackground">@color/prim</item>
<item name="android:calendarTextColor">@color/primDark</item>
<item name="android:dayOfWeekBackground">@color/primDark</item>
<item name="android:yearListSelectorColor">@color/accent</item>
<item name="android:datePickerMode">calendar</item>
<item name="android:minDate">01/01/2000</item>

+1

Source: https://habr.com/ru/post/1650165/


All Articles