To set a custom date in android date picker, you have to use the Calendar object to set a previous date like following:

static final int DATE_DIALOG_ID = 0;

final Calendar c = Calendar.getInstance();
// setting jun-20-2011
c.set(2011, 5, 20);
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);

mPickDate = (EditText) findViewById(R.id.dateDisplay);

mPickDate.setText(
new StringBuilder()
// Jan is 0, so add 1
.append(mMonth + 1).append("-")
.append(mDay).append("-")
.append(mYear).append(" "));

mPickDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {

showDialog(DATE_DIALOG_ID);

}
});



protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this,  mDateSetListener, mYear, mMonth, mDay);

}
return null;
}

Incoming search terms:

  • DATE IN ANDROID

Tagged with:

Filed under: Android

Like this post? Subscribe to my RSS feed and get loads more!