Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change DatePickerDialog buttons to text

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>
like image 686
Szuler Avatar asked Nov 28 '18 11:11

Szuler


2 Answers

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>
like image 168
Szuler Avatar answered Nov 20 '22 08:11

Szuler


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.

like image 25
Marcel Hofgesang Avatar answered Nov 20 '22 07:11

Marcel Hofgesang