How to disable past dates in Android date picker?
You can do
datePicker.setMinDate(System.currentTimeMillis() - 1000);
which sets today's date as minimum date and all the past dates are disabled.
datePicker
is an object of DatePicker
if you are using an object of DatePickerDialog
you can do
datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
Note: setMinDate
was introduced in API 11
Disable past date in DatePickerDialog
First of all declare as following:
private Calendar myCalendar = Calendar.getInstance();
then like your code:-
DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
String selectedDate = dateFormat.format(calendar.getTime());
btnDate.setText(selectedDate);
btnDate.setTextColor(getResources().getColor(colorPrimaryDark));
}
};
make this change in your code instead of your last two lines:
`Date newDate = calendar.getTime();
new DatePickerDialog(TutorAddNewTimeSlotActivity.this, dateSetListener,
calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)).show();`
change it and make it like following:
datePickerDialog.getDatePicker().setMinDate(myCalendar.getTimeInMillis());
datePickerDialog.show();
which will disable past date.
How to disable past dates in MaterialDatePicker?
You can build your own DateValidator
or you can just use the DateValidatorPointForward
provided by the library.
Something like:
//Returns a DateValidator which enables days from {@code point}, in
//UTC milliseconds, forward
CalendarConstraints.DateValidator dateValidator = DateValidatorPointForward.from(yourDate);
constraintsBuilder.setValidator(dateValidator);
Android DatePicker : Disabling the PREVIOUS dates based on the CURRENT date
private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
// the callback received when the user "sets" the Date in the
// DatePickerDialog
public void onDateSet(DatePicker view, int yearSelected,
int monthOfYear, int dayOfMonth) {
year = yearSelected;
month = monthOfYear + 1;
day = dayOfMonth;
// Set the Selected Date in Select date Button
txtarrivedate.setText(year + "-" + month + "-" + day);
}
};
// Method automatically gets Called when you call showDialog()
method
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case 1:
// create a new DatePickerDialog with values you want to show
DatePickerDialog da = new DatePickerDialog(this, mDateSetListener,
mYear, mMonth, mDay);
Calendar c = Calendar.getInstance();
c.add(Calendar.DATE, 1);
Date newDate = c.getTime();
da.getDatePicker().setMinDate(newDate.getTime());
return da;
// create a new TimePickerDialog with values you want to show
case 2:
// create a new DatePickerDialog with values you want to show
DatePickerDialog da1 = new DatePickerDialog(this,
mDateSetListener2, mYear, mMonth, mDay);
Calendar c1 = Calendar.getInstance();
c1.add(Calendar.DATE, 1);
Date newDate2 = c1.getTime();
da1.getDatePicker().setMinDate(newDate2.getTime());
return da1;
}
return null;
}
only use use the for calling dialog
showDialog(1);
How to block only past dates in DatePicker in android
You can also try this:
da.getDatePicker().setMinDate(newDate.getTime()-(newDate.getTime()%(24*60*60*1000)));
It basically subtracts the current date by the current offset of the day.
How to disable dates before today date in DatePickerDialog Android?
See this example..!
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
public class MyAndroidAppActivity extends Activity {
private TextView tvDisplayDate;
private Button btnChangeDate;
private int myear;
private int mmonth;
private int mday;
static final int DATE_DIALOG_ID = 999;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setCurrentDateOnView();
addListenerOnButton();
}
// display current date
public void setCurrentDateOnView() {
tvDisplayDate = (TextView) findViewById(R.id.tvDate);
final Calendar c = Calendar.getInstance();
myear = c.get(Calendar.YEAR);
mmonth = c.get(Calendar.MONTH);
mday = c.get(Calendar.DAY_OF_MONTH);
// set current date into textview
tvDisplayDate.setText(new StringBuilder()
// Month is 0 based, just add 1
.append(mmonth + 1).append("-").append(mday).append("-")
.append(myear).append(" "));
}
public void addListenerOnButton() {
btnChangeDate = (Button) findViewById(R.id.btnChangeDate);
btnChangeDate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
// set date picker as current date
DatePickerDialog _date = new DatePickerDialog(this, datePickerListener, myear,mmonth,
mday){
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth)
{
if (year < myear)
view.updateDate(myear, mmonth, mday);
if (monthOfYear < mmonth && year == myear)
view.updateDate(myear, mmonth, mday);
if (dayOfMonth < mday && year == myear && monthOfYear == mmonth)
view.updateDate(myear, mmonth, mday);
}
};
return _date;
}
return null;
}
private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
// when dialog box is closed, below method will be called.
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
myear = selectedYear;
mmonth = selectedMonth;
mday = selectedDay;
// set selected date into textview
tvDisplayDate.setText(new StringBuilder().append(mmonth + 1)
.append("-").append(mday).append("-").append(myear)
.append(" "));
}
};
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btnChangeDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change Date" />
<TextView
android:id="@+id/lblDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Current Date (M-D-YYYY): "
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/tvDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
Related Topics
Recyclerview - How to Smooth Scroll to Top of Item on a Certain Position
Google Android Usb Driver and Adb
How to Set Relativelayout Layout Params in Code Not in Xml
Trigger Mediascanner on Specific Path (Folder), How To
Why Do I Get Access Denied to Data Folder When Using Adb
Change Toolbar Color in Appcompat 21
Error: "Adb Connection Error:An Existing Connection Was Forcibly Closed by the Remote Host"
How to Turn On/Off Wifi Hotspot Programmatically in Android 8.0 (Oreo)
How to Simulate :Active CSS Pseudo Class in Android on Non-Link Elements
Can an Android App Connect Directly to an Online MySQL Database
How to Enable Logging for Apache Commons Httpclient on Android
Android Design Support Library for API 28 (P) Not Working
Could Not Find Com.Android.Tools.Build:Aapt2:3.2.0
Does Android Keep the .Apk Files? If So Where
Firebase (Fcm) How to Get Token
How to Put Gridview Inside Scrollview
Adt 22.2 New App Wizard: Unsupported Template Dependency: Upgrade Your Android Eclipse Plugin