AlertDialog with DatePicker

My idea is to use AlertDialog and set the DatePicker for it.
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 listeners on the set and cancel button ?
About the set button, I would like the listener and (of course) get the user's date (his date choice)


I also want to show only the year , so I want to hide the data of the day and month.

+5
source share
1 answer

You can use setPositiveButton , for example:

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

Same thing for setNegativeButton .

I also want to show only the year, so I want to hide the day and month Data

Look, use the 1st answer:

Hide Year field in Android DatePicker?

+5
source

Source: https://habr.com/ru/post/1206526/


All Articles