Reputation: 379
Hi i want to adjust my DatePickerDialog
, and instead of buttons at the bottom (displayed on image below) i just want to make blue text.
Here is how i created DatePickerDialog
private val birthdayDate by lazy {
Calendar.getInstance()
}
private val datePicker by lazy {
DatePickerDialog(
context,
R.style.MyDatePickerDialogTheme,
this,
birthdayDate.get(Calendar.YEAR),
birthdayDate.get(Calendar.MONTH),
birthdayDate.get(Calendar.DAY_OF_MONTH)
).also {
it.datePicker.maxDate = Calendar.getInstance().timeInMillis
}
}
Then i just call show method to display the dialog. I think problem is somewhere in my style
, but i can not find where. Any help will be highly appreciated.
<style name="MyDatePickerDialogTheme" parent="Theme.AppCompat.Light.Dialog">
<item name="colorAccent">@color/colorPrimary</item>
</style>
Upvotes: 9
Views: 5545
Reputation: 634
The current most voted answer changes the default minHeight
and minWidth
of buttons compared to the default date picker.
To correct this I had to specify these attributes in the custom style.
I was able to find the standard attribute for minWidth
, but default minHeight
has 54dp
value in the runtime and I couldn't find this value anywhere among standard Android values.
This example uses Material themes directly.
<style name="Theme.AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
.....
<item name="android:datePickerDialogTheme">@style/DatePickerDialogTheme</item>
</style>
<style name="DatePickerDialogTheme" parent="android:Theme.Material.Light.Dialog">
<item name="android:colorAccent">@color/colorPrimary<item>
<item name="android:buttonBarPositiveButtonStyle">@style/DatePickerButtonStyle</item>
<item name="android:buttonBarNegativeButtonStyle">@style/DatePickerButtonStyle</item>
</style>
<style name="DatePickerButtonStyle" parent="android:Widget.Material.Button.Borderless">
<item name="android:minHeight">54dp</item>
<item name="android:minWidth">@dimen/mtrl_btn_dialog_btn_min_width</item>
<item name="android:textColor">@color/colorPrimary</item>
</style>
Upvotes: 0
Reputation: 1040
When you're working with MaterialDesign you could use this.
final SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
final Calendar calendar = Calendar.getInstance(parent.getResources().getConfiguration().locale);
DatePickerDialog datePickerDialog = new DatePickerDialog(parent.getContext(),
(view, year, monthOfYear, dayOfMonth) -> {
Calendar selectedDate = Calendar.getInstance();
selectedDate.set(year, monthOfYear, dayOfMonth);
},
calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
datePicker.show();
styles.xml:
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="android:datePickerStyle">@style/DatePicker</item>
<item name="android:datePickerDialogTheme">@style/DatePicker</item>
</style>
<style name="DatePicker" parent="ThemeOverlay.MaterialComponents.Dialog">
<item name="colorAccent">@color/blue</item>
<item name="android:datePickerStyle">@style/DatePickerStyle</item>
</style>
<style name="DatePickerStyle" parent="@android:style/Widget.Material.Light.DatePicker">
<item name="android:headerBackground">@color/darkblue</item>
<item name="android:yearListSelectorColor">@color/darkgray</item>
<item name="android:datePickerMode">calendar</item>
<item name="android:minDate">01/01/2000</item>
</style>
This style will lead to have no outlined or filled box buttons. Your cancel and confirm buttons will appear in colorAccents color.
Upvotes: 2
Reputation: 379
So i finally found the solution. I added extra fields in MyDatePickerDialogTheme
and extra style for buttons used in DatePicker
. Solution looks like that:
<style name="MyDatePickerDialogTheme" parent="Theme.AppCompat.Light.Dialog">
<item name="colorAccent">@color/colorPrimary</item>
<item name="android:buttonBarPositiveButtonStyle">@style/DatePickerButtonStyle</item>
<item name="android:buttonBarNegativeButtonStyle">@style/DatePickerButtonStyle</item>
</style>
<style name="DatePickerButtonStyle" parent="Base.Widget.AppCompat.Button.Borderless">
<item name="android:textColor">@color/colorPrimary</item>
<item name="android:backgroundTint">@color/white</item>
</style>
Upvotes: 19
Reputation: 400
as listed in this link you can use this theme
DatePickerDialog dpd = new DatePickerDialog(getActivity(),
AlertDialog.THEME_DEVICE_DEFAULT_LIGHT,this,year,month,day);
to achieve what you want
Upvotes: 0