Customise DatePicker and TimePicker Dialog in Android

Sunday, March 18, 2012

To provide a widget for selecting a date, use the DatePicker widget, which allows the user to select the month, day, and year, in a familiar interface.

In this tutorial, you'll create a DatePickerDialog, which presents the date picker in a floating dialog box at the click on the EditText box. When the date is set by the user, a EditText will update with the new date.

Also you can set TimePickerDialog similarly DatePickerDialog.

public class TimeDialog extends Activity {
    private int mYear;
    private int mMonth;
    private int mDay;
    private EditText mPickTime,mPickDate;
    private int mhour,mminute;
    static final int TIME_DIALOG_ID = 1;
    static final int DATE_DIALOG_ID = 0;
    public void onCreate(Bundle savedInstanceState) {
        mPickDate = (EditText) findViewById(;
        mPickTime=(EditText) findViewById(;
        mPickTime.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v) {
                // TODO Auto-generated method stub

        //PickDate's click event listener
        mPickDate.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {


        final Calendar c = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH);
        mDay = c.get(Calendar.DAY_OF_MONTH);
        mhour = c.get(Calendar.HOUR_OF_DAY);
        mminute = c.get(Calendar.MINUTE);

private void updateDate() {

        new StringBuilder()
                // Month is 0 based so add 1
                .append(mMonth + 1).append("/")
                .append(mYear).append(" "));


  //-------------------------------------------update time----------------------------------------//   
public void updatetime()
            new StringBuilder()
private static String pad(int c)
    if (c >= 10)
           return String.valueOf(c);
           return "0" + String.valueOf(c);
//Datepicker dialog generation 

private DatePickerDialog.OnDateSetListener mDateSetListener =
    new DatePickerDialog.OnDateSetListener() {
        public void onDateSet(DatePicker view, int year,
                              int monthOfYear, int dayOfMonth) {
            mYear = year;
            mMonth = monthOfYear;
            mDay = dayOfMonth;
 // Timepicker dialog generation
private TimePickerDialog.OnTimeSetListener mTimeSetListener =
        new TimePickerDialog.OnTimeSetListener() {
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                mhour = hourOfDay;
                mminute = minute;
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DATE_DIALOG_ID:
            return new DatePickerDialog(this,
                    mDateSetListener, mYear, mMonth,mDay);
        case TIME_DIALOG_ID:
            return new TimePickerDialog(this,
                    mTimeSetListener, mhour, mminute, false);
        return null;

The output of this program is as follows:


Post a Comment

Note: Only a member of this blog may post a comment.