Is it possible to change the datepicker (and also the timepicker) colour scheme for android 5.0?
I've tryed setting the accent colours, but this doesn't work (both with and 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 original:
To something like this:
This question is related to
android
dialog
styles
background-color
try this, work for me
Put the 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>
Just to mention, you can also use the default a theme like android.R.style.Theme_DeviceDefault_Light_Dialog
instead.
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();
For Changing Year list item text color (SPECIFIC FOR ANDROID 5.0)
Just set certain text color in your date picker dialog style. For some reason setting flag yearListItemTextAppearance
doesn't reflect any change on year list.
<item name="android:textColor">@android:color/black</item>
I had the problem that time picker buttons have not been seen in the screen for API 24 in android. (API 21+) it is solved by
<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>
<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>
Create a new style
<!-- Theme.AppCompat.Light.Dialog -->
<style name="DialogTheme" parent="Theme.AppCompat.Light.Dialog">
<item name="colorAccent">@color/blue_500</item>
</style>
Java code:
The parent theme is the key here. Choose your colorAccent
DatePickerDialog = new DatePickerDialog(context,R.style.DialogTheme,this,now.get(Calendar.YEAR),now.get(Calendar.MONTH),now.get(Calendar.DAY_OF_MONTH);
Result:
Give this 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();
The Style In your styles.xml file
EDIT - Changed theme to Theme.AppCompat.Light.Dialog as suggested
<style name="DialogTheme" parent="Theme.AppCompat.Light.Dialog">
<item name="colorAccent">@color/blue_500</item>
</style>
Kotlin, 2021
// set date as button text if pressed
btnDate.setOnClickListener(View.OnClickListener {
val dpd = DatePickerDialog(
this,
{ view, year, monthOfYear, dayOfMonth ->
val selectDate = Calendar.getInstance()
selectDate.set(Calendar.YEAR, year)
selectDate.set(Calendar.MONTH, monthOfYear)
selectDate.set(Calendar.DAY_OF_MONTH, dayOfMonth)
var formatDate = SimpleDateFormat("dd/MM/yyyy", Locale.getDefault())
val date = formatDate.format(selectDate.time)
Toast.makeText(this, date, Toast.LENGTH_SHORT).show()
btnDate.text = date
}, 1990, 6, 6
)
val calendar = Calendar.getInstance()
val year = calendar[Calendar.YEAR]
val month = calendar[Calendar.MONTH]
val day = calendar[Calendar.DAY_OF_MONTH]
dpd.datePicker.minDate = GregorianCalendar(year - 90, month, day, 0, 0).timeInMillis
dpd.datePicker.maxDate = GregorianCalendar(year - 10, month, day, 0, 0).timeInMillis
dpd.show()
})
Styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- This is main Theme Style for your application! -->
<item name="android:datePickerDialogTheme">@style/MyDatePickerDialogTheme</item>
</style>
<style name="MyDatePickerDialogTheme" parent="android:Theme.Material.Dialog">
<item name="android:datePickerStyle">@style/MyDatePickerStyle</item>
</style>
<style name="MyDatePickerStyle" parent="@android:style/Widget.Material.DatePicker">
<item name="android:headerBackground">#A500FF</item>
</style>
You don't have create theme just write it in your dialog creation object
DatePickerDialog datePicker = new DatePickerDialog(getActivity(), AlertDialog.THEME_HOLO_LIGHT,this, mYear, mMonth, mDay);
follow this it will give you all type date picker style it's really work
http://www.android-examples.com/change-datepickerdialog-theme-in-android-using-dialogfragment/
Source: Stackoverflow.com