Android Date Time Picker Dialog With Examples

Android Date Time picker are used a lot in android apps. In this tutorial we’ll demonstrate the use of a Date Picker and Timer Picker Dialog in our android application. These components are used to select date and time in a customised user interface. We will use DatePickerDialog and TimePickerDialog classes with Calendar class in our android application code to achieve this.

Android DatePickerDialog and TimePickerDialog

Though a Date Picker and Time Picker can be used as independent widgets but they occupy more space on the screen. Hence using them inside a Dialog is a better choice. Fortunately android provides use with its own DatePickerDialog and TimePickerDialog classes.

DatePickerDialog and TimePickerDialog classes have onDateSetListener() and onTimeSetListener() callback methods respectively. These callback methods are invoked when the user is done with filling the date and time respectively.

The DatePickerDialog class consists of a 5 argument constructor with the parameters listed below.

  1. Context: It requires the application context
  2. CallBack Function: onDateSet() is invoked when the user sets the date with the following parameters:
  • int year : It will be store the current selected year from the dialog
  • int monthOfYear : It will be store the current selected month from the dialog
  • int dayOfMonth : It will be store the current selected day from the dialog
  • int mYear : It shows the the current year that’s visible when the dialog pops up
  • int mMonth : It shows the the current month that’s visible when the dialog pops up
  • int mDay : It shows the the current day that’s visible when the dialog pops up

The TimePickerDialog class consists of a 5 argument constructor with the parameters listed below.

  1. Context: It requires the application context
  2. CallBack Function: onTimeSet() is invoked when the user sets the time with the following parameters:
  • int hourOfDay : It will be store the current selected hour of the day from the dialog
  • int minute : It will be store the current selected minute from the dialog
  • int mHours : It shows the the current Hour that’s visible when the dialog pops up
  • int mMinute : It shows the the current minute that’s visible when the dialog pops up
  • boolean false : If its set to false it will show the time in 24 hour format else not

Android Date Time Picker Example Project Structure

android-date-time-picker-dialog-project-323x450

Android Date Time Picker Dialog Project Code

The activity_main.xml consists of two buttons to invoke the Date and Time Picker Dialogs and set the user selected time in the two EditText views. The xml code is given below.

activity_main.xml

The MainActivity.java class is given below:

In the above code we’ve created a Calendar object using Calendar.getInstance() to display the current date and time using the respective static fields.

Note: The display calendar and clock are the default UI themes as provided in the AppCompat Theme.

Below is the output produced by our android date time picker example application.

android date time picker example

This brings an end to android date time picker dialog example tutorial. You can download the final Android DateTimePickerDialog Project from the below link.

By admin

Leave a Reply

%d bloggers like this: