Android project – Date and Time Picker
1. Create a New Android Project
• Click on Start a new Android Studio project.
• Choose Empty view Activity.
• Name your project (e.g., datetimepickerdemo).
2. Update the Layout (XML)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="16dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Button to select a date -->
<Button
android:id="@+id/btn_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Select Date" />
<!-- Button to select a time -->
<Button
android:id="@+id/btn_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Select Time"
android:layout_marginTop="16dp" />
<!-- TextView to display the selected date and time -->
<TextView
android:id="@+id/tv_date_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Selected Date and Time will appear here"
android:textSize="18sp"
android:layout_marginTop="16dp" />
</LinearLayout>
3. Update the MainActivity
package com.example.datetimedpickerdemo;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.widget.DatePicker;
import android.widget.TimePicker;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Calendar;
//The import android.app.DatePickerDialog is needed because it's the class for the date picker
dialog that lets users select a date.
//The import android.widget.DatePicker is required as it defines the widget used inside the
dialog. Both are part of the Android SDK, working together for this UI feature.
public class MainActivity extends AppCompatActivity {
private Button btnDate, btnTime;
private TextView tvDateTime;
private String selectedDate = "";
private String selectedTime = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Bind UI components
btnDate = findViewById(R.id.btn_date);
btnTime = findViewById(R.id.btn_time);
tvDateTime = findViewById(R.id.tv_date_time);
// Set click listeners for buttons
btnDate.setOnClickListener(view -> showDatePickerDialog());
btnTime.setOnClickListener(view -> showTimePickerDialog());
}
// Method to show the DatePickerDialog
private void showDatePickerDialog() {
// Get current date as default values
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH); // Note: month is 0-based.
int day = calendar.get(Calendar.DAY_OF_MONTH);
// Create and show the DatePickerDialog
DatePickerDialog datePickerDialog = new DatePickerDialog(this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int
selectedDay) {
// Increment month by 1 for display since it's zero-based.
selectedDate = selectedDay + "/" + (selectedMonth + 1) + "/" + selectedYear;
updateDateTimeTextView();
}
}, year, month, day);
datePickerDialog.show();
}
// Method to show the TimePickerDialog
private void showTimePickerDialog() {
// Get current time as default values
Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
// Create and show the TimePickerDialog
TimePickerDialog timePickerDialog = new TimePickerDialog(this,
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int selectedHour, int selectedMinute) {
selectedTime = String.format("%02d:%02d", selectedHour, selectedMinute);
updateDateTimeTextView();
}
}, hour, minute, true); // Time Format: A boolean indicating whether to use a 24-
hour view (true) or a 12-hour view (false).
timePickerDialog.show();
}
// Update the TextView with the selected date and time
private void updateDateTimeTextView() {
tvDateTime.setText("Selected Date: " + selectedDate + "\nSelected Time: " +
selectedTime);
}
}
Output:
Working
• Retrieving the Current Time:
We use Calendar.getInstance() to get the current hour and minute, which are used as
the default values in the TimePickerDialog.
• TimePickerDialog Creation:
The dialog is created with an OnTimeSetListener that gets called when the user sets
the time. Inside onTimeSet(), the selected hour and minute are formatted and then
displayed in the TextView.
• Displaying the Dialog:
Finally, timePickerDialog.show() displays the dialog to the user.
Format String:
• %02d:
o %: Indicates that a format specifier follows.
o 0: Specifies that the number should be padded with zeros if it's less than two
digits.
o 2: Indicates the minimum width for the number (at least 2 digits).
o d: Means that the argument is a decimal integer.
Note:
In case of updation request, do the following:
Updating build.gradle.kts (Module:app):
update your compileSdkVersion to 35 in the build.gradle file of your module. Find the
compileSdkVersion line and change it to 35.
android {
compileSdkVersion 35
defaultConfig {
applicationId "com.example.yourapp"
minSdkVersion 21
targetSdkVersion 35
versionCode 1
versionName "1.0"
// ... other configurations