Android TimePicker

Android TimePicker is a part of the android picker widget that is used to select date. It allows you to select time by hour and minute. You cannot select time by seconds. Android provides this functionality through TimePicker class. The android.widget.TimePicker is the subclass of FrameLayout class.


To use TimePicker class, First You have to define the TimePicker component in your activity.xml. It is define as below −

Next step to create an object of TimePicker class and get a reference of the above defined xml component.for the creating the object follow the below code.

To get the time on the selected user screen, you need to  use getCurrentHour() and getCurrentMinute() method of the TimePicker Class. Syntax is given as below.

There are other methods in the API that gives more control over TimePicker Component. They are listed below.

Sr.No Method & description
1 is24HourView()This method returns true if this is in 24 hour view else false
2 isEnabled()This method returns the enabled status for this view
3 setCurrentHour(Integer currentHour)This method sets the current hour
4 setCurrentMinute(Integer currentMinute)This method sets the current minute
5 setEnabled(boolean enabled)This method set the enabled state of this view
6 setIs24HourView(Boolean is24HourView)This method set whether in 24 hour or AM/PM mode
7 setOnTimeChangedListener(TimePicker.OnTimeChangedListener onTimeChangedListener)This method Set the callback that indicates the time has been adjusted by the user

Android TimePicker Example

Steps Description
1 You need to use Android studio to create an Android application and name it as TimePicker under a package com.example.timepicker. While creating this project, make sure you Target SDK and Compile With at the latest version of Android SDK to use higher levels of APIs.
2  Modify the res/layout/activity_main to add respective XML components
3 Modify src/ file to add necessary code.
4 Modify the res/values/string.xml to add necessary string components
5 Run the application and choose a running android device and install the application on it and verify the results


Following is the content to the modify activity XML.

Activity class

Following is the content to the modify main activity file


Now run our TimePicker application. To run the app from Android studio, open one of your project’s activity files and click Run Eclipse Run Icon icon from the toolbar. Android Studio installs the app on your AVD and starts it and if everything is fine with your setup and application, it will display following .