Zum Hauptinhalt springen

TimePickerDialog Example

An Introduction to TimePickerDialog in Android

Are you developing an Android application that requires users to pick a time? If yes, then TimePickerDialog is your friend! It is a built-in Android widget that allows users to select a time from a dialog box. In this article, we'll discuss what TimePickerDialog is, how to use it in your Android application, and provide code examples.

What is TimePickerDialog in Android?

TimePickerDialog is an Android widget that displays a dialog box with a time picker. Users can use the time picker to select a specific time. The selected time can be used to perform various actions, such as scheduling reminders or setting alarms.

How to Use TimePickerDialog in Android

Using TimePickerDialog in your Android application is straightforward. Here are the steps involved:

  1. Create a new instance of TimePickerDialog.
  2. Set the TimePickerDialog.OnTimeSetListener to handle the user's time selection.
  3. Show the TimePickerDialog.

Creating a TimePickerDialog

To create a TimePickerDialog, you need to provide a few parameters, including the context, the OnTimeSetListener, the initial time, and whether the 24-hour format should be used. Here's an example of creating a TimePickerDialog:

// Get the current time
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);

// Create a new instance of TimePickerDialog
TimePickerDialog timePickerDialog = new TimePickerDialog(MainActivity.this,
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// Handle the user's time selection
}
}, hour, minute, false);

// Show the TimePickerDialog
timePickerDialog.show();

In the example above, we created a TimePickerDialog and set the OnTimeSetListener to handle the user's time selection. We also set the initial time to the current time and specified that the 24-hour format should not be used.

Handling the User's Time Selection

To handle the user's time selection, we need to implement the OnTimeSetListener interface. The onTimeSet() method of this interface is called when the user selects a time. Here's an example of how to handle the user's time selection:

new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// Do something with the selected time
String selectedTime = hourOfDay + ":" + minute;
Log.d("TAG", "Selected time: " + selectedTime);
}
}

In the example above, we simply log the selected time to the console. However, you can use the selected time to perform various actions in your application.

Here are more examples:

Example 1: Kotlin Android TimePickerDialog Example

Learn how to pick time using TimePickerDialog in android. We look at a simple example where a user presses a button, a TimePickerDialog is then shown, and the picked Time is rendered on a TextView.

Step 1: Create Project

Start by creating an empty Android Studio project.

Step 2: Dependencies

No third party dependency is needed for this project.

Step 3: Design Layout

Design your MainActivity layout as follows:

activity_main.xml

Add a TextView and Button.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">

<TextView
android:id="@+id/timeTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Time"
android:textSize="20sp"
android:textColor="@android:color/black"/>

<Button
android:id="@+id/pickTimeBtn"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pick Time"
android:background="@color/colorPrimary"
android:textColor="@android:color/white"/>

</LinearLayout>

Step : Write Code

Here is the code for rendering a TimePickerDialog when a button is clicked:

        pickTimeBtn.setOnClickListener {
val cal = Calendar.getInstance()
val timeSetListener = TimePickerDialog.OnTimeSetListener { view, hourOfDay, minute ->
cal.set(Calendar.HOUR_OF_DAY, hourOfDay)
cal.set(Calendar.MINUTE, minute)

timeTv.text = SimpleDateFormat("HH:mm").format(cal.time)
}
TimePickerDialog(this, timeSetListener, cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), false).show()
}

Here's the full code:

MainActivity.kt

import android.app.TimePickerDialog
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
import java.text.SimpleDateFormat
import java.util.*

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

pickTimeBtn.setOnClickListener {
val cal = Calendar.getInstance()
val timeSetListener = TimePickerDialog.OnTimeSetListener { view, hourOfDay, minute ->
cal.set(Calendar.HOUR_OF_DAY, hourOfDay)
cal.set(Calendar.MINUTE, minute)

timeTv.text = SimpleDateFormat("HH:mm").format(cal.time)
}
TimePickerDialog(this, timeSetListener, cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), false).show()
}
}
}

Run

Copy the code or download it in the link below, build and run.

Reference

Here are the reference links:

NumberLink
1.Download Example
2.Follow code author
3.Code: Apache 2.0 License

Conclusion

In this article, we discussed what TimePickerDialog is, how to use it in your Android application, and provided code examples. TimePickerDialog is an essential Android widget that allows users to select a time easily. We hope this article has helped you understand how to use TimePickerDialog in your Android application.