Saturday, January 19, 2013

DatePicker widget



call the date dialog by

showDialog(DATE_DIALOG_ID);

declare constant
static final int DATE_DIALOG_ID = 333;



@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
// set date picker as current date
return new DatePickerDialog(this, datePickerListener, year, month,
day);
}
return null;
}

private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {

// when dialog box is closed, below method will be called.
public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay)
 {

year = selectedYear;
month = selectedMonth;
day = selectedDay;

editext.setText(new StringBuilder()
// Month is 0 based, just add 1
.append(day).append("/").append(month + 1).append("/")
.append(year).append(" "));

}
};

No comments:

Post a Comment