Date Picker Dialog in Android Studio (Java)
Let users pick a date from a calendar instead of typing it. The chosen date fills a text box in the format you want.
What you'll learn
- Open a DatePickerDialog
- Start on today's date
- Show the picked date in an EditText
- Format dates correctly
Before you start
- Android Studio (any recent version)
- Basic Java: variables, methods and if/else
- An emulator or an Android phone
How it fits together
- 1Tap the date box
- 2DatePickerDialog
- 3onDateSet
- 4Formatted date in the box
Build it step by step
1. Create the project
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.
Application code
Select a file to see its code and where it belongs.
This code uses package com.example.datepicker. Change that first line to your own package, the one at the top of your MainActivity.
app/src/main/java/com/example/datepicker/MainActivity.javaOpens the DatePickerDialog and writes the chosen date into the box.
package com.example.datepicker;
import android.app.DatePickerDialog;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.material.textfield.TextInputEditText;
import java.text.DateFormat;
import java.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.
*/
public class MainActivity extends AppCompatActivity {
private final Calendar selected = Calendar.getInstance();
TextInputEditText date;
@Override
protected void onCreate(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();
}
}
private void openPicker() {
DatePickerDialog dialog = new DatePickerDialog(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();
}
private void showDate() {
// 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()));
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if (date.getText() != null && date.getText().length() > 0) outState.putLong("date", selected.getTimeInMillis());
}
}


