Change the color of the Datepicker dialog box for Android 5.0

Is it possible to change the color scheme of datepicker (as well as timepicker) for android 5.0?

I tried to set the colors of the accent, but this will not work (with or without android: :

 <!-- colorPrimary is used for the default action bar background --> <item name="colorPrimary">@color/purple</item> <!-- colorPrimaryDark is used for the status bar --> <item name="colorPrimaryDark">@color/purple_tint</item> <!-- colorAccent is used as the default value for colorControlActivated which is used to tint widgets --> <item name="colorAccent">@color/purple_tint</item> 

From the original: enter image description here

Something like that: enter image description here

+84
android dialog
Feb 26 '15 at 8:59
source share
9 answers

The reason the Neil clause leads to the full-screen DatePicker screen is to choose the parent theme:

 <!-- Theme.AppCompat.Light is not a dialog theme --> <style name="DialogTheme" parent="**Theme.AppCompat.Light**"> <item name="colorAccent">@color/blue_500</item> </style> 

In addition, if you are following this route, you must specify the topic when creating the DatePickerDialog :

 // R.style.DialogTheme new DatePickerDialog(MainActivity.this, R.style.DialogTheme, new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { //DO SOMETHING } }, 2015, 02, 26).show(); 

This, in my opinion, is not very good. You need to try to save the style from java and inside styles.xml / themes.xml.

I agree that the Neil proposal with a slight change (changing the parent theme to say Theme.Material.Light.Dialog ) will give you the desired result. But otherwise:

On first inspection, we come across a datePickerStyle that defines things like: headerBackground (what you are trying to change), dayOfWeekBackground and several other text colors and text styles.

Overriding this attribute in the application theme will not work. DatePickerDialog uses a separate topic, assigned by the datePickerDialogTheme attribute. So, for our changes to affect, we have to override datePickerStyle inside the override of datePickerDialogTheme .

Here we go:

Override datePickerDialogTheme inside your application’s main theme:

 <style name="AppBaseTheme" parent="android:Theme.Material.Light"> .... <item name="android:datePickerDialogTheme">@style/MyDatePickerDialogTheme</item> </style> 

Define MyDatePickerDialogTheme . The choice of the parent theme will depend on what your base theme for the application is: it can be either Theme.Material.Dialog or Theme.Material.Light.Dialog :

 <style name="MyDatePickerDialogTheme" parent="android:Theme.Material.Light.Dialog"> <item name="android:datePickerStyle">@style/MyDatePickerStyle</item> </style> 

We redefined datePickerStyle with MyDatePickerStyle style. The choice of parent will again depend on what your base theme for your application is: either Widget.Material.DatePicker , or Widget.Material.Light.DatePicker . Define it according to your requirements:

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

Currently, we are only redefining the headerBackground , which defaults to ?attr/colorAccent (this is also due to the fact that the Neil clause works when changing the background). But there are quite a few settings:

 dayOfWeekBackground dayOfWeekTextAppearance headerMonthTextAppearance headerDayOfMonthTextAppearance headerYearTextAppearance headerSelectedTextColor yearListItemTextAppearance yearListSelectorColor calendarTextColor calendarSelectedTextColor 

If you do not want this big control (settings), you do not need to redefine datePickerStyle . colorAccent controls most DatePicker's colors. So, overriding only colorAccent inside MyDatePickerDialogTheme should work:

 <style name="MyDatePickerDialogTheme" parent="android:Theme.Material.Light.Dialog"> <item name="android:colorAccent">@color/date_picker_accent</item> <!-- No need to override 'datePickerStyle' --> <!-- <item name="android:datePickerStyle">@style/MyDatePickerStyle</item> --> </style> 

Overriding colorAccent gives you the added benefit of changing the colors of OK and CANCEL text. Not bad.

This way you do not need to provide any style information to DatePickerDialog's constructor. Everything is correctly connected:

 DatePickerDialog dpd = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { } }, 2015, 5, 22); dpd.show(); 
+261
Mar 12 '15 at 15:56
source share

Give it a try.

The code

 new DatePickerDialog(MainActivity.this, R.style.DialogTheme, new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { //DO SOMETHING } }, 2015, 02, 26).show(); 

Style In styles.xml

EDIT - Changed theme for Theme.AppCompat.Light.Dialog as suggested

 <style name="DialogTheme" parent="Theme.AppCompat.Light.Dialog"> <item name="colorAccent">@color/blue_500</item> </style> 
+52
Feb 26 '15 at 9:18
source share

try it, work for me

Put two options: colorAccent and android:colorAccent

 <style name="AppTheme" parent="@style/Theme.AppCompat.Light.NoActionBar"> <!-- Customize your theme here. --> .... <item name="android:dialogTheme">@style/AppTheme.DialogTheme</item> <item name="android:datePickerDialogTheme">@style/Dialog.Theme</item> </style> <style name="AppTheme.DialogTheme" parent="Theme.AppCompat.Light.Dialog"> <!-- Put the two options, colorAccent and android:colorAccent. --> <item name="colorAccent">@color/colorPrimary</item> <item name="android:colorPrimary">@color/colorPrimary</item> <item name="android:colorPrimaryDark">@color/colorPrimaryDark</item> <item name="android:colorAccent">@color/colorPrimary</item> </style> 
+4
Mar 20 '17 at 19:46
source share

Just to mention, you can also use the default theme like android.R.style.Theme_DeviceDefault_Light_Dialog .

 new DatePickerDialog(MainActivity.this, android.R.style.Theme_DeviceDefault_Light_Dialog, new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { //DO SOMETHING } }, 2015, 02, 26).show(); 
+3
Jan 28 '16 at 5:51 on
source share

You do not have a topic to create, just write it in the dialogue creation object

 DatePickerDialog datePicker = new DatePickerDialog(getActivity(), AlertDialog.THEME_HOLO_LIGHT,this, mYear, mMonth, mDay); 

Follow it, it will give you all type of date picker it really works

http://www.android-examples.com/change-datepickerdialog-theme-in-android-using-dialogfragment/

+1
Jul 29 '16 at 7:28
source share

I had a problem that the timing buttons did not appear on the API 24 screen in Android. (API 21+) it is solved

 <style name="MyDatePickerDialogTheme" parent="android:Theme.Material.Light.Dialog"> <item name="android:colorAccent">@color/colorPrimary2</item></style> <style name="Theme" parent="BBaseTheme"> <item name="android:datePickerDialogTheme">@style/MyDatePickerDialogTheme</item> </style> 
0
May 23 '17 at 17:04
source share
 <style name="AppThemeDatePicker" parent="Theme.AppCompat.Light.NoActionBar"> <item name="colorAccent">@color/select2</item> <item name="android:colorAccent">@color/select2</item> <item name="android:datePickerStyle">@style/MyDatePickerStyle</item> </style> <style name="MyDatePickerStyle" parent="@android:style/Widget.Material.Light.DatePicker"> <item name="android:headerBackground">@color/select2</item> </style> 
0
Aug 24 '17 at 5:38 on
source share

To change the text color of an item in the Change Year list item (SPECIFIED FOR Android 5.0)

Just set a specific text color in the style of the date picker. For some reason, setting the flag yearListItemTextAppearance does not reflect any changes in the list of years.

 <item name="android:textColor">@android:color/black</item> 
0
Jun 25 '18 at 7:56
source share

Create new style

 <!-- Theme.AppCompat.Light.Dialog --> <style name="DialogTheme" parent="Theme.AppCompat.Light.Dialog"> <item name="colorAccent">@color/blue_500</item> </style> 

Java code:

Parenting is the key here. Choose your color Accent

 DatePickerDialog = new DatePickerDialog(context,R.style.DialogTheme,this,now.get(Calendar.YEAR),now.get(Calendar.MONTH),now.get(Calendar.DAY_OF_MONTH); 

Result:

enter image description here

0
Dec 12 '18 at 6:25
source share



All Articles