0

I placed a datepicker on a layout to let the user select their birthday. I have used datepickerdialogs earlier by clicking on a button and bringing the dialog up. But this time the datepicker itself is on the layout and I can select a date. How can I get the date every time the user changes the day, month or the year?

enter image description here

Solution:

dpResult = (DatePicker) findViewById(R.id.dpResult);

dpResult.init(year, month, day, new OnDateChangedListener() {
    @Override
    public void onDateChanged(DatePicker view, int year, int monthOfYear,int dayOfMonth) {

           //here I got all the values I need
    }
    });
erdomester
  • 11,789
  • 32
  • 132
  • 234
  • do you want to show the date and time on a textView in layout as the user change date? – Ashish Kumar Jul 13 '12 at 16:38
  • 1
    This question may help you : http://stackoverflow.com/questions/2051153/android-ondatechangedlistener-how-do-you-set-this – Olwaro Jul 13 '12 at 16:39

1 Answers1

1

I guess you have to use init(int year, int monthOfYear, int dayOfMonth, DatePicker.OnDateChangedListener yourListenerHere). Did you try this ?

AMerle
  • 4,354
  • 1
  • 28
  • 43
  • One other thing: this works when I change the date by clicking the + and - buttons. But how do I handle when I manually enter the date? – erdomester Jul 14 '12 at 13:53
  • @erdomester Regarding the source code of DatePicker and NumberPicker, I'm afraid you have to make your own component if onDateChangeListener does not work :/ Is your listener called when you change the focus ? – AMerle Jul 14 '12 at 14:08
  • At first I have Jul 14 2012 in the DatePicker. Now I click on 2012 and change it to 1980 manually. I show the actual date in a textview that still says Jul 14 2012. Now I click on the month, and the textviews shows Jul 14 1979. Now I change 14 to 25 manually, the textview is unchanged. Now I click on the year and the textview says Jul 25 1979. So until I remove the cursor from the day/mth/yr box, the datepicker doesn't realize the change – erdomester Jul 14 '12 at 18:03
  • I found it! In the onclicklistener method of my button I put dpResult.clearChildFocus(getCurrentFocus()); that removes the focus of the actual edittext, so the onDateChanged works :) – erdomester Jul 14 '12 at 18:31