Android Material Design Inline Datepicker issue

31,212

Solution 1

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.

Solution 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()
}
Share:
31,212
AndreaF
Author by

AndreaF

Updated on January 20, 2020

Comments

  • AndreaF
    AndreaF over 4 years

    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???

  • AndreaF
    AndreaF over 9 years
    THANK YOU! You have saved me!
  • Guillaume Husta
    Guillaume Husta over 9 years
    Why can't I see that attribute in the API ? -> developer.android.com/reference/android/widget/DatePicker.ht‌​ml
  • Adam Rosenfield
    Adam Rosenfield over 9 years
    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.
  • alanv
    alanv over 9 years
    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.
  • Mulgard
    Mulgard over 9 years
    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)
  • akhy
    akhy over 9 years
    @Mulgard no problem, just add tools:ignore="UnusedAttribute" to silent the warning. The android:datePickerMode attribute will be ignored in pre-Lollipop API levels.
  • Newtz
    Newtz almost 9 years
    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.
  • Hiren Patel
    Hiren Patel almost 9 years
    @alanv, Superb solution. +1 for you. :)
  • J Steven Perry
    J Steven Perry about 8 years
    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.
  • Tash Pemhiwa
    Tash Pemhiwa almost 7 years
    Is it possible to set this mode in Java?