Home » Android » android – Impossible to make my DatePickerDialog use a spinner style programmatically

android – Impossible to make my DatePickerDialog use a spinner style programmatically

Posted by: admin June 15, 2020 Leave a comment


I’m using a DialogFragment to open a DatePickerDialog

public class DatePickerFragment extends DialogFragment{

public Dialog onCreateDialog(Bundle savedInstanceState) {

    // Use the current date as the default date in the picker
    final Calendar c = Calendar.getInstance();
    int year = c.get(Calendar.YEAR);
    int month = c.get(Calendar.MONTH);
    int day = c.get(Calendar.DAY_OF_MONTH);

    // Create a new instance of DatePickerDialog and return it
    DatePickerDialog DatePickerDialog = new DatePickerDialog(getActivity(), (ProfileCreationActivity)getActivity(), year, month, day);
    return DatePickerDialog;

I’m getting a calendar look, where I would prefer a spinner look.

I tried:




but it does not work.

Please note that I want the spinner look for one activity, but that I will want the calendar view for another activity. So I can not change the whole application style. I need a custom style for one activity.

How to&Answers:

I have found a the explanation in the following post (which describes a problem very similar to mine) :

Android Material Design Inline Datepicker issue

In fact the setCalendarViewShown(false) and setSpinnersShown(true) are apparently not working anymore in latest versions.

We have to use an explicit XML attribute like this one android:datePickerMode=”spinner”.

The problem is that I’m using a DialogFragment without any XML layout (just a date picker dialog). So I cannot set any XML attribute.

The solution is to create a dedicated custom dialog with an XML layout file using the requested attribute.


You can keep it programatically, don’t need to create a new XML with the spinner, I simply changed my AppTheme(v21) style and worked 😉

<style name="AppTheme"  parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="colorControlActivated">@color/colorPrimary</item>
    <item name="android:timePickerDialogTheme">@style/PickerDialogCustom</item>
    <item name="android:datePickerDialogTheme">@style/PickerDialogCustom</item>
    <item name="alertDialogTheme">@style/AlertDialogCustom</item>

<style name="PickerDialogCustom" parent="AlertDialogCustom">
    <item name="android:textColor">@color/colorPrimary</item>
    <item name="android:textColorPrimary">@color/colorPrimaryDark</item>
    <item name="colorControlNormal">@color/greyLight500</item>
    <item name="android:layout_margin">2dp</item>
    <item name="android:datePickerMode">spinner</item>

<style name="AlertDialogCustom" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorAccent">@color/colorPrimary</item>
    <item name="android:positiveButtonText">@color/colorPrimary</item>
    <item name="android:negativeButtonText">@color/greyDark200</item>
    <item name="buttonBarNegativeButtonStyle">@style/negativeButton</item>
    <item name="android:datePickerStyle">@style/PickerDialogCustom</item>

remember to keep the support for <21 just adding this line, this command is ignored for >=21



<item name="android:datePickerMode">spinner</item>Just add one line in style v21