1

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.

hata
  • 11,633
  • 6
  • 46
  • 69
TareK Khoury
  • 12,721
  • 16
  • 55
  • 78
  • Take a look at this [post](http://stackoverflow.com/questions/30431250/how-to-achieve-multiple-datepicker-functionality-with-two-buttons-and-save-those/30433506#30433506). – Dhaval Patel Oct 13 '15 at 18:38

3 Answers3

4

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...

}
Atmaram
  • 3,655
  • 2
  • 27
  • 33
  • No, this if statement should be in the onDateSet(), which is a listener callback and i have no reference there to the EditText. Unless of course i set the "chosen" EditText as a global variable. But, i want to know if it's possible to achieve this using the passed DatePicker from the onDateSet(). – TareK Khoury Oct 13 '15 at 18:32
  • Try setting a tag in DatePicker object while creating the dialog and get it on callback to compare it. – Atmaram Oct 13 '15 at 18:49
  • i thought of this also, but there is not setTag method on the picker object :) – TareK Khoury Oct 13 '15 at 18:51
  • 1
    Use picker.getDatePicker().setTag(et.getId()) and getTag in OnDateSet method(). this will work .. you can use something else to set as tag if you don;t like to compare id... I will edit the answer. – Atmaram Oct 13 '15 at 18:55
  • 1
    It worked! this solution is the best instead of using a global variable to save the edittext that is selected. Thanks – TareK Khoury Oct 13 '15 at 19:07
1

You can use this as this code is working in my app

public void showDialog(EditText ed){
    final EditText e = ed;
    e.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            final Calendar c = Calendar.getInstance();
            int mYear = c.get(Calendar.YEAR); // current year
            int mMonth = c.get(Calendar.MONTH); // current month
            int mDay = c.get(Calendar.DAY_OF_MONTH); // current day

            dpd = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker datePicker, int year, int monthOfYear, int dayOfMonth) {
                    e.setText(dayOfMonth + " / "+ (monthOfYear+1) + " / " + year);
                }
            },mYear,mMonth,mDay);
            dpd.show();
        }
    });
}
0
DatePickerDialog picker= new DatePickerDialog(getActivity(), this, year, month,day);
picker.getDatePicker().setTag("id")
Sagar Devanga
  • 2,815
  • 27
  • 30