Android datepicker and timepicker issue
-
22-06-2021 - |
Pregunta
The problem that am facing is with the from-to date and time dialog's. I have created all of them separate. On onCreate i have set the current date and time on the dialog's for the user. i.e for eg 24/7/2012 as todays date. When ever i change/click on the dialog for the from_date to the date for eg: 26/7/2012 then i have change the text of the to_date dialog to 26/7/2012 via setText. The issue that comes here is when i open the to_date dialog the date that appears in the dialog is 24/7/2012 and not 26/7/2012. How can this be achieved same for date and time picker dialog. I have attached the code below. Thanks in advance.
public Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID_FROM:
Log.i("Date Case", "" + from_day);
return new DatePickerDialog(this, dateListenerfrom, from_year,
from_month, from_day);
case DATE_DIALOG_ID_TO:
Log.i("Date Case", "" + to_day);
return new DatePickerDialog(this, dateListenerto, to_year,
to_month, to_day);
case TIME_DIALOG_ID_FROM:
return new TimePickerDialog(this, timeListenerfrom, from_hours,
from_min, false);
case TIME_DIALOG_ID_TO:
return new TimePickerDialog(this, timeListenerto, to_hours, to_min,
false);
}
return null;
}
private DatePickerDialog.OnDateSetListener dateListenerfrom = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int yr, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
from_year = yr;
from_month = monthOfYear;
from_day = dayOfMonth;
// to_year = yr;
// to_month = monthOfYear;
to_day = dayOfMonth;
Log.i("From Day", "" + from_day);
Log.i("To Day", "" + to_day);
Log.i("To Cons", "" + dayOfMonth);
try {
updateDateFrom();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
private DatePickerDialog.OnCancelListener mDateFromCancelListener = new DatePickerDialog.OnCancelListener() {
public void onCancel(DialogInterface dialog) {
}
};
private DatePickerDialog.OnDateSetListener dateListenerto = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int yr, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
to_year = yr;
to_month = monthOfYear;
to_day = dayOfMonth;
Log.i("From Day", "" + from_day);
Log.i("To Day", "" + to_day);
Log.i("To Cons", "" + dayOfMonth);
try {
updateDateTo();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
private void updateDateFrom() throws ParseException {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm");
try {
date_from = dateFormat.parse(from_day + "/" + from_month + "/"
+ from_year + " " + from_hours + ":" + to_min);
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println("From LONG " + date_from.getTime());
from_date_builder = new StringBuilder().append(from_day).append('/')
.append(from_month + 1).append('/').append(from_year);
from_date.setText(from_date_builder.toString());
to_date.setText(new StringBuilder().append(from_day).append('/')
.append(from_month + 1).append('/').append(from_year));
}
private void updateDateTo() throws ParseException {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm");
try {
date_to = dateFormat.parse(to_day + "/" + to_month + "/" + to_year
+ " " + to_hours + ":" + to_min);
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println("To LONG " + date_to.getTime());
if (date_from.getTime() <= date_to.getTime()) {
to_date_builder = new StringBuilder().append(to_day).append('/')
.append(to_month + 1).append('/').append(to_year);
to_date.setText(to_date_builder.toString());
} else {
Toast.makeText(this, "Please set proper date", Toast.LENGTH_SHORT)
.show();
}
}
private TimePickerDialog.OnTimeSetListener timeListenerfrom = new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
from_hours = hourOfDay;
from_min = minute;
try {
updateTimeFrom();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
private TimePickerDialog.OnTimeSetListener timeListenerto = new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
to_hours = hourOfDay;
to_min = minute;
try {
updateTimeTo();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
private void updateTimeFrom() throws ParseException {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm");
time_from = dateFormat.parse(from_day + "/" + from_month + "/"
+ from_year + " " + from_hours + ":" + from_min);
from_time_builder = new StringBuilder().append(from_hours).append(':')
.append(from_min);
from_time.setText(from_time_builder.toString());
to_time.setText(new StringBuilder().append(from_hours).append(':')
.append(from_min));
}
private void updateTimeTo() throws ParseException {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm");
time_to = dateFormat.parse(to_day + "/" + to_month + "/" + to_year
+ " " + to_hours + ":" + to_min);
if (time_from.getTime() <= time_to.getTime()
&& date_from.getTime() <= date_to.getTime()) {
to_time_builder = new StringBuilder().append(to_hours).append(':')
.append(to_min);
to_time.setText(to_time_builder.toString());
}
else {
Toast.makeText(this, "Please set proper date", Toast.LENGTH_SHORT)
.show();
}
System.out.println("Thi is in the time to ");
}
This is onClick {
if (v == from_date) {
showDialog(DATE_DIALOG_ID_FROM);
}
if (v == to_date) {
showDialog(DATE_DIALOG_ID_TO);
}
if (v == from_time) {
showDialog(TIME_DIALOG_ID_FROM);
}
if (v == to_time) {
showDialog(TIME_DIALOG_ID_TO);
}
}
Solución
You have to implement the Below method in your Activity to update the Date/Time to be shown in the Dialog Box.
protected void onPrepareDialog(int id, Dialog dialog) {
switch (id) {
case DATE_DIALOG_ID_TO:
case DATE_DIALOG_ID_FROM:
((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay);
break;
}
}
Otros consejos
If you want to show from_date by default for to_date, you can change your to_date datepicker to show from date values.
case DATE_DIALOG_ID_TO:
Log.i("Date Case", "" + to_day);
return new DatePickerDialog(this, dateListenerto, from_year,
from_month, from_day);
Or, you can set to_year, to_month and to_day from onDateSet function of from_date picker.