Time Picker Dialog in Android
Android TimePicker is a user interface control for selecting the time in either 24-hour format or AM/PM mode. It is used to ensure that users pick the valid time for the day in our application. The time picker interface exists basically in two modes one is under XML layout and another is a dialog. In this discussion, it’s been demonstrated how to implement TimePicker Dialog in android. Refer to TimePicker in Kotlin for the TimePicker in the XML layout. Have a look at the following image to get an overview of the discussion.
Steps to implement Time Picker Dialog in Android app
Step 1: Create an empty activity project
Create an empty activity Android Studio project and select Kotlin as the programming language. Android | How to Create/Start a New Project in Android Studio?
Step 2: Working with the activity_main.xml file
The main layout of the application contains a button and TextView to preview the picked time. To implement the same layout invoke the following code inside the activity_main.xml file.
Before Heading to implement the TimePicker dialog, understanding the dialog anatomy is important
Step 3: Working with MainActivity.kt file
In the MainActivity.kt file the instance of TimePickerDialog which takes 5 parameters.
TimePickerDialog(Context, TimePickerDialogListener, DefaultHourOfDialog(Int), DefaultMinuteOfDialog(Int), is24HourView(boolean))
To implement TimePicker Dialog invoke the following code.