How to use DatePicker Dialog in Android
Please follow the steps below in order to use the datepicker dilog in Android:
Step 1) First of all, take a button and change the text of this button as "show date picker dialog" so that when we click this button, the datepicker dialog will appear and we will be able to select some date using this date time picker and we can perform some action on the basis of this day selected.
Step 2) Now go to your java folder mainactivity.java and declare first of all object of button and three variables – one for year, one for month and one for the day of the month, so int year_x, month_x and day_x. And also one variable DIALOG_ID to hold the dialog id of our dialog.
Step 3) Now declare a method 'showDialogOnButtonClick()' which we will call inside our oncreate method and cast the variables inside the showDialogOnButtonClick() method.
Step 4) Now take the button instance or object and call set onClickListner, and inside this set onClickListner, we will call a new onClickListner and inside this onClick method what we are going to call our showdialog which is an inbuilt method in Android. This inbuilt method showdialog will takes the id of the dialog.
Step 5) Now we will create call onCreate dialog method which is an inbuilt method and inside this onCreateDialog, we will write code.
Step 6) Now we are going to declare this variable which is our datepickerListener variable and it’s going to automatically create this method for us when we press enter key and just don’t remember to close this variable here by semi-colon. Inside this created method we will see year in the ondatesetmethod arguments are year, month of the year and date of the month or day of the month. So we can assign this to our declared three variables above i.e year_x, month_x and day_x.
Step 7) Now will display this year, month and day using a toast or something other like textbox or edit text or something like this, so take this toast.maketext and it takes first argument as the class context or context of this class itself, so mainactivity.this and the second argument is the text itself and third argument, Toast.LENGT_LONG.
Step 8) Now call this 'showDialogOnButtonClick()' method inside your onCreate method.
Step 9) So now the program is running and we will see "show date picker dialog" button and when we click this button, we will be able to see the date but the date here, whenever you select the date, it will not be the current date because we haven’t set any date to our datepicker dialog.
Step 10) So what we can do is we can set the date to our datepicker dialog which is the current date by using calendar class.
Step 11) In addition, you need to do one more little thing here, whenever you print you day using your toast, you can, get the month by default, months start from 0 here so what you can do is you can add 1 to your month here and it will give you the correct month.
Now our program is once again running, click button and you will see the current date and the year and the month and when we click OK, we will be able to see this month, date and year below the button "show date picker dialog".