9

Seems like anyone using Marshmallow (Android 6.0) is not able to use the DatePicketDialog within my app. There appears to be some sort of theme issue that I'm encountering. I use a DialogFragment which contains a DatePicketDialog for the user to select birthday. Here are shots of the DialogFragment with Android 5.x an d 6.x.

Android 5.x Android 6.x

I attempted to add a theme in the DatePickerDialog constructor, but that made the DialogFragment fullscreen and I don't want that. Does anyone know how I can get the DatePickerDialog to look like it was prior to Marshmallow?

UPDATE 1

Here is the code where I create the DialogFragment:

DialogFragment ageFragment = new DatePickerDialogFragment();
ageFragment.show(getFragmentManager(), "datePicker");

Here is the onCreateDialog inside the DatePickerDialogFragment:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the current date as the default date in the picker if no filters are set
    Calendar cal = Calendar.getInstance();
    // Set the date 18 years previous, since only 18 and older are allowed on the app
    cal.add(Calendar.YEAR, -18);
    int year, month, day;
    if (iDialogListener.getYear() == -1 || iDialogListener.getMonth() == -1
            || iDialogListener.getDay() == -1) {
        Calendar defaultCal = Calendar.getInstance();
        // 40 is the default age to show
        defaultCal.add(Calendar.YEAR, -40);
        year = defaultCal.get(Calendar.YEAR);
        month = defaultCal.get(Calendar.MONTH);
        day = defaultCal.get(Calendar.DAY_OF_MONTH);
    } else {
        year = iDialogListener.getYear();
        month = iDialogListener.getMonth();
        day = iDialogListener.getDay();
    }

    DatePickerDialog datepicker = new DatePickerDialog(getActivity(), this, year, month, day);
    datepicker.getDatePicker().setMaxDate(cal.getTimeInMillis());
    Calendar minDate = Calendar.getInstance();
    minDate.set(Calendar.YEAR, Calendar.getInstance().get(Calendar.YEAR) - 100);
    datepicker.getDatePicker().setMinDate(minDate.getTimeInMillis());

    // Create a new instance of DatePickerDialog and return it
    return datepicker;
}

In the themes.xml the only line that touches Dialogs is

<item name="android:alertDialogTheme">@style/CustomDialogTheme</item>

But if I'm thinking right, that doesn't touch the DialogFragment does it?

Update 2

Here is the CustomDialogTheme:

<style name="CustomDialogTheme" parent="@android:style/Theme.Holo.Light.Dialog">
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowMinWidthMajor">@android:dimen/dialog_min_width_major</item>
    <item name="android:windowMinWidthMinor">@android:dimen/dialog_min_width_minor</item>
</style>
Mike Walker
  • 2,944
  • 8
  • 30
  • 62
  • 1
    Please provide a [minimal, complete, and verifiable example](http://stackoverflow.com/help/mcve) demonstrating your problem. In this case, that would include details about the theme being used by the activity hosting this fragment, plus the code from the fragment itself. Simply showing a `DatePickerDialog` works fine on Android 6.0. – CommonsWare Nov 17 '15 at 21:37
  • Updated the post with code. – Mike Walker Nov 17 '15 at 21:57
  • "But if I'm thinking right, that doesn't touch the DialogFragment does it?" -- no, but probably it affects the `DatePickerDialog` itself, since `DatePickerDialog` extends `AlertDialog`. – CommonsWare Nov 17 '15 at 22:04
  • I added the `CustomDialogTheme` code – Mike Walker Nov 17 '15 at 22:07
  • Hmmm... the screenshot on the left does not look like a Holo-themed `DatePickerDialog`, which still used the old up/down "spinner" approach to picking dates, not showing a calendar. Yet your `CustomDialogTheme` is using Holo. I can't explain that. – CommonsWare Nov 17 '15 at 22:14
  • The activities theme's parent is `Theme.AppCompat.Light.NoActionBar`. – Mike Walker Nov 17 '15 at 22:16
  • I have not tried using `DatePickerDialog` in an `appcompat-v7` app. I would not be shocked if the conflict is between your Holo theme for the dialog and the `AppCompat` theme for the activity and its fragment. Regardless, you're now outside my ability to help -- sorry! – CommonsWare Nov 17 '15 at 22:22
  • it worked on android 6.0 but although I changed to blue color, the color is stil red on android 5.1.1.anyone can help me? – nahoang Jan 31 '18 at 09:57

3 Answers3

6

You create theme in styles.xml but didn't refer it to DatePickerDialog like

DatePickerDialog dlg = new DatePickerDialog(getActivity(),
                R.style.datepickerCustom,this,year,month,day);

and create a folder name values-v21 and add a copy of style.xml to it. and paste

<style name="datepickerCustom" parent="@android:style/Theme.DeviceDefault.Light.Dialog">
        <item name="android:colorAccent">@color/orange_theme</item>
        <item name="android:windowBackground">@android:color/darker_gray</item>
        <!--<item name="android:windowContentOverlay">@null</item>-->
        <!--<item name="android:textColorPrimary">@color/white</item>-->
        <!--<item name="android:textColorSecondary">@color/white</item>-->
    </style>
Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300
5

Just change colorAscent color in styles.xml :

<style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">

    <item name="colorAccent">#FC6C2D</item>   ---> I used orange color here to display the picker dialog in orange color for marshmallow device.

    <item name="android:textColorPrimary">#000000</item>
    <item name="android:textColorSecondary">#000000</item>

</style>
Stephen
  • 9,899
  • 16
  • 90
  • 137
4

Thanks to the post How to change the style of Date picker in android?, I was able to properly show the DatePicker. I had to use parent="Theme.AppCompat.Light.Dialog" when styling the DatePicker

Community
  • 1
  • 1
Mike Walker
  • 2,944
  • 8
  • 30
  • 62
  • If I use your solution, My DatePicker's theme is getting change (old theme like 4.4). Any suggestion??? – GreenROBO May 18 '16 at 13:01