Open In App

Set MIN and MAX Selectable Dates in DatePicker Dialog in Android

Last Updated : 24 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 launches.

Step-by-Step Implementation

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: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.DatePickerDialog
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import 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()
 
    }
}


Java




import android.app.DatePickerDialog;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Calendar;
 
public class MainActivity extends AppCompatActivity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        // Get instance of calendar
        // mCalendar will be set to current/today's date
        final Calendar mCalendar = Calendar.getInstance();
 
        // Creating a simple calendar dialog.
        // It was 9 Aug 2021 when this program was
        // developed.
        final DatePickerDialog mDialog
            = new DatePickerDialog(
                this,
                new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(
                        android.widget.DatePicker view,
                        int mYear, int mMonth, int mDay)
                    {
                        mCalendar.set(Calendar.YEAR, mYear);
                        mCalendar.set(Calendar.MONTH,
                                      mMonth);
                        mCalendar.set(Calendar.DAY_OF_MONTH,
                                      mDay);
                    }
                },
                mCalendar.get(Calendar.YEAR),
                mCalendar.get(Calendar.MONTH),
                mCalendar.get(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
        final int minDay = 15;
        final int minMonth = 8;
        final int minYear = 2021;
        mCalendar.set(minYear, minMonth - 1, minDay);
        mDialog.getDatePicker().setMinDate(
            mCalendar.getTimeInMillis());
 
        // Changing mCalendar date from current to
        // some random MAX day 20/08/2021 20 Aug 2021
        final int maxDay = 20;
        final int maxMonth = 8;
        final int maxYear = 2021;
        mCalendar.set(maxYear, maxMonth - 1, maxDay);
        mDialog.getDatePicker().setMaxDate(
            mCalendar.getTimeInMillis());
 
        // Display the calendar dialog
        mDialog.show();
    }
}
// This code is contributed by chinmaya121221


Output:

You can see that we can select dates from a particular range only. The rest are greyed-out.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads