Time Picker in Android

This entry is part 44 of 50 in the series Android Development Primer

Just like Number Picker provides a neat way to select values from a given range, Android also provides a Time Picker tool. This, as the name suggest is used to pick time. Now, you can use the Time Picker the way you use Number Picker, but this post shows how to use it with the help of a TimePickerDialog.

  • Create a layout for an activity and insert a button. This button will trigger a dialog which will contain the TimePicker.
  • Define the button in the Java file.
  • Set the onClick() method for the button.
    b.setOnClickListener(new OnClickListener() {
    
    			@Override
    			public void onClick(View v) {
    				showDialog(9999);
    			}
    		});
    
  • Create a new method onCreateDialog(). This comes into play when we call a Dialog to appear quite like the one we did above.
    protected Dialog onCreateDialog(int id){
    		switch(id){
    			case 9999:
    				return new TimePickerDialog(this, timePickerListener, Calendar.HOUR_OF_DAY, Calendar.MINUTE, false);
    		}
    		return null;
    	}
    
  • We are setting the current time of the device as the default time every time the dialog appears.
  • Now, we need to define what happens when a time is selected. Here, I am just creating a Toast to show the selected time. Remember that you can do whatever you want. We will override the method onTimeSetListener()
    private TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener() {
    
    		@Override
    		public void onTimeSet(TimePicker view, int newhour, int newminute) {
    			Toast.makeText(getApplicationContext(), newhour+":"+newminute, Toast.LENGTH_LONG).show();
    		}
    	};
    
  • Save and run the application on an emulator or device.

Tpick1  Tpick2

Remember that the Time is always set in 24 Hour format.

COMPLETE SOURCE CODE

MainActivity.java

package com.nero.mysecondapp;

import java.util.Calendar;

import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.NumberPicker;
import android.widget.TimePicker;
import android.widget.Toast;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Button b = (Button) findViewById(R.id.button1);
		b.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				showDialog(9999);
			}
		});
	}

	protected Dialog onCreateDialog(int id){
		switch(id){
			case 9999:
				return new TimePickerDialog(this, timePickerListener, Calendar.HOUR_OF_DAY, Calendar.MINUTE, false);
		}
		return null;
	}

	private TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener() {

		@Override
		public void onTimeSet(TimePicker view, int newhour, int newminute) {
			Toast.makeText(getApplicationContext(), newhour+":"+newminute, Toast.LENGTH_LONG).show();
		}
	};

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

Series Navigation<< Number Picker in AndroidAndroid development primer: Working with Sliding Drawers in Android – Part I >>

Leave a Reply

Your email address will not be published. Required fields are marked *

*

* Copy This Password *

* Type Or Paste Password Here *

42,383 Spam Comments Blocked so far by Spam Free Wordpress

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>