Android Dialog DatePicker

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!


DatePicker allows user to pick date. When we create new instance of DatePicker, we can set initial date. If we don't set initial date, current date will be set by default.

We can show DatePicker to user by using DatePickerDialog or by creating our own layout with DatePicker widget.

Also we can limit range of date, which user can pick.

By setting minimum date in milliseconds

//In this case user can pick date only from future

By setting maximum date in milliseconds

//In this case user can pick date only, before following week.
datePicker.setMaxDate(System.currentTimeMillis() + TimeUnit.DAYS.toMillis(7));

To receive information, about which date was picked by user, we have to use Listener.

If we are using DatePickerDialog, we can set OnDateSetListener in constructor when we are creating new instance of DatePickerDialog:

Sample use of DatePickerDialog

public class SampleActivity extends AppCompatActivity implements DatePickerDialog.OnDateSetListener {

    public void onCreate(Bundle savedInstanceState) {

    private void showDatePicker() {
        //We need calendar to set current date as initial date in DatePickerDialog.
        Calendar calendar = new GregorianCalendar(Locale.getDefault());
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);

        DatePickerDialog datePickerDialog = new DatePickerDialog(this, this, year, month, day);;

    public void onDateSet(DatePicker datePicker, int year, int month, int day) {


Otherwise, if we are creating our own layout with DatePicker widget, we also have to create our own listener as it was shown in other example

Got any Android Question?