Can't get Android DatePickerDialog to switch to Spinner mode

I am creating an application that uses DatePickerDialog to allow the user to select a date of birth. 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 this is what the dialog looks like when loading: Original dialogue

However, they want to be able to use the old DatePicker because it is not always obvious to the user in the new Calendar view that they can change the year. So I am researching this topic, and according to what I have read, it should be possible to use themes to make the DatePickerDialog work in Spinner mode. So here's what I did.

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

(Sorry for the screenshot. Apparently the SO parser cannot 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 dialogue

Obviously something has changed; dialogue has a darker theme. But it doesn't do what I want. It still displays a calendar view instead of a spinner view. Any idea what I might be missing?

+3


source to share


3 answers


Here's what worked for me:

1) Create these two styles in 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 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 the color and color of the text.

+6


source


Using the styles mentioned in another answer to get the counter only working in stock (or near) Android devices like Pixel, Motorola, OnePlus, etc. I made a mistake when testing only on these devices, and to my surprise, Samsung users were still complaining about a different collector where they couldn't find how to change the year of birth without scrolling through month after month.



The only cross-device solution is to use a library, so you're sure you're getting the same spinner picker regardless of OS. In my case, I used SpinnerDatePicker , which is a fairly simple replacement for the native picker.

0


source


Similar to what @Abuzaid said, in your AppTheme style add an element android:datePickerStyle

as shown in this AppTheme example below:

 <!-- Example of a Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorPrimary</item>

    <!-- !Important! Add this to force the use of the Spinner mode -->
    <item name="android:datePickerStyle">@style/myDatePickerStyle</item>
</style>

      

Add this style to your styles.xml file (inside the values-v21 folder as this option is only available for API 21+)

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

      

For Android version <21, the default rotation mode. For 21+, Calendar Mode is the new default.

In this case, you do not have to specify the topic of the dialogue, because it will depend on the AppTheme we have. For example: (since Java 8 lambda)

final Calendar cal = Calendar.getInstance();
final DatePickerDialog.OnDateSetListener dateSelectedListener = (view, year, monthOfYear, dayOfMonth) -> {
    cal.set(Calendar.YEAR, year);
    cal.set(Calendar.MONTH, monthOfYear+1);
    cal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
 };

int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);

DatePickerDialog dp= new DatePickerDialog(context, dateSelectedListener, year, month, day);

      

Link: https://developer.android.com/reference/android/app/DatePickerDialog

Hope this helps! :)

0


source







All Articles