6

My idea is to use an AlertDialog and set to it a a DatePicker.
My code is:

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    DatePicker picker = new DatePicker(this);
    picker.setCalendarViewShown(false);

    builder.setTitle("Create Year");
    builder.setView(picker);
    builder.setNegativeButton("Cancel", null);
    builder.setPositiveButton("Set", null);

    builder.show();

it works, but my question is: how can i have the listeners on the set and cancel button?
About the set button i would like to have the listener and (of course) get the date of the user (his date selection)


I want also show just the year so i want to hide the day and month data.

Giovanni Far
  • 1,623
  • 6
  • 23
  • 37

1 Answers1

7

You can use setPositiveButton, like this :

dialog.setPositiveButton(R.string.dialog_ok, new DialogInterface.OnClickListener() {
  public void onClick(DialogInterface dialog, int id) {
    //do something         
  }
});

The same for setNegativeButton.

I want also show just the year so i want to hide the day and month data

See this, use the 1st answer :

Hide Year field in Android DatePicker?

Community
  • 1
  • 1
Blaze Tama
  • 10,828
  • 13
  • 69
  • 129
  • Perfect, thank you. now when the user click set, i'm in the method of the listener but how can i get his date? (the date that he has selected?) – Giovanni Far Nov 10 '14 at 00:11
  • @GiovanniFar No offense, but its how SO work, 1 answer for 1 question..you even already asked 2 questions. Better for you to create another question for future problems – Blaze Tama Nov 10 '14 at 00:14
  • usually happen that i get down vote because they say: u can merge all in 1 question, now isnt good because i have split the questions... :roll: in everycase thank u so much – Giovanni Far Nov 10 '14 at 00:17