Android Material Design Inline Datepicker issue
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()
}