flaviussn
flaviussn

Reputation: 1355

Android DatePicker color style

I want to change the default "green" color of Material Design DatePicker using AppCompat, but I don't found anything about.

I tried to do something like this, but no results:

<item name="android:datePickerStyle">#00BCD4</item> 

This other way don't allow me to change the color because the minSdkVersion..

<item name="android:calendarTextColor">#00BCD4</item>

Is it possible to change the color for all Android versions from API 15 to 21 ?

Upvotes: 4

Views: 1980

Answers (1)

albert c braun
albert c braun

Reputation: 2710

This is how to get the purple and orange colors in the image below.

Create a custom theme for your DatePicker in your styles.xml file:

    <style name="MyDatePickerTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
        <item name="colorAccent">@android:color/holo_purple</item>
        <item name="android:windowBackground">@drawable/date_picker_background</item>
    </style>

Note that this theme references a custom background for the date picker in the drawable resource folder. So create a drawable resource named date_picker_background.xml and put this in it:

<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:insetLeft="16dp"
    android:insetTop="16dp"
    android:insetRight="16dp"
    android:insetBottom="16dp">
    <shape android:shape="rectangle">
        <corners android:radius="2dp" />
        <solid android:color="@android:color/holo_orange_dark" />
    </shape>
</inset>

Finally, you must reference this theme when you instantiate the DatePickerDialog:

DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(),
    R.style.MyDatePickerTheme, this, year, month, day);

My explanation here was adapted from this better, more complete explanation of DatePicker styling.

enter image description here

Upvotes: 1

Related Questions