Android Time Picker Dialog and Example in Kotlin

Android Time Picker dialog is the user interface, where the user can select a time. Users can select time by Hour and Minutes. You can see the time picker example in the android default app alarm. How is working? When a user clicks on any widget like Button, that time you have to set click listener. Then a user clicks the button Android TimePickerDialog will popup.

Android Time Picker Dialog and Example in Kotlin

In this tutorial, you will learn the following:

  1. Overview Android Time Picker Dialog 
  2. Complete example and code of Android TimePickerDialog in kotlin

Let’s Build Android Android Time Picker Dialog example in kotlin:

In Android app development using kotlin language and showing TimePickerDialog is very easy, in this example, you will see how much easy. In the example, the selected Time by the user will show in Toast. You can show it in the TextView label or elsewhere your want to use it in your app. Let’s see the steps to how to do it ..(Note: there is any way to do it)

Step 1. Create a new project “Build Your First Android App in Kotlin
Step 2. Add following code in “activity_main.xml” layout file

Where button have method to perform a onClick action.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="Hello World!"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:onClick="clickTimePicker"
        android:text="Open Time Picker"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

</android.support.constraint.ConstraintLayout>
Step 3. Open the “MainActivity.kt” and add following code

On clicking button – Creates a new time picker dialog for the current time using the parent context’s default time picker dialog theme. Context is requires the application context.

  • var hour : It shows the the current hour that’s visible when the dialog pops up
  • var minute : It shows the the current minute that’s visible when the dialog pops up
package `in`.eyehunt.androidtimepicker

import android.app.TimePickerDialog
import android.icu.util.Calendar
import android.os.Build
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.annotation.RequiresApi
import android.view.View
import android.widget.Toast

class MainActivity : AppCompatActivity() {

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

    @RequiresApi(Build.VERSION_CODES.N)
    fun clickTimePicker(view: View) {
        val c = Calendar.getInstance()
        val hour = c.get(Calendar.HOUR)
        val minute = c.get(Calendar.MINUTE)

        val tpd = TimePickerDialog(this,TimePickerDialog.OnTimeSetListener(function = { view, h, m ->

            Toast.makeText(this, h.toString() + " : " + m +" : " , Toast.LENGTH_LONG).show()

        }),hour,minute,false)

        tpd.show()
    }
}
Step 4. Now Run the application, in emulator or On you android device

Output screenshot Android Time Picker Dialog :

Android Time Picker Dialog and Example in Kotlin output

Download source code Android TimePickerDialog in kotlin

https://github.com/eyehunt/AndroidTimePicker

Note : This example (Project) is developed in Android Studio 3.1.3 . Tested on Android 9 ( Android-P), compile SDK version API 27: Android 8.0 (Oreo)

MinSdkVersion=”15″

TargetSdkVersion=”27″

Coding in Kotlin


One thought on “Android Time Picker Dialog and Example in Kotlin

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.