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.

Output

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.

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

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"?>
<LinearLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="match_parent"
	android:layout_height="match_parent"
	android:gravity="center"
	android:orientation="vertical">

	<TimePicker
		android:id="@+id/timePicker"
		android:layout_width="match_parent"
		android:layout_height="wrap_content"
		android:layout_marginLeft="40dp"
		android:layout_marginRight="40dp"
		android:layout_marginTop="5dp"/>

	<TextView
		android:id="@+id/textView"
		android:layout_width="match_parent"
		android:layout_height="wrap_content"
		android:layout_marginLeft="35dp"
		android:layout_marginTop="30dp"
		android:visibility="gone"/>

</LinearLayout>

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/MainActivity.java file. Then, add below code into it.

package com.tutorialwing.timepicker;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.TimePicker;

public class MainActivity extends AppCompatActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		getSelectedTime();
	}

	private void getSelectedTime() {
		final TextView textView = findViewById(R.id.textView);
		TimePicker timePicker = findViewById(R.id.timePicker);
		timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
			@Override
			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;
					textView.setText(text);
					textView.setVisibility(View.VISIBLE);
				}
			}
		});
	}
}

In MainActivity.java 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.

AndroidManifest.xml

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

<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.tutorialwing.timepicker"
		  xmlns:android="http://schemas.android.com/apk/res/android">

	<application
		android:allowBackup="true"
		android:icon="@mipmap/ic_launcher"
		android:label="@string/app_name"
		android:roundIcon="@mipmap/ic_launcher_round"
		android:supportsRtl="true"
		android:theme="@style/AppTheme">
		<activity android:name=".MainActivity">
			<intent-filter>
				<action android:name="android.intent.action.MAIN"/>

				<category android:name="android.intent.category.LAUNCHER"/>
			</intent-filter>
		</activity>
	</application>

</manifest>

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

That’s end of tutorial on Android TimePicker widget.

Leave a Reply