Android DatePicker Date Limit

I use DatePicket in my activity,

I want to limit the date selected by the user to today's date.

They cannot choose a date greater than today's date.

thanks.

+7
source share
4 answers

yes, you can do it very easily, validation here is an example:

if(dateObj1.before(dateObj2) || dateObj1.equals(dateObj2)){ //the program runs normally } else{ new AlertDialog.Builder(PM_Edit.this) .setTitle("Wrong Data Input!") .setMessage("The end Date must be Before the start Date, please insert new Date values") .setNeutralButton("Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { } }).show(); } 

Credits to: http://www.brighthub.com/mobile/google-android/articles/41545.aspx

+7
source
 DatePicker datePicker = (DatePicker)findViewById(R.id.new_date_picker); datePicker.init(year, month, day, new OnDateChangedListener() { @Override public void onDateChanged(DatePicker view, int year, int monthOfYear,int dayOfMonth) { if(isDateAfter(view)){ Calendar mCalendar = Calendar.getInstance(); view.init(mCalendar.get(Calendar.YEAR), mCalendar.get(Calendar.MONTH), mCalendar.get(Calendar.DAY_OF_MONTH), this); } } private boolean isDateAfter(DatePicker tempView) { Calendar mCalendar = Calendar.getInstance(); Calendar tempCalendar = Calendar.getInstance(); tempCalendar.set(tempView.getYear(), tempView.getMonth(), tempView.getDayOfMonth(), 0, 0, 0); if(tempCalendar.after(mCalendar)) return true; else return false; } }); 
+5
source

I did not work with DatePicker, but the documentation does not show any methods that can limit the maximum date that Picker displays, but you can always check the date when it returns and can notify the user of the validation criterion.

+3
source

I agree with the Sheikh.

Perhaps consider checking for an entered date and if it is not valid, notify the user and run DatePicker again.

Other than that, maybe a custom widget, but I have not seen the default method for this.

0
source

All Articles