I have created an activity in which there is a field for inserting birth date of the user. I have used a date picker to achieve this.
This is the source code of what I am doing:
// onclick dob edittext open date picker dialog
dob.setFocusableInTouchMode(false);
dob.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//open date picker
showDialog(DATE_DIALOG_ID);
}
});
//======================For picking birth day==========================
// Creating dialog
@Override
protected Dialog onCreateDialog(int id) {
Calendar c = Calendar.getInstance();
int cyear = c.get(Calendar.YEAR);
int cmonth = c.get(Calendar.MONTH);
int cday = c.get(Calendar.DAY_OF_MONTH);
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this, mDateSetListener, cyear, cmonth, cday);
}
return null;
}
private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
// onDateSet method
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
// String date_selected = String.valueOf(monthOfYear+1)+" /"
// +String.valueOf(dayOfMonth)+" /"
// +String.valueOf(year);
String date_selected = String.valueOf(dayOfMonth)+" /"
+String.valueOf(monthOfYear+1)+" /"
+String.valueOf(year);
//Toast.makeText(RegisterActivity.this, "Selected Date is ="+date_selected, Toast.LENGTH_SHORT).show();
dob.setText(date_selected);
}
};
//======================For picking birth day==========================
Now what I want to do is, to limit the date picker to particular year.
Like this:
int maxYear = c.get(Calendar.YEAR) - 15; // this year ( 2013 ) - 15 = 1998
int maxMonth = c.get(Calendar.MONTH);
int maxDay = c.get(Calendar.DAY_OF_MONTH);
int minYear = maxYear-49;
int minMonth = 0; // january
int minDay = 25;
What should I do to set the above mentioned bounding to the date picker?
//----------------Edit---------------------------
I have done something like this:
//======================For picking birth day==========================
// Creating dialog
@Override
protected Dialog onCreateDialog(int id) {
Calendar c = Calendar.getInstance();
maxYear = c.get(Calendar.YEAR) - 15; // this year ( 2013 ) - 15 = 1998
maxMonth = c.get(Calendar.MONTH);
maxDay = c.get(Calendar.DAY_OF_MONTH);
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this, mDateSetListener, maxYear, maxMonth, maxDay);
}
return null;
}
DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
int minYear = maxYear-49;
int minMonth = 0; // january
int minDay = 25;
public void onDateChanged(DatePicker view, int year, int monthOfYear,int dayOfMonth) {
Date current = new Date(year, monthOfYear, dayOfMonth);
Date minDate = new Date(minYear, minMonth, minDay);
Date maxDate = new Date(maxYear, maxMonth, maxDay);
if( current.getTime() < minDate.getTime() )
{
//set to minimum date
}else if( current.getTime() > maxDate.getTime() )
{
//Set to max Date
}
}
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
String date_selected = String.valueOf(dayOfMonth)+" /"
+String.valueOf(monthOfYear+1)+" /"
+String.valueOf(year);
//Toast.makeText(RegisterActivity.this, "Selected Date is ="+date_selected, Toast.LENGTH_SHORT).show();
dob.setText(date_selected);
}
};
//======================For picking birth day==========================
This is fixing the upper limit, but the lower limit or the minDate is not getting fixed! What is to be done?
See Question&Answers more detail:
os