6

I want to display a DatePickerDialog with mode spinner instead of calendar. In my styles.xml, I tried to put this attribute but it says that the minimum SDK version I should be using is API 21.

<item name="android:datePickerMode">spinner</item>

I am using minimum SDK version 18. How do I set the default date picker mode to spinner for APIs lower than API 21?

This is my Java code for my date picker:

public class MyDatePicker extends DialogFragment implements DatePickerDialog.OnDateSetListener {

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    DatePickerDialog datePickerDialog = new DatePickerDialog(getContext(), R.style.MyTheme, this, 0, 0, 0 );
    return datePickerDialog;
}

@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {

}

}

Thanks!

racumin
  • 392
  • 2
  • 3
  • 17
  • You cannot change it to the spinner mode on all the APIs, but you can improve user experience: https://stackoverflow.com/a/54185405/5730321 – Leo DroidCoder Jan 14 '19 at 16:57

2 Answers2

8

Found an answer. Instead of creating a new DatePickerDialog in the onCreateDialog method, I just created my own layout for the date picker inflated it in the method.

Here is the xml layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<DatePicker
    android:id="@+id/dialog_date_datePicker"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:calendarViewShown="false"
    android:datePickerMode="spinner"
    android:spinnersShown="true">
</DatePicker>

And here is the Java code:

public class MyDatePicker extends DialogFragment {
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        View v = getActivity().getLayoutInflater().inflate(R.layout.datepicker, null);
        return new AlertDialog.Builder(getActivity()).setView(v).create();
    }
}

Thanks!

racumin
  • 392
  • 2
  • 3
  • 17
0

My suggestion First get the API version.. If the API version is below 21 then set spinner visible and date picker invisible To get API version. Check this link:

Retrieving Android API version programmatically

Let me know if this can solve your problem, I'm not sure how reliable my suggestion is

Thanks!

Community
  • 1
  • 1
  • I tried this but still did not work. I also found [this](http://stackoverflow.com/questions/30509828/impossible-to-make-my-datepickerdialog-use-a-spinner-style-programmatically) and it looks like I need to really put the xml spinner in the style.xml but Android Studio says it nneds to have a minSdk of 21 – racumin Feb 13 '17 at 03:19