Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - OnDateChangedListener - how do you set this?

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:

  1. How then do you set a date changed listener in Android?
  2. If it is not possible to set a date changed listener, what is the purpose for this event?

Any documentation/tutorials will be very helpful.

like image 638
Tawani Avatar asked Jan 12 '10 18:01

Tawani


People also ask

How to use Android date change listener?

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.

How to set the calendarview setondatechangelistener in datepicker?

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

How to show current date on toast message in Android Studio?

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.

How do I add a listener to a datepicker?

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


2 Answers

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()

like image 76
Christopher Orr Avatar answered Sep 17 '22 02:09

Christopher Orr


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);                  }             }); 
like image 43
turbandroid Avatar answered Sep 17 '22 02:09

turbandroid