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".
Complete Code of DatePicker Dialog Example in Android
app>src>main>java>com.example.DatePicker>MainActivity.java
package com.example.DatePicker;
import android.support.v7.app.ActionBarActivity;
import android.os.bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button btn;
int year_x,month_x,day_x;
static final int DIALOG_ID = 0;
@Override
protected onCreate(bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Calender cal = calender.getInstance();
year_x = cal.get(Calender.YEAR);
month_x = cal.get(Calender.MONTH);
day_x = cal.get(Calender.DAY_OF_MONTH);
showDialogonButtonClick();
}
public void showDialogonButtonClick() {
btn = (Button)findViewById(R.Id.button);
btn.setonClickListener( new View.onClickListener (){
@Override
public void onClick(View v){
showDialog(DIALOG_ID);
}
} );
}
protected Dialog onCreateDialog(int id){
if(id == DIALOG_ID)
return new DatePickerDialog(this, dpickerListner, year_x,month_x,day_x);
return null;
}
private DatePickerDialog.OnDateSetListener dpickerListner = new DatePickerDialog.onDateSetListner(){
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth){
year_x = year;
month_x = monthOfYear + 1;
day_x = dayOfMonth;
Toast.makeText(MainActivity.this,year_x + "/" + month_x + "/" + day_x,Toast.LENGTH_LONG).show();
}
};
}