• Have Any Question?
  • contact@eitworld.com

Date and Time Picker Dialog





  • There are two views in Android named ‘DatePicker’ and ‘TimePicker’. These views contain spinners to select date and time respectively. See DatePicker, TimePicker
  • There is also a facility in android to open a dialog box for date and time picking. In between an activity if you want to let user choose date and time from a dialog box that looks similar to ‘DatePicker’ and ‘TimePicker’; following classes are used-
    • DatePickerDialog
    • TimePickerDialog
  • They both are classes from package ‘android.app’. We will describe them one by one.

Creating Date Picker Dialog

Step 1- Create an object of class ‘DatePickerDialog’ which will create the Dialog Box.

DatePickerDialog dpd=new DatePickerDialog(MainActivity.this, MyDate, year, month, date);

The constructor of DatePickerDialog contains five parameters-

  • Context- It is the context for which the Dialog Box has to be added. Use ‘YourActivity.this’ here.
  • CallBack- It is the callback for the DatePickerDialog which will be called when the user has done selecting the date. It has been described in step 2.
  • year- It is the year which you want to be displayed in the dialog box when the dialog box is opened. Use any four digit integer value.
  • month- It is the month which you want to be displayed in the dialog box when the dialog box is opened. Use any integer between 0 and 11. Note that January will be represented by 0, not 1.
  • day- It is the day which you want to be displayed in the dialog box when the dialog box is opened. Use any integer between 1 and 31.

Step 2- Create an object of ‘OnDateSetListener’. It must be created before using it in the constructor of ‘DatePickerDialog’. This is a callback that indicates that the user has entered the Date from DatePickerDialog. It has a method ‘onDateSet()’ which will contain the task that will occur after setting of the date by user.

OnDateSetListener MyDate=new OnDateSetListener() {

@Override

public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) {

// TODO Auto-generated method stub

}

};

onDateSet() methods contain four parameters-

  • DatePicker- First one represents the DatePicker itself.
  • Year- Second parameter represents the year set by user.
  • Month- Third parameter represents the month set by user. Note that the system assumes that the month numbering starts from 0. Therefore, before using second argument, increment it by 1.
  • Day- Fourth parameter represents the day set by user.

Step 3- Open the DatePickerDialog-

dpd.show();

 

Creating Time Picker Dialog

Step 1- Create an object of class ‘TimePickerDialog’ which will create the Dialog Box.

TimePickerDialog tpd=new TimePickerDialog(MainActivity.this,MyTime,hour,minute,24hourview);

The constructor of DatePickerDialog contains five parameters-

  • Context- It is the context for which the Dialog Box has to be added. Use ‘YourActivity.this’ here.
  • CallBack- It is the callback for the TimePickerDialog which will be called when the user has done selecting the time. It has been described in step 2.
  • hour- It is the hour which you want to be displayed in the dialog box when the dialog box is opened.
  • minute- It is the minutes that you want to be displayed in the dialog box when the dialog box is opened.
  • 24hourview- It contains a Boolean value. If it is set to true, the range of hour will be 0 to 23. And if it is set to false, the range of hour will be from 0 to 11 and an extra spinner for selecting AM/PM will be displayed in the Dialog Box.

Step 2- Create an object of ‘OnTimeSetListener’. It must be created before using it in the constructor of ‘TimePickerDialog’. This is a callback that indicates that the user has entered the Time from TimePickerDialog. It has a method ‘onTimeSet()’ which will contain the task that will occur after setting of the time by user.

OnTimeSetListener MyTime=new OnTimeSetListener() {

@Override

public void onTimeSet(TimePicker arg0, int arg1, int arg2) {

// TODO Auto-generated method stub

}

};

onTimeSet() methods contain four parameters-

  • TimePicker- First one represents the TimePicker itself.
  • Hour- Second parameter represents the hours set by user.
  • Minute- Third parameter represents the minutes set by user

Step 3- Open the TimePickerDialog-

tpd.show();



Real Time Web Analytics