1

I tried some suggestion from How to change DatePicker dialog color for Android 5.0 but the only problem is next the two of the buttons are not visible as after writing the code

public static void showExpenseDate(final Context context, final EditText textView) {

        final Calendar calendar = Calendar.getInstance();
        int yy = calendar.get(Calendar.YEAR);
        int mm = calendar.get(Calendar.MONTH);
        int dd = calendar.get(Calendar.DAY_OF_MONTH);
        DatePickerDialog datePicker = new DatePickerDialog(context, R.style.DialogTheme, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                //String[] mons = new DateFormatSymbols(Locale.ENGLISH).getShortMonths();
                //String mName = mons[monthOfYear];
                expense_date = String.valueOf(year) + "-" + String.valueOf((monthOfYear + 1))
                        + "-" + String.valueOf(dayOfMonth);
                textView.setText(expense_date);
                Log.d("djkjiksd", expense_date);

            }
        }, yy, mm, dd);

        datePicker.show();
    }

where as in theme.xml

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

enter image description here

but the button colors are not coming on UI. What is the solution and correct way to implement date picker theme according to application.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • 1
    using dialog right is painfulll (especially on activity recreation) ... use fragment like `AppCompatDialogFragment` – Selvin Nov 24 '22 at 13:41
  • 1
    I'm using [this](https://gist.github.com/SelvinPL/ed73544da79022634a38e288d7a13088) like `DatePicker.newInstance(SOME_ID_TO_CHECK_IN_CALLBACK_ONACTION, someCalendar).show(get(Child)FragmentManager())` and implementation of Callback like `public boolean onAction(int ID, boolean canceled, Calendar date) { final TextView textView; if (ID == SOME_ID_TO_CHECK_IN_CALLBACK_ONACTION) { if (!canceled) { /*do the staff*/ } return true; } return false; }` – Selvin Nov 24 '22 at 13:50
  • Switching to `MaterialDatePicker` is an option? https://stackoverflow.com/questions/57605124/compatibility-between-appcompat-and-androidx/57605698#57605698 – Gabriele Mariotti Nov 25 '22 at 07:45
  • 1
    `MaterialDatePicker` is somehow bummer (state on 2022-12)... it losing callbacks on recreate(fx orientation changed) ... they did a good job with `registerForActivityResult` and it's working fine I would expect to `MaterialDatePicker ` work in similar way – Selvin Dec 09 '22 at 13:43

4 Answers4

2

Add this in your style.xml file

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

And then add this style while creating the DatePickerDialog

DatePickerDialog datePicker = new DatePickerDialog(context, R.style.DialogTheme, new DatePickerDialog.OnDateSetListener() {
    
}
  • I did try this one, the only problem was a button was not of the same color also was not visible so I got the solution from here https://stackoverflow.com/a/50771927/12637592 – Pragyanshree Das Nov 25 '22 at 08:53
1

Modify your theme.xml something like this -

<style name="DialogTheme" parent="Theme.AppCompat.Light.Dialog">
       <item name="colorAccent">@color/light_yellow</item>
       <item name="android:colorAccent">@color/light_yellow</item>
       <item name="android:buttonBarPositiveButtonStyle">@style/DialogButtonStyled</item>
       <item name="android:buttonBarNegativeButtonStyle">@style/DialogButtonStyled</item>
       <item name="android:buttonBarNeutralButtonStyle">@style/DialogButtonStyled</item>
    </style>
    
    <style name="DialogButtonStyled" parent="Theme.MaterialComponents.Light">
        <item name="android:textColor">@color/black</item>
    </style>
Aditya Nandardhane
  • 915
  • 1
  • 8
  • 22
1

If you are using a MaterialComponents theme in your app use this theme overlay for your DatePickerDialog:

<style name="ThemeOverlay.App.Dialog" parent="@style/ThemeOverlay.MaterialComponents.Dialog">
    <item name="colorSecondary">@color/red500_light</item>
    <item name="colorPrimary">@color/blu500_dark</item> <!-- button text color -->
</style>

If you are using an AppCompat theme:

<style name="ThemeOverlay.AppCompat.Dialog" parent="@style/Theme.AppCompat.Light.Dialog">
    <item name="colorAccent">@color/red500_light</item>
    <item name="android:buttonBarPositiveButtonStyle">@style/DialogButtonStyled</item>
    <item name="android:buttonBarNegativeButtonStyle">@style/DialogButtonStyled</item>
</style>
<style name="DialogButtonStyled" parent="Widget.AppCompat.Button.Borderless">
    <item name="android:textColor">@color/blu500_dark</item> <!-- button text color -->
</style>

enter image description here

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
0

This works for me hopefully this will help others

the theme code still there in XML this code works for button color setting

enter image description here