There is an event listener in Android called DatePicker.OnDateChangedListener. I am trying to set a DatePicker view's on date changed listener as follows:
DatePicker dp = new DatePicker(getContext()); dp.setOnDateChangedListener(this); //where this is my activity extends DatePicker.OnDateChangedListener
But guess what? Date picker does not have a method called setOnDateChangedListener.
My question is:
Any documentation/tutorials will be very helpful.
This example demonstrates how to use android date change listener. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml.
In the DatePicker class, there is a method called the getCalendarView, and it is the method you can call if you want to get your hands on the contained CalendarView. Once you get your hands on the contained CalendarView, then, needlessly to say, you can call its setOnDateChangeListener
Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml. In the above code, we have declare date picker to select the date. when you select the date it going to show current date on Toast message.
Any documentation/tutorials will be very helpful. Once you've created your DatePicker, you need to initialize it with the date you want to display at first. That's the point at which you can add your listener. See DatePicker.init (int, int, int, OnDateChangedListener).
Once you've created your DatePicker
, you need to initialize it with the date you want to display at first. That's the point at which you can add your listener.
See DatePicker.init(int, int, int, OnDateChangedListener)
.
Update
26 API allows to set listener: DatePicker.setOnDateChangedListener()
Best way is
DatePicker datePicker = (DatePicker) findViewById(R.id.datePicker); Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(System.currentTimeMillis()); datePicker.init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), new DatePicker.OnDateChangedListener() { @Override public void onDateChanged(DatePicker datePicker, int year, int month, int dayOfMonth) { Log.d("Date", "Year=" + year + " Month=" + (month + 1) + " day=" + dayOfMonth); } });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With