41

I have a ScrollView with a Datepicker

In the previous versions of Android the Datepicker is this:

enter image description here

And I can scroll the single elements of Datepicker day, month, years without problems

In Android Lollipop API level 21 Material, the Datepiker is shown in this way:

enter image description hereenter image description here

If I click on the month is shown a calendar view and I cannot change the month, I can only select the day. If I try to edit the year is shown a year scroller, but If I try tro scroll it, the whole layout where datepicker is contained is scrolled, not only the date.

calendarViewShown=false

seems ignored

How could I fix this issue???

Ruan_Lopes
  • 1,381
  • 13
  • 18
AndreaF
  • 11,975
  • 27
  • 102
  • 168

2 Answers2

65

The calendarViewShown attribute is deprecated in the calendar-style date picker. If you want the spinner-style date picker back, you can set the datePickerMode attribute to spinner.

<DatePicker
    ...
    android:datePickerMode="spinner" />

As for the scrolling issue, the calendar-style date picker doesn't support nested scrolling.

alanv
  • 23,966
  • 4
  • 93
  • 80
  • 2
    Why can't I see that attribute in the API ? -> http://developer.android.com/reference/android/widget/DatePicker.html – Guillaume Husta Nov 13 '14 at 13:39
  • 7
    Gee, thanks Google for taking existing working code and completely breaking it on new versions of Android! This isn't even source code compatibility, it's binary compatibility they broke. – Adam Rosenfield Nov 28 '14 at 15:29
  • 3
    If you are using Holo, you will still get the spinner date picker by default and the API will still work as expected. If you update your app to Material, this will be one of many things you will need to change to support the new styles. – alanv Nov 28 '14 at 22:15
  • 2
    with `android:datePickerMode="spinner"` you get a really ugly warning: `attribute "datePickerMode" is only used in API level 21 and higher (current min is 16)` – Mulgard Dec 01 '14 at 06:05
  • 11
    @Mulgard no problem, just add `tools:ignore="UnusedAttribute"` to silent the warning. The `android:datePickerMode` attribute will be ignored in pre-Lollipop API levels. – akhy Jan 27 '15 at 08:20
  • 1
    I can't see anything in the documentation that suggests calendarViewShown is deprecated. In fact I found that both datePickerMode="spinner" and calendarViewShown="false" were required to display a three column spinner, otherwise you end up with a weird spinner/calendar hybrid. A very useful answer otherwise, thank you. – Newtz Jun 18 '15 at 02:37
  • @alanv, Superb solution. +1 for you. :) – Hiren Patel Jul 19 '15 at 05:56
  • Thank you very much. I was scratching my head over this one. This is not the first thing that seems broken in newer versions of Android. – J Steven Perry Feb 03 '16 at 04:37
  • Is it possible to set this mode in Java? – Tash Pemhiwa Jul 27 '17 at 04:34
2

Step-1: Create spinner/calendar date picker layout

.../main/res/layout/spinner_date_picker_layout.xml

    <?xml version="1.0" encoding="utf-8"?>
    <DatePicker xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/datePicker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:datePickerMode="spinner"
        android:calendarViewShown="false" />

.../main/res/layout/calendar_date_picker_layout.xml

    <?xml version="1.0" encoding="utf-8"?>
    <DatePicker xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/datePicker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:datePickerMode="calendar" />

Step-2: Set clickable behavior on TextView for showing Date Dialog.

.../main/res/layout/activity_layout.xml

    <TextView
        android:id="@+id/dateText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:clickable="true"
        android:text="Date"
        android:onClick="@{() -> viewModel.onClickDate()}"></TextView>

Step-3: Show Dialog on onClickDate

override fun onClickDate() {
        showDialogForDate()
}

Step-4: Set DatePicker Layout into Dialog View.

private fun showDialogForDate() {

    //Set spinner/calendar date picker layout
    val spinnerDatePicker = layoutInflater.inflate(R.layout.spinner_date_picker_layout, null)

    // On click listener for dialog buttons
    val dialogClickListener = DialogInterface.OnClickListener { _, which ->
        when (which) {
            DialogInterface.BUTTON_POSITIVE -> {
                activity!!.dateText.text = spinnerDatePicker.datePicker.dayOfMonth.toString() + "/" + (spinnerDatePicker.datePicker.month + 1) + "/" + spinnerDatePicker.datePicker.year
            }
            DialogInterface.BUTTON_NEGATIVE -> {

            }
        }
    }

    val builder = AlertDialog.Builder(context!!)
    builder.setTitle(resources.getString(R.string.dialog_title))
        .setView(spinnerDatePicker)
        .setPositiveButton("Ok", dialogClickListener)
        .setNegativeButton("Cancel", dialogClickListener)
        .create()
        .show()
}
B. Go
  • 1,436
  • 4
  • 15
  • 22