Free Shipping

Secure Payment

easy returns

24/7 support

  • Home
  • Blog
  • Introduction to Android Date Picker Dialog with Example

Introduction to Android Date Picker Dialog with Example

 July 13  | 0 Comments

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: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.

activity_main.xml
Add ImageButton in your xml file.

<ImageButton
         android:id="@+id/imageButton1"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignBottom="@+id/editText"
         android:layout_toRightOf="@+id/editText"
         android:contentDescription="@string/selectdate"
         android:cropToPadding="true"
         android:src="@drawable/calendar_icon" />

MainActivity.java
Add below code in onCreate(){..}

ib = (ImageButton) findViewById(R.id.editText);
cal = Calender.getInstance();
day = cal.get(Calender.DAY_OF_MONTH);
month=cal.get(Calender.MONTH);
year = cal.get(Calender.YEAR);
et= (EditText) findViewById(R.id.editText);
ib.setOnClickListener(this);

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

showDialog(0);
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 :
https://github.com/hiteshbpatel/Android_Projects/tree/master/AndroidDatePicker/
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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Set Date"
android:layout_below="@id/tv" />

MainActivity.java
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();
dFragment.show(getFragmentManager(), "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{
@Override
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(),
 AlertDialog.THEME_HOLO_LIGHT,this,year,month,day);
calendar.add(Calendar.DATE, 3);
dpd.getDatePicker().setMaxDate(calendar.getTimeInMillis());
calendar.add(Calendar.DATE, -6);
dpd.getDatePicker().setMinDate(calendar.getTimeInMillis());
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(R.id.tv);
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 :Download Project Link : 
https://github.com/hiteshbpatel/Android_Projects/tree/master/DatePickerDemo
Keep visiting our site Acadgild for more updates on Android and other technologies.

 

>