TimePicker Dialog is used in many applications where we have to book appointments based on a specific time. This widget is seen in the applications where we have to select specific time slots. In this article, we will take a look at How to use TimePicker Dialog on Android. TimePicker provides two display modes:
- Clock Mode: Displays an analog clock-style interface for time selection.
- Spinner Mode: Presents hour and minute values in a scrollable spinner format.
Note: This Android article covered in both Java and Kotlin languages.
Steps of Implementing TimePicker
Step 1: Create a new project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.
Step 2: Adding TimePicker widget in Layout file
We can use android:timePickerMode to choose which the mode for the TimePicker. The possible values are “clock” and “spinner”. This article demonstrate how to implement both type of modes.
activity_main.xml:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
tools:context=".MainActivity">
<TimePicker
android:id="@+id/timePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp"
android:timePickerMode="spinner"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:textSize="18sp"
android:text="Time is: "
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/timePicker" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
tools:context=".MainActivity">
<TimePicker
android:id="@+id/timePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp"
android:timePickerMode="clock"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:textSize="18sp"
android:text="Time is: "
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/timePicker" />
</androidx.constraintlayout.widget.ConstraintLayout>
Design UI:

Step 3: Access the TimePicker in MainActivity.kt file
First of all, we declare two variables textView and timePicker to access the widgets from the XML layout using their id’s.
val textView = findViewById(R.id.textView)
val timePicker = findViewById(R.id.timePicker)
Then, we set the setOnTimeChangedListener(), to detect when the time is changed
timePicker.setOnTimeChangedListener { view, hourOfDay, minute ->
// your code here
}
MainActivity file:
package org.geeksforgeeks.demo;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.TimePicker;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize TextView and TimePicker from layout
TextView textView = findViewById(R.id.textView);
TimePicker timePicker = findViewById(R.id.timePicker);
// Set a listener for when the time changes
timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
int hour = hourOfDay;
String amPm;
// Determine AM or PM and adjust hour
if (hour == 0) {
hour += 12;
amPm = "AM";
} else if (hour == 12) {
amPm = "PM";
} else if (hour > 12) {
hour -= 12;
amPm = "PM";
} else {
amPm = "AM";
}
// Format hour and minute for display
String formattedHour = (hour < 10) ? "0" + hour : String.valueOf(hour);
String formattedMinute = (minute < 10) ? "0" + minute : String.valueOf(minute);
// Display the selected time
String msg = "Time is: " + formattedHour + " : " + formattedMinute + " " + amPm;
textView.setText(msg);
textView.setVisibility(ViewGroup.VISIBLE);
}
});
}
}
package org.geeksforgeeks.demo
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.ViewGroup
import android.widget.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Initialize TextView and TimePicker from layout
val textView: TextView = findViewById(R.id.textView)
val timePicker: TimePicker = findViewById(R.id.timePicker)
// Set a listener for when the time changes
timePicker.setOnTimeChangedListener { _, hour, minute ->
var hour1 = hour
var amPm = ""
// Determine AM or PM and adjust hour
when {hour1 == 0 -> { hour1 += 12
amPm = "AM"
}
hour1 == 12 -> amPm = "PM"
hour1 > 12 -> { hour1 -= 12
amPm = "PM"
}
else -> amPm = "AM"
}
// Format hour and minute for display
val hour2 = if (hour1 < 10) "0$hour1" else hour1
val min = if (minute < 10) "0$minute" else minute
// Display the selected time
val msg = "Time is: $hour2 : $min $amPm"
textView.text = msg
textView.visibility = ViewGroup.VISIBLE
}
}
}