6

I want to be able to detect changes on datepicker as soon as user starts to change it. However, I don't want to use datrpickerdialog. Does anyone know why the onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) doesn't fire when I change the date on datepicker?

I would highly appreciate any comment on that. Cheers

Here is the simplified version of my code:

public class AddItem extends Activity  implements OnDateChangedListener,OnDateSetListener{

DatePicker start ;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.entryelements);
    start = (DatePicker) findViewById(R.id.start);

@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear,
        int dayOfMonth) {

    // TODO Auto-generated method stub
    Toast.makeText(getApplicationContext(),
            " thiv view"+year,
            Toast.LENGTH_LONG).show();
    Log.v("indatechange", "ok");

}

@Override
public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) {
    // TODO Auto-generated method stub
    Toast.makeText(getApplicationContext(),
            " thiv view"+arg0,
            Toast.LENGTH_LONG).show();
    Log.v("indatechange", "ok");
}

}

Alireza Noorali
  • 3,129
  • 2
  • 33
  • 80
Zardaloop
  • 1,594
  • 5
  • 22
  • 43

4 Answers4

8

You need to tell the DatePicker about your OnDateChangedListener. Unlike regular widgets a DatePicker does not have setOnDateChangedListener()... but it does have init(). Use init() to set the default date and declare the listener together:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.entryelements);

    // Set the date to now
    Calendar calendar = Calendar.getInstance();
    start = (DatePicker) findViewById(R.id.start);
    start.init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), this);
}
Sam
  • 86,580
  • 20
  • 181
  • 179
0

You'll need to call the init(year, month, day, onDateChangedListener) method on your DatePicker to set it up, if you want to be proactively informed of interactions.

Assuming you want to initialize it to show a particular date to begin with, it makes sense to call this instead of its updateDate method (which is what you're doing? Or are you not caring what the starting value is?)

Note that it doesn't support an OnDateSetListener the way DatePickerDialog does, since that would imply that the UI has some way of knowing when the user is done interacting with the picker. Since it's not a dialog, you have to decide yourself when you think they're done. Just keep track with the OnDateChangedListener, and it'll make sense.

0
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.entryelements);
start = (DatePicker) findViewById(R.id.start);

expirationDate=(EditText) findViewById(R.id.expirationDate);
Calendar calendar = Calendar.getInstance();
start = (DatePicker) findViewById(R.id.start);
MyOnDateChangeListener onDateChangeListener = new MyOnDateChangeListener();
start.init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), onDateChangeListener);
}

public class MyOnDateChangeListener implements OnDateChangedListener {
    @Override
    public void onDateChanged(DatePicker view, int year, int month, int day) {
        int mon=month+1;
        expirationDate.setText(day+"/"+mon+"/"+year);
    }
}
0
DatePicker dpEndDate,dpStartDate;
dpStartDate=(DatePicker)findViewById(R.id.dpStartDate);
dpEndDate=(DatePicker)findViewById(R.id.dpEndDate);
  Calendar today = Calendar.getInstance();
        dpStartDate.init(today.get(Calendar.YEAR),
                today.get(Calendar.MONTH),
                today.get(Calendar.DAY_OF_MONTH),
                new DatePicker.OnDateChangedListener() {
                    @Override
                    public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                        SmartUtils.ting(MicaAttendanceFilter.this,"Start Date Changes");
                        try {
                            SimpleDateFormat simpleDateFormat=new SimpleDateFormat("dd M yyyy");
                            Calendar startCalendar = Calendar.getInstance();
                            int startDay=dpStartDate.getDayOfMonth();
                            int startMonth=dpStartDate.getMonth()+1;
                            int startYear=dpStartDate.getYear();
                            String startDate = startDay+" "+startMonth+" "+startYear;
                            startCalendar.setTime(simpleDateFormat.parse(startDate));
                            dpEndDate.setMinDate(startCalendar.getTimeInMillis());
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                    }
                }
        );
Pratik Vyas
  • 644
  • 7
  • 20