Ilias Tsagklis

About Ilias Tsagklis

Ilias Tsagklis is a senior software engineer working in the telecom domain. He is an applications developer in a wide variety of applications/services. Ilias is co-founder and Executive Editor at Java Code Geeks.

Android Time Picker Example

Android it’s very easy to set the time using the android.widget.TimePicker component. In this tutorial we are going to see how the user can select the hour, and the minute using the which is an easy to use dialog box.


For this tutorial, we will use the following tools in a Windows 64-bit platform:

  1. JDK 1.7
  2. Eclipse 4.2 Juno
  3. Android SKD 4.2

1. Create a new Android Project

Open Eclipse IDE and go to File -> New -> Project -> Android -> Android Application Project. You have to specify the Application Name, the Project Name and the Package Name in the appropriate text fields and then click Next.

In the next window make sure the “Create activity” option is selected in order to create a new activity for your project, and click Next. This is optional as you can create a new activity after creating the project, but you can do it all in one step.

Select “BlankActivity” and click Next.

You will be asked to specify some information about the new activity.  In the Layout Name text field you have to specify the name of the file that will contain the layout description of your app. In our case the file res/layout/main.xml will be created. Then, click Finish.

2. Adding resources

Use the Package Explorer in Eclipse to navigate to res/values/strings.xml

When you open the strings.xml file, Eclipse will display the graphical Resources View editor :

That’s a nice and easy tool you can use to add several resources to your application like strings, integers, color values etc. But we are going to use the traditional way and that is editing the strings.xml file by hand. In the bottom of the screen, press the string.xml tab and paste the following code :

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

    <string name="app_name">TimePickerExample</string>
    <string name="button_label">Change Time</string>
    <string name="time_txt">Current Time (H:M): </string>
    <string name="menu_settings">Settings</string>


So, we’ve just created some string resources that we can use in many ways and in many places in our app.

3. Creating the Time Picker

Open res/layout/main.xml file :

And paste the following code :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
    android:orientation="vertical" >

        android:text="@string/button_label" />

        android:textAppearance="?android:attr/textAppearanceLarge" />

        android:textAppearance="?android:attr/textAppearanceLarge" />

        android:layout_height="wrap_content" />


Now you may open the Graphical Layout editor to preview the User Interface you created:

4. Code

Go to the java file that contains the code of the activity you’ve just created and paste the following code:


import java.util.Calendar;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;

public class MainActivity extends Activity {

	private TextView textViewTime;
	private TimePicker timePicker;
	private Button button;

	private int hour;
	private int minute;

	static final int TIME_DIALOG_ID = 999;

	public void onCreate(Bundle savedInstanceState) {



	// display current time
	public void setCurrentTimeOnView() {

		textViewTime = (TextView) findViewById(;
		timePicker = (TimePicker) findViewById(;

		final Calendar c = Calendar.getInstance();
		hour = c.get(Calendar.HOUR_OF_DAY);
		minute = c.get(Calendar.MINUTE);

		// set current time into textview
		textViewTime.setText(new StringBuilder().append(padding_str(hour)).append(":").append(padding_str(minute)));

		// set current time into timepicker


	public void addButtonListener() {

		button = (Button) findViewById(;

		button.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {





	protected Dialog onCreateDialog(int id) {
		switch (id) {
			// set time picker as current time
			return new TimePickerDialog(this, timePickerListener, hour, minute,false);

		return null;

	private TimePickerDialog.OnTimeSetListener timePickerListener =  new TimePickerDialog.OnTimeSetListener() {
		public void onTimeSet(TimePicker view, int selectedHour, int selectedMinute) {
			hour = selectedHour;
			minute = selectedMinute;

			// set current time into textview
			textViewTime.setText(new StringBuilder().append(padding_str(hour)).append(":").append(padding_str(minute)));

			// set current time into timepicker


	private static String padding_str(int c) {
		if (c >= 10)
		   return String.valueOf(c);
		   return "0" + String.valueOf(c);

5. Run the application

This is the main screen of our Application. When the application launches, the setCurrentTimeOnView() method is called, finds out the current time using java.util.Calendar, and updates the values both on the TextView and the Time Picker:

Now, press “Change Time”. When you press that button, the onClick method is called, which calls showDialog(). Now, when showDialog() is called, automatically onCreateDialog() is called. onCreateDialog() creates and returns a new TimePickerDialog . And so the dialog window with a Time Picker pops up. You can now adjust the time:

When you press “Done” the onTimeSet() method is called and it updates the values in the TextView and in the Time Picker:

Download Eclipse Project

This was an Android Time Picker Example. Download the Eclipse Project of this tutorial:

Related Whitepaper:

Beginning Android Application Development (Free Sample Chapter)

Create must-have applications for the latest Android OS!

The Android OS is a popular and flexible platform for many of today's most in-demand mobile devices. This full-color guide offers you a hands-on introduction to creating Android applications for the latest mobile devices. Veteran author Wei Meng Lee accompanies each lesson with real-world examples to drive home the content he covers. Beginning with an overview of core Android features and tools, he moves at a steady pace while teaching everything you need to know to successfully develop your own Android applications.

Get it Now!  

Examples Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Examples Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below: