I want to put the limit on date so that user can not pick date more then that, for example if today is 1 January then User should not be able to select more then 7 dates , I mean he can not select 9 January. I also want him not to select the month and year. So I am putting a limit to set his task in one week.
what I have done so far is showing the date picker fragment and setting current date in it. the code in my main activity goes like this:
etSelectDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment datePickerFragment = new DatePickerFragment() {
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
Log.d("Date Change", "onDateSet");
Calendar c = Calendar.getInstance();
c.set(year, month, day);
DateFormat df = DateFormat.getDateInstance();
etSelectDate.setText(df.format(c.getTime()));
//nextField.requestFocus(); //moves the focus to something else after dialog is closed
}
};
datePickerFragment.show(MainActivity.this.getSupportFragmentManager(), "datePicker");
}
});
and date picker fragment class goes like this :
public static class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener{
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
//blah
}
}
till then its is working fine , but I do not know how to put the limit on date and rest of the months and year should be non Select able . I have seen many link such as like this , but I do not understand How can I do that and also there is nothing helpful on android site.
So please help me , How can I put limit of seven days only
Update
Through your replies I know how to set the max date in calender , so As I want to set the max date 7 days ahead of current date , I am still not getting it. the method I read so far is :
pickerDialog.getDatePicker().setMaxDate(new Date().getTime());
It is setting the current date as maximum, but How can I add 7 days ahead in it since it is Date object ? please help
You have the
setMinDate(long)
andsetMaxDate(long)
methods at your disposal. Both of these will work on API level 11 and above. Since you are using aDatePickerDialog
, you need to first get the underlying DatePicker by calling thegetDatePicker()
method.Source :Set Limit on the DatePickerDialog in Android?
You can calculate the minDate by using,
Updated :
Replace the below line
with