How to Disable Previous or Future Dates in Datepicker in Android?
Last Updated :
26 Dec, 2022
In this article, we are going to disable previous or future dates in Datepicker. Most of the time when we use DatePicker in Android we see that all the date in that is enabled. We can select any of the dates. But here we are going to see how to disable past or future dates. Disabling the Past Date can be useful when we are assigning tasks to someone. Then the selected date must be in the future. Disabling the Previous Date can be useful when we are asking for the date of birth for someone. Then the selected date must be in past. Let's implement this feature.
Step by Step Implementation
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
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"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="@+id/edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Step 3: Working with the MainActivity.java file
Go to the MainActivity.java file and refer to the following code.
For Max Date:
Get the DatePicker from DatePickerDialog with getDatePicker(). Set the max date to the current date with setMaxDate():
Note: Requires API level 11.
datePicker.getDatePicker().setMaxDate(calendar.getTimeInMillis());
Below is the code for the MainActivity.java file.
Java
import android.app.DatePickerDialog;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
EditText editText;
DatePickerDialog datePicker;
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initialising the calendar
final Calendar calendar = Calendar.getInstance();
// initialising the layout
editText = findViewById(R.id.edittext);
final int day = calendar.get(Calendar.DAY_OF_MONTH);
final int year = calendar.get(Calendar.YEAR);
final int month = calendar.get(Calendar.MONTH);
// initialising the datepickerdialog
datePicker = new DatePickerDialog(MainActivity.this);
// click on edittext to set the value
editText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
datePicker = new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(android.widget.DatePicker view, int year, int month, int dayOfMonth) {
// adding the selected date in the edittext
editText.setText(dayOfMonth + "/" + (month + 1) + "/" + year);
}
}, year, month, day);
// set maximum date to be selected as today
datePicker.getDatePicker().setMaxDate(calendar.getTimeInMillis());
// show the dialog
datePicker.show();
}
});
}
}
For min Date:
Similarly, Get the DatePicker from DatePickerDialog with getDatePicker(). Set the min date to the current date with setMinDate():
datePicker.getDatePicker().setMinDate(calendar.getTimeInMillis());
Below is the code for the MainActivity.java file.
Java
import android.app.DatePickerDialog;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
EditText editText;
DatePickerDialog datePicker;
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initialising the calendar
final Calendar calendar = Calendar.getInstance();
// initialising the layout
editText = findViewById(R.id.edittext);
final int day = calendar.get(Calendar.DAY_OF_MONTH);
final int year = calendar.get(Calendar.YEAR);
final int month = calendar.get(Calendar.MONTH);
// initialising the datepickerdialog
datePicker = new DatePickerDialog(MainActivity.this);
// click on edittext to set the value
editText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
datePicker = new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(android.widget.DatePicker view, int year, int month, int dayOfMonth) {
// adding the selected date in the edittext
editText.setText(dayOfMonth + "/" + (month + 1) + "/" + year);
}
}, year, month, day);
// set maximum date to be selected as today
datePicker.getDatePicker().setMinDate(calendar.getTimeInMillis());
// show the dialog
datePicker.show();
}
});
}
}
Output:
Similar Reads
Android Architecture Android architecture contains a different number of components to support any Android device's needs. Android software contains an open-source Linux Kernel having a collection of a number of C/C++ libraries which are exposed through application framework services. Among all the components Linux Kern
5 min read
Android Tutorial In this Android Tutorial, we cover both basic and advanced concepts. So whether you are a fresher (graduate) or an experienced candidate with several years of Android Development experience, you can follow this Android tutorial to kick-start your journey in Android app development. Our Android Tutor
15+ min read
Activity Lifecycle in Android with Demo App In Android, an activity is referred to as one screen in an application. It is very similar to a single window of any desktop application. An Android app consists of one or more screens or activities. Each activity goes through various stages or a lifecycle and is managed by activity stacks. So when
9 min read
Introduction to Android Development Android operating system is the largest installed base among various mobile platforms across the globe. Hundreds of millions of mobile devices are powered by Android in more than 190 countries of the world. It conquered around 71% of the global market share by the end of 2021, and this trend is grow
5 min read
Top 50 Android Interview Questions and Answers - SDE I to SDE III A Linux-based open-source OS, Android was created by Andy Rubin and became one of the most popular smartphone operating systems. With 71 percent of the market share worldwide, Android is on top. Because it is on top in the smartphone OS, Android development is always in demand.If you are seeking a j
15+ min read
Android UI Layouts Layouts in Android define the user interface and hold UI controls or widgets that appear on the screen of an application. Every Android application consists of View and ViewGroup elements. Since an application contains multiple activitiesâeach representing a separate screenâevery activity has multip
5 min read
Components of an Android Application There are some necessary building blocks that an Android application consists of. These loosely coupled components are bound by the application manifest file which contains the description of each component and how they interact. The manifest file also contains the appâs metadata, its hardware confi
3 min read
Android Studio Tutorial It is stated that "If you give me six hours to chop down a tree then I will spend the first four hours in sharpening the axe". So in the Android Development World if we consider Android Development as the tree then Android Studio should be the axe. Yes, if you are starting Android Development then y
9 min read
MVVM (Model View ViewModel) Architecture Pattern in Android Developers always prefer clean and structured code for projects. Organizing the codes according to a design pattern helps in the maintenance of the software. By having knowledge of all crucial logic parts of the android application, it is easier to add and remove app features. Further, design patter
8 min read
What is Intent in Android? In Android, it is quite usual for users to witness a jump from one application to another as a part of the whole process, for example, searching for a location on the browser and witnessing a direct jump into Google Maps or receiving payment links in Messages Application (SMS) and on clicking jumpin
4 min read