Set MIN and MAX Selectable Dates in DatePicker Dialog in Android

09 Dec, 2021
Applications like ticket booking for traveling, theatres, movies, plays, appointments, reminders, etc., require the user to pick a specific day or time for confirming a slot or a booking. In most of these applications, a service is to be picked and then the user has to pick up a slot to further proceed. At this point, while picking up a date, the current calendar month or the nearest available date is generally displayed. However, not all the dates show availability for the slot. Knowingly, a past date is always greyed-out, meaning one cannot book a slot on a past date. Moreover, if the service provider has a limited number of slots or no slots for a particular day in the future, those dates are greyed-out. Similarly, the service providers may also take bookings for a range of dates, which they can facilitate accordingly. So in this article, we will show you how you could display a calendar and make a range of dates selectable in it. Follow the below steps once the IDE launched.
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.
Step 2: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.
XML
<?xml version="1.0" encoding="utf-8"?><RelativeLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity"></RelativeLayout>
Step 3: Working with the MainActivity.kt file
Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
Kotlin
import android.app.DatePickerDialogimport androidx.appcompat.app.AppCompatActivityimport android.os.Bundleimport java.util.*  class MainActivity : AppCompatActivity() {    override fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        setContentView(R.layout.activity_main)          // Get instance of calendar        // mCalendar will be set to current/today's date        val mCalendar = Calendar.getInstance()          // Creating a simple calendar dialog.           // It was 9 Aug 2021 when this program was developed.        val mDialog = DatePickerDialog(this, { _, mYear, mMonth, mDay ->            mCalendar[Calendar.YEAR] = mYear            mCalendar[Calendar.MONTH] = mMonth            mCalendar[Calendar.DAY_OF_MONTH] = mDay        }, mCalendar[Calendar.YEAR], mCalendar[Calendar.MONTH], mCalendar[Calendar.DAY_OF_MONTH])          // Changing mCalendar date from current to          // some random MIN day 15/08/2021 15 Aug 2021        // If we want the same current day to be the MIN day,          // then mCalendar is already set to today           // and the below code will be unnecessary        val minDay = 15        val minMonth = 8        val minYear = 2021        mCalendar.set(minYear, minMonth-1, minDay)        mDialog.datePicker.minDate = mCalendar.timeInMillis          // Changing mCalendar date from current to          // some random MAX day 20/08/2021 20 Aug 2021        val maxDay = 20        val maxMonth = 8        val maxYear = 2021        mCalendar.set(maxYear, maxMonth-1, maxDay)        mDialog.datePicker.maxDate = mCalendar.timeInMillis          // Display the calendar dialog        mDialog.show()      }}
Output:
You can see that we can select dates from a particular range only. The rest are greyed-out.
Android
Kotlin
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.