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();
}
@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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With