Android Date Time picker in one dialog

anuradha picture anuradha · Jul 27, 2016 · Viewed 38.9k times · Source

I am using material Date Time picker for my Android app. But I want to combine the Date and Time picker in one dialog.

I am following this one. But I need to select the time right after the date selection.

Please suggest possible way to do this one.

Answer

user4696837 picture user4696837 · Jul 27, 2016

You can call TimePicker after DatePicker set Date. Like this..

    String date_time = "";
    int mYear;
    int mMonth;
    int mDay;

    int mHour;
    int mMinute;

    et_show_date_time = (EditText) findViewById(R.id.et_show_date_time);
    btn_set_date_time = (Button) findViewById(R.id.btn_set_date_time);

    btn_set_date_time.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            datePicker();

        }
    });

Date Picker:

    private void datePicker(){

    // Get Current Date
    final Calendar c = Calendar.getInstance();
    mYear = c.get(Calendar.YEAR);
    mMonth = c.get(Calendar.MONTH);
    mDay = c.get(Calendar.DAY_OF_MONTH);

    DatePickerDialog datePickerDialog = new DatePickerDialog(this,
            new DatePickerDialog.OnDateSetListener() {

                @Override
                public void onDateSet(DatePicker view, int year,int monthOfYear, int dayOfMonth) {

                    date_time = dayOfMonth + "-" + (monthOfYear + 1) + "-" + year;
                   //*************Call Time Picker Here ********************
                   tiemPicker();
                }
            }, mYear, mMonth, mDay);
       datePickerDialog.show();
    }

Time Picker

 private void tiemPicker(){
    // Get Current Time
    final Calendar c = Calendar.getInstance();
    mHour = c.get(Calendar.HOUR_OF_DAY);
    mMinute = c.get(Calendar.MINUTE);

    // Launch Time Picker Dialog
    TimePickerDialog timePickerDialog = new TimePickerDialog(this,
            new TimePickerDialog.OnTimeSetListener() {

                @Override
                public void onTimeSet(TimePicker view, int hourOfDay,int minute) {

                    mHour = hourOfDay;
                    mMinute = minute;

                    et_show_date_time.setText(date_time+" "+hourOfDay + ":" + minute);
                }
            }, mHour, mMinute, false);
    timePickerDialog.show();
}

--