All CategoriesAndroid App Development

Introduction to Android Date Picker Dialog with Example


Android Date Picker helps you to select the date with day, month and year in your Layout/User Interface. Android provides you two classes DatePicker and DatePickerDialog to implement the functionality. You can set the date like below:

Let’s look into the example on how to implement it.

In this example, you will see how to set a date. When you click on ImageButton it should open a dialog box with today’s date and while clicking on “Set” it will set the value in EditText.
Android Programming
Add ImageButton in your xml file.

         android:src="@drawable/calendar_icon" />
Add below code in onCreate(){..}

ib = (ImageButton) findViewById(;
cal = Calender.getInstance();
day = cal.get(Calender.DAY_OF_MONTH);
year = cal.get(Calender.YEAR);
et= (EditText) findViewById(;

Create onClick(){..} and add one line code :

private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay) {
et.setText(selectedDay + “/” + (selectedMonth + 1) + “/” + selectedYear);

Output : 

Download Project Link :
How to set Minimum and Maximum Date in DatePicker :
In Contribution with above example we will see how to set minimum and maximum Date in Date Picker.
Add a Button in activity_main.xml.

<Button android:id="@+id/btn"
android:text="Set Date"
android:layout_below="@id/tv" />
Write a logic for button’s setOnClickListener(){..} . When event triggers then a Dialog with DatePicker will be shown to the user.

btn.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v)
DialogFragment dFragment = new DatePickerFragment();, "Date Picker");

Create a Fragment class – DatePickerFragment, extend it with DialogFragment & implement it with DatePickerDialog.OnDateSetListener class :

public static class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener{
public Dialog onCreateDialog(Bundle savedInstanceState){
 final Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dpd = new DatePickerDialog(getActivity(),
calendar.add(Calendar.DATE, 3);
calendar.add(Calendar.DATE, -6);
return dpd;

Now create a new method onDateSet(){..} and add some code to it.

public void onDateSet(DatePicker view, int year, int month, int day)
TextView tv = (TextView) getActivity().findViewById(;
Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(0);
cal.set(year, month, day, 0, 0, 0);
Date chosenDate = cal.getTime();
DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.US);
String formattedDate = df.format(chosenDate);  tv.setText(formattedDate);

Output :
Set Max Date Android
Download Project Link :
Keep visiting our site Acadgild for more updates on Android and other technologies.
Android Programming

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Articles