I am doing the following in my app:
1.I have two EditText
boxes.
On clicking each of them a
DatePickerDialog
pops up.I set the date in the Datepicker.
On closing the dialog box the date gets set in the edittext.
The problem is as follows.
After setting the date in the datepicker , the datepicker always shows the date I have set and not the current date. i.e. if I set the date as 2014-09-09 when I click the edittext again the date on the datepicker is shown as the above set date and not the present date.
Can anyone guide me how to fix this step by step..
My codes are as follows:
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.edt_date_from:
InputMethodManager imm = (InputMethodManager) getSystemService(this.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(edt_date_to.getWindowToken(), 0);
edt = 1;
showDialog(DATE_DIALOG_ID);
break;
case R.id.edt_date_to:
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
edt = 2;
showDialog(DATE_DIALOG_ID);
break;
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
Calendar cal = Calendar.getInstance();
int iDay,
iMonth,
iYear;
iDay = cal.get(Calendar.DAY_OF_MONTH);
iMonth = cal.get(Calendar.MONTH);
iYear = cal.get(Calendar.YEAR);
DatePickerDialog dp = new DatePickerDialog(this, pDateSetListener,
iYear, iMonth, iDay);
dp.updateDate(iYear, iMonth, iDay);
return dp;
}
return null;
}
private DatePickerDialog.OnDateSetListener pDateSetListener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
years = year;
month = monthOfYear;
day = dayOfMonth;
updateDisplay();
}
};
public void updateDisplay() {
// TODO Auto-generated method stub
if (edt == 1) {
date_frm.setText(new StringBuilder()
.append(day).append("-").append(month + 1).append("-")
.append(years).append(" "));
} else {
edt_date_to.setText(new StringBuilder()
.append(day).append("-").append(month + 1).append("-")
.append(years).append(" "));
}
}
From the docs:
If you insist on using these deprecated activity-managed dialogs and not
DialogFragment
s, move the init code that resets the date toonPrepareDialog()
.