In Android Studio choose New Project → Empty Views Activity, with Language Java and Minimum SDK API 24.
2. Add a date box
Use a Material TextInputLayout with a calendar icon. Set the EditText to inputType="none" and focusableInTouchMode="false", so tapping it opens the calendar instead of the keyboard.
3. Open the calendar
When the box is tapped, create a DatePickerDialog starting on the current date from a Calendar, and call show().
4. Use the picked date
The dialog calls you back with the year, month and day. Save them in the Calendar, then show the date with DateFormat.getDateInstance(), which writes it the way the phone's language expects.
5. Limit the dates
getDatePicker().setMinDate(...) stops users choosing past dates, which is handy for bookings.
6. Keep it on rotation
Save the chosen date in onSaveInstanceState and restore it in onCreate.
7. Run it
Tap the box, pick a date, and rotate the phone.
Tip
Months in Calendar start at 0 (January = 0). Let DateFormat do the formatting and you'll never get this wrong.
What's improved over the video
The box can't be typed into, past dates are blocked, the date follows the phone's language, and it survives rotation.
Files affected
Check what to add and what to replace before copying code.
Opens the DatePickerDialog and writes the chosen date into the box.
MainActivity.java
packagecom.example.datepicker;importandroid.app.DatePickerDialog;importandroid.os.Bundle;importandroidx.appcompat.app.AppCompatActivity;importcom.google.android.material.textfield.TextInputEditText;importjava.text.DateFormat;importjava.util.Calendar;/**
* Tapping the date box opens a calendar. The picked date fills the box, formatted for the phone's language.
* The box itself can't be typed into, so the date is always valid.
*/publicclassMainActivityextendsAppCompatActivity{privatefinalCalendar selected =Calendar.getInstance();TextInputEditText date;@OverrideprotectedvoidonCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);Screens.setUp(this,R.layout.activity_main);
date =findViewById(R.id.date);
date.setOnClickListener(v ->openPicker());if(savedInstanceState !=null&& savedInstanceState.containsKey("date")){
selected.setTimeInMillis(savedInstanceState.getLong("date"));showDate();}}privatevoidopenPicker(){DatePickerDialog dialog =newDatePickerDialog(this,(view, year, month, day)->{
selected.set(year, month, day);showDate();}, selected.get(Calendar.YEAR), selected.get(Calendar.MONTH), selected.get(Calendar.DAY_OF_MONTH));// Only allow today or later, for example for a booking.
dialog.getDatePicker().setMinDate(System.currentTimeMillis()-1000);
dialog.show();}privatevoidshowDate(){// DateFormat writes the date the way this phone's language expects, e.g. 27 Oct 2026 or Oct 27, 2026.
date.setText(DateFormat.getDateInstance(DateFormat.MEDIUM).format(selected.getTime()));}@OverrideprotectedvoidonSaveInstanceState(Bundle outState){super.onSaveInstanceState(outState);if(date.getText()!=null&& date.getText().length()>0) outState.putLong("date", selected.getTimeInMillis());}}