Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

After Selecting date the date picker should close in android without clicking okay button

I am using a default date picker in android, now I want close the dialog box after Selecting date the date picker should close in android without clicking okay button

    private void updateDOB() {
    String myFormat = "yyyy/MM/dd"; //In which you need put here
    SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
          btnDOB.setText(sdf.format(calendar.getTime()));
             tvDOB.setTextColor(ContextCompat.getColor(getApplicationContext(),    R.color.colorPrimary));
}

private void selectDOB() {
    DatePickerDialog.OnDateSetListener listener = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker datePicker, int year, int month, int day) {
            calendar.set(Calendar.YEAR, year);
            calendar.set(Calendar.MONTH, month);
            calendar.set(Calendar.DAY_OF_MONTH, day);
            updateDOB();
        }
    };
    new DatePickerDialog(ActivityAddChild.this, listener,
            calendar.get(Calendar.YEAR),
            calendar.get(Calendar.MONTH),
            calendar.get(Calendar.DAY_OF_MONTH)).show();
} 
like image 739
ajay110125 Avatar asked Jul 26 '16 09:07

ajay110125


1 Answers

@Override the onDateChanged and dismiss(); it.

new DatePickerDialog(context, null, year, month, day) {
    @Override
    public void onDateChanged(@NonNull DatePicker view, int year, int month, int dayOfMonth) {
        /* Here you got the date without "OnDateSetListener" */

        dismiss();
    }
}.show();

Referenced from this answer.

Note that this doesn't work on Android 5.0, see there.

Use MaterialDateTimePicker instead.

like image 144
Jie Wang Avatar answered Sep 30 '22 23:09

Jie Wang