Disable future dates on datepicker in android?
Get Current instance of Calendar in Android
Calendar c = Calendar.getInstance();
//Date Formater
SimpleDateFormat dformate = new SimpleDateFormat("dd MMM yyyy");
//calling function
private void openDatepicker() {
int mYear = c.get(Calendar.YEAR);
int mMonth = c.get(Calendar.MONTH);
int mDay = c.get(Calendar.DAY_OF_MONTH);
// Launch Date Picker Dialog
android.app.DatePickerDialog dpd = new android.app.DatePickerDialog(
getActivity(),
R.style.DialogThemeRed,
new android.app.DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,int monthOfYear, int dayOfMonth) {
//Set current selected date to Calendar
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, monthOfYear);
c.set(Calendar.DAY_OF_MONTH, dayOfMonth);
//Set date to textview
yourTextview.setText(dformate.format(c.getTime()));
}
}, mYear, mMonth, mDay);
//Set Max Date for disable future dates
dpd.getDatePicker().setMaxDate(new Date().getTime());
dpd.show();
}
No comments:
Post a Comment