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 version = "1.0" encoding = "utf-8" ?>
< LinearLayout
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.
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.
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: