Datepicker How to popup datepicker when click on edittext
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Showing a date picker when the user taps an EditText is a common Android pattern, but the clean implementation is not really about the dialog. It is about treating the field as a date selector instead of a free-text input, so the keyboard does not fight the picker and the displayed value stays formatted consistently.
Basic DatePickerDialog Approach
The classic solution is to open a DatePickerDialog from the EditText click listener and then write the chosen date back into the field.
This is enough for many apps, and it keeps the user from typing invalid dates manually.
Make the EditText Behave Like a Picker Field
The biggest UI mistake is leaving the field fully editable. If tapping the field opens the dialog and the keyboard also appears, the screen feels broken.
Two common fixes are:
- disable soft input with
showSoftInputOnFocus = false - hide the cursor and rely on the picker for data entry
You can also make the field non-focusable and open the picker on click, but then you need to ensure accessibility and navigation still feel right. The general goal is clear: this field displays a date, but the date should come from the picker.
XML Layout Example
The layout is ordinary. The important part is assigning an ID and making the field visually communicate that it is a selector:
Some teams prefer TextInputEditText inside a Material TextInputLayout, but the click-handling idea stays the same.
Restricting the Allowed Date Range
If users should not pick dates in the past or beyond a business limit, configure the dialog before showing it:
You can also set maxDate in the same way. This is usually better than allowing every date and rejecting invalid ones afterward.
Consider MaterialDatePicker in Modern Apps
If your app already uses Material Components, MaterialDatePicker often provides a better modern UI than the older platform dialog. The principle is still the same: tap the field, show a picker, format the chosen value, and write it back into the visible text field.
The title here is about EditText, but the real design lesson is broader: date values are usually better chosen than typed.
Common Pitfalls
- Opening the picker but still showing the soft keyboard.
- Letting users type arbitrary date text and then trying to reconcile it with the picker.
- Forgetting that
Calendar.MONTHis zero-based in the Android APIs. - Hard-coding a date format that does not fit the app's locale or backend contract.
- Validating forbidden dates only after selection instead of restricting the picker itself.
Summary
- Open a
DatePickerDialogfrom theEditTextclick event and write the result back into the field. - Treat the field as a selector, not as free-text input.
- Disable the keyboard or cursor behavior so the UI feels intentional.
- Use
minDateandmaxDatewhen business rules restrict valid dates. - In modern Material apps, consider
MaterialDatePickerfor a newer picker UI.

