Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't get Android DatePickerDialog to switch to Spinner mode

I am building an app that uses a DatePickerDialog to allow the user to select their birthdate. Here's the code that loads the dialog right now:

private void selectBirthdate() {
    int year, month, day;
    if (mBirthDate == null) {
        year = DEF_YEAR;
        month = DEF_MON;
        day = DEF_DAY;
    }
    else {
        year = mBirthDate.get(Calendar.YEAR);
        month = mBirthDate.get(Calendar.MONTH);
        day = mBirthDate.get(Calendar.DAY_OF_MONTH);
    }
    new DatePickerDialog(
            getActivity(),
            new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                    mBirthDate = new GregorianCalendar();
                    mBirthDate.set(Calendar.YEAR, year);
                    mBirthDate.set(Calendar.MONTH, month);
                    mBirthDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                    if (mTxtBirthDate != null) {
                        mTxtBirthDate.setText(mBirthDateFormat.format(mBirthDate.getTime()));
                    }
                }
            },
            year,
            month,
            day
    ).show();
}

And here's what the dialog looks like when I load it: Original Dialog

However, they want to be able to use the old-style spinner DatePicker, because in the new Calendar view, it's not always obvious to the user that they can change the year. So I have been studying up on the topic, and according to what I have read, it should be possible to use themes to force the DatePickerDialog into Spinner mode. So here's what I've done.

First, I added the following to my styles.xml: Style XML

(Sorry for the screenshot. Apparently SO's parser can't handle XML.)

Then, I update the DatePickerDialog constructor to use my new style:

new DatePickerDialog(
        getActivity(),
        R.style.MyDialogTheme,
        new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                mBirthDate = new GregorianCalendar();
                mBirthDate.set(Calendar.YEAR, year);
                mBirthDate.set(Calendar.MONTH, month);
                mBirthDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                if (mTxtBirthDate != null) {
                    mTxtBirthDate.setText(mBirthDateFormat.format(mBirthDate.getTime()));
                }
            }
        },
        year,
        month,
        day
).show();

Now, when I load the dialog it looks like this: New Dialog

Clearly something has changed; the dialog has a darker theme. But it's not doing what I want. It's still displaying the calendar view instead of the spinner view. Any idea what I might have missed?

like image 675
Russell Stewart Avatar asked Jun 07 '17 16:06

Russell Stewart


People also ask

Can I use themes to force the datepickerdialog into spinner mode?

However, they want to be able to use the old-style spinner DatePicker, because in the new Calendar view, it's not always obvious to the user that they can change the year. So I have been studying up on the topic, and according to what I have read, it should be possible to use themes to force the DatePickerDialog into Spinner mode.

What is datepickerdialog in Android?

DatePickerDialog in Android. Android DatePicker is a user interface control that is used to select the date by a day, month, and year in the android application. DatePicker is used to ensure that the users will select a valid date.

Can I have spinner picker dialogs appear in my Android app?

Using the DatePickerDialog and TimePickerDialog in a spinner fashion can be a great way for users to enter data into your app. If you still support Android 7.0 devices though and you want to be able to have spinner picker dialogs appear in your app, check out Ibotta’s open source library which provides a simple fix to this issue.

Why is the inline datepicker not working in Android?

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".


1 Answers

Here is what worked for me:

1) Create these two styles in your style.xml

<style name="MySpinnerDatePickerStyle" parent="android:Theme.Material.Dialog">
    <item name="android:datePickerStyle">@style/MySpinnerDatePicker</item>
</style>

<style name="MySpinnerDatePicker" parent="android:Widget.Material.DatePicker">
    <item name="android:datePickerMode">spinner</item>
</style>

2) Set "MySpinnerDatePickerStyle" as your style in your new DatePickerDialog

DatePickerDialog datePickerDialog = new DatePickerDialog(myContext, R.style.MySpinnerDatePickerStyle, date, myCalendar
                    .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                    myCalendar.get(Calendar.DAY_OF_MONTH));

Hope this works for you.. Note: I am still trying to figure out how to style the spinner by changing color and text color

like image 93
Abuzaid Avatar answered Oct 07 '22 23:10

Abuzaid