Android TimePicker Tutorial With Example

Hello Readers! In this post, we are going to learn about how to use android timePicker widget in any android application. We will also go through different attributes of timePicker widget that can be used to customise it.


Tutorialwing Android TimePicker Output Android TimePicker Tutorial With Example

Tutorialwing Android TimePicker Output

Getting Started

TimePicker widget can be defined as below –

TimePicker is a widget to select time in either AM/PM format or 24-hour format.

Attributes of Android TimePicker Widget

Attributes of android TimePicker widget are –

Sr. XML Attributes Description
1 android:timePickerMode Specifies look of the TimePicker widget

Some of the popular attributes of TimePicker inherited from FrameLayout are –

Sr. XML Attributes Description
1 android:foregroundGravity It defines the gravity of the foreground drawable
2 android:measureAllChildren When measuring, It specifies whether to measure all children or only those that are in visible or invisible state

Some of the popular attributes of TimePicker inherited from ViewGroup are –

Sr. XML Attributes Description
1 android:animationCache Defines whether layout animations should create a drawing cache for their children
2 android:layoutAnimation Defines the layout animation to use the first time the ViewGroup is laid out
3 android:layoutMode Defines the layout mode

Some of the popular attributes of TimePicker inherited from View are –

Sr. XML Attributes Description
1 android:background Sets background of the view
2 android:clickable Sets whether view is clickable or not
3 android:fadeScrollbars Defines whether scrollbar should fade out when not in use or not
4 android:fitsSystemWindows Defines whether to adjust view layout according to system windows
5 android:id Defines id of the view
6 android:minHeight Defines minimum height
7 android:minWidth Defines minimum width
8 android:padding Defines padding of the view
9 android:paddingBottom Defines padding to bottom of the view
10 android:paddingEnd Defines padding to end of the view
11 android:visibility Defines visibility of the view

Example of Android TimePicker Widget

At first, we will create android application. Then, we will use timePicker widget in this application.

1. Creating New Project

Follow steps below to create new project. Please ignore the steps if you have already created a new application.

Step Description
1. Open Android Studio.
2. Go to File => New => New Project. Write application name as TimePicker. Then, click next button.
3. Select minimum SDK you need. However, we have selected 17 as minimum SDK. Then, click next button
4. Then, select Empty Activity => click next => click finish.
5. If you have followed above process correctly, you will get a newly created project successfully. However, you can also visit post to create a new project to know steps in detail.

Now, we will modify xml and java file to use timePicker widget in the application.

2. Modify Values folder

Open res/values/strings.xml file. Then, add below code into it.

	<string name="app_name">TimePicker</string>
	<string name="selected_time">The selected time is:</string>

3. Use TimePicker Widget in xml file

Open res/layout/activity_main.xml file. Then, add below code into it.

<?xml version="1.0" encoding="utf-8"?>




In activity_main.xml file, we have defined timePicker and textView widgets. TextView will be used to show selected time in timePicker widget. Now, we will access these widgets in java file to perform some actions on it.

4. Access TimePicker Widget in java file

Open src/main/java/com.tutorialwing.timepicker/ file. Then, add below code into it.

package com.tutorialwing.timepicker;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.TimePicker;

public class MainActivity extends AppCompatActivity {

	protected void onCreate(Bundle savedInstanceState) {


	private void getSelectedTime() {
		final TextView textView = findViewById(;
		TimePicker timePicker = findViewById(;
		timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
			public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {

				String format = "";
				if (hourOfDay == 0) {
					hourOfDay += 12;
					format = "AM";
				} else if (hourOfDay == 12) {
					format = "PM";
				} else if (hourOfDay > 12) {
					hourOfDay -= 12;
					format = "PM";
				} else {
					format = "AM";

				if (textView != null) {
					String hour = String.valueOf(hourOfDay < 10 ? "0" + hourOfDay : hourOfDay);
					String min = String.valueOf(minute < 10 ? "0" + minute : minute);

					String text = getString(R.string.selected_time) + " " + hour + " : " + min + " " + format;

In file, we have accessed textView and TimePicker widgets. Then, we have set a listener, time change listener, to get the time selected in timePicker widget. Then, we have shown the selected time in textView after some calculations. Calculation is just to show the time in proper format.

Since AndroidManifest.xml file is very important in any android application, we are also going to see the content inside this file.


Code inside src/main/AndroidManifest.xml file is as below –

<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.tutorialwing.timepicker"

		<activity android:name=".MainActivity">
				<action android:name="android.intent.action.MAIN"/>

				<category android:name="android.intent.category.LAUNCHER"/>


When we run the program, we will get output as shown above.

That’s end of tutorial on Android TimePicker widget.

Leave a Reply