How set maximum date in datepicker dialog in android?
Use setMaxDate()
.
For example, replace return new DatePickerDialog(this, pDateSetListener, pYear, pMonth, pDay)
statement with something like this:
DatePickerDialog dialog = new DatePickerDialog(this, pDateSetListener, pYear, pMonth, pDay);
dialog.getDatePicker().setMaxDate(new Date().getTime());
return dialog;
Android DatePickerDialog: Set min and max date for selection
Try this method
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dialog = new DatePickerDialog(getContext(), listener, year, month, day);
Field mDatePickerField;
try {
mDatePickerField = dialog.getClass().getDeclaredField("mDatePicker");
mDatePickerField.setAccessible(true);
} catch (Exception e) {
e.printStackTrace();
}
dialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
return dialog;
}
instead of your
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dialog = new DatePickerDialog(getContext(), listener, year, month, day);
dialog.getDatePicker().setMinDate(calendar.getTimeInMillis());
return dialog;
}
EDIT1:
I have also faced this issue that user can select not-selectable dates in Android L 5.0.2
. Currently there is bug reported here. It is solved in Android L 5.1.0
.
For temporary solution of this issue you can compare selected date with current system date and put some condition based on that. I used this as my workaround
EDIT2:
add onDateSent()
method in DatePickerDialogFragment
and just check if it's earlier than the date you set in setMinDate()
. If so, then just show the DatePickerDialog
again.
final long today = System.currentTimeMillis() - 1000;
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Calendar calendar = Calendar.getInstance();
calendar.set(year, monthOfYear, dayOfMonth);
//If user tries to select date in past (or today)
if (calendar.getTimeInMillis() < today)
{
//Make them try again
DatePickerDialogFragment fragment = new DatePickerDialogFragment();
fragment.setListener(dateSetListener);
fragment.show(getSupportFragmentManager(), "Choose booking date");
Toast.makeText(this, "Invalid date, please try again", Toast.LENGTH_LONG).show();
}
else
{
//success
}
}
How to set a minimum and maximum limit for a date picker in android?
Try this, This shows from today to next 5 year date in datepicker dialog.
public class MainActivity extends Activity {
TextView txtDatePicker;
Calendar cal,cal1;
long maxDate;
Date date;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cal = Calendar.getInstance();
cal1 = Calendar.getInstance();
txtDatePicker = (TextView) findViewById(R.id.txtDatePicker);
txtDatePicker.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
DatePickerDialog dialog = new DatePickerDialog(MainActivity.this, new OnDateSetListener() {
@Override
public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, ""+arg1+"/"+(arg2+1)+"/"+arg3, Toast.LENGTH_SHORT).show();
}
}, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
dialog.getDatePicker().setMinDate(System.currentTimeMillis());
cal1.add(Calendar.YEAR, 5);
dialog.getDatePicker().setMaxDate(cal1.getTimeInMillis());
dialog.show();
}
});
}
}
How to set the limit on date in Date picker dialog
You have the setMinDate(long)
and setMaxDate(long)
methods at your disposal. Both of these will work on API level 11 and above. Since you are using a DatePickerDialog
, you need to first get the underlying DatePicker by calling the getDatePicker()
method.
dpdialog.getDatePicker().setMinDate(minDate);
dpdialog.getDatePicker().setmaxDate(maxDate);
Source :Set Limit on the DatePickerDialog in Android?
You can calculate the minDate by using,
Date today = new Date();
Calendar c = Calendar.getInstance();
c.setTime(today);
c.add( Calendar.MONTH, -6 ) // Subtract 6 months
long minDate = c.getTime().getTime() // Twice!
Updated :
Replace the below line
return new DatePickerDialog(getActivity(), this, year, month, day);
with
// Create a new instance of DatePickerDialog and return it
DatePickerDialog pickerDialog = new DatePickerDialog(getActivity(), this, year, month, day);
pickerDialog.getDatePicker().setMaxDate(maxDate);
pickerDialog.getDatePicker().setMinDate(minDate);
return pickerDialog;
Set MinDate and MaxDate in DatePIcker
To set the min date two years before and max two years after today use the following code:
Calendar c = Calendar.getInstance();
c.add(Calendar.YEAR, -2); // subtract 2 years from now
datePickerDialog.getDatePicker().setMinDate(c.getTimeInMillis());
c.add(Calendar.YEAR, 4); // add 4 years to min date to have 2 years after now
datePickerDialog.getDatePicker().setMaxDate(c.getTimeInMillis());
how can set max date in date picker kotlin android
In Kotlin, you can do something like
DatePickerDialog(
it1, date, myCalendar[Calendar.YEAR],
myCalendar[Calendar.MONTH],
myCalendar[Calendar.DAY_OF_MONTH]
)
.apply { datePicker.maxDate = Date().time }
.show()
Max Date on an Android DatePickerDialog
The getDatePicker() method is available in API Level 11.
Related Topics
Using Alarmmanager to Start a Service at Specific Time
How to Change Datepicker Dialog Color for Android 5.0
How to Programmatically Create and Read Wep/Eap Wifi Configurations in Android
Remove Fragment Page from Viewpager in Android
How to Remove Application from Recent Application List
Bad Image Quality After Resizing/Scaling Bitmap
Android Transparent Status Bar and Actionbar
Difference Between Getdefaultsharedpreferences and Getsharedpreferences
Activity Ondestroy Never Called
Constraintlayout: Change Constraints Programmatically
Receive Result from Dialogfragment
Android Edittext Delete(Backspace) Key Event
Does Android Support Near Real Time Push Notification
Programmatically Go Back to the Previous Fragment in the Backstack
Android: Combining Text & Image on a Button or Imagebutton