In this tutorial you will learn about how to pick dates and time using simple TimePickerDialog widget in android.
We mostly write our examples in Kotlin programming language.
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:
Number | Link |
---|---|
1. | Download Example |
2. | Follow code author |
3. | Code: Apache 2.0 License |