I have an activity with 2 EditText (startDate and endDate).
I have created a function that i pass it an EditText and it will show a DatePicker:
private void showDatePicker(EditText et) {
Calendar calendar = Calendar.getInstance();
DatePickerDialog picker = new DatePickerDialog(getActivity(), this, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
if (!Global.isEmpty(et)) {
try {
SimpleDateFormat sdf = new SimpleDateFormat(Global.USER_FRIENDLY_DATE_FORMAT, Locale.getDefault());
calendar.setTime(sdf.parse(et.getText().toString()));
} catch (ParseException e) {
Toast.makeText(getActivity(), "Error setting the date", Toast.LENGTH_SHORT).show();
}
}
picker.show();
}
When i pick a date, i will get a callback to the onDateSet() function with the date choosen.
My questions is, how can i tell for which EditText it is?
I know there is a "DatePicker view" that is passed as an argument to the onDateSet(), but cannot seem to figure out how to use it or even if i should.
You can set a Tag to DatePicker object of your DatepickerDialog
by calling picker.getDatePicker()
and setTag()
.The same tag can be retrieved from the datePicker object in your call back function onDateSet()
like
DatePickerDialog picker = new DatePickerDialog(getActivity(), listner , calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
DatePicker pick = picker.getDatePicker().setTag(et.getId());
and
public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
Log.d("", " " + datePicker.getTag());
// based on the returned tag you can decide what to do...
}