In this example creating time picker. time picker is used for selecting the time of day, in either 24 hour or AM/PM mode. The hour, each minute digit, and AM/PM (if applicable) can be conrolled by vertical spinners.

Use android.widget.TimePicker class to create time picker.

Use OnTimeChangedListener to get time picker event.

 

Project Structure :

 

TimePicker_basics_project_sketch

 

File : res/layout/activity_time_picker.xml

 


    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
        <Button
            android:id="@+id/btnClick"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Click To Change Time" />
    
        <TextView
            android:id="@+id/lblTime"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Current Time (H:M): "
            android:textAppearance="?android:attr/textAppearanceLarge" />
      
        <TextView
            android:id="@+id/output"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:textAppearance="?android:attr/textAppearanceLarge" />
    
    </LinearLayout>

 

 

File : src/TimePickerExample.java

 


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

public class TimePickerExample extends Activity {

	static final int TIME_DIALOG_ID = 1111;
	private TextView output;
	public Button btnClick;

	private int hour;
	private int minute;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_time_picker);

        output = (TextView) findViewById(R.id.output);
		
     /********* display current time on screen Start ********/
     
		final Calendar c = Calendar.getInstance();
		// Current Hour
		hour = c.get(Calendar.HOUR_OF_DAY);
		// Current Minute
		minute = c.get(Calendar.MINUTE);
		
		// Append in a StringBuilder
		StringBuilder outputMsg = new StringBuilder().append(utilTime(hour)).append(":")
		.append(utilTime(minute));
		
		// set current time into output textview
		output.setText(outputMsg);
		
	 /********* display current time on screen End ********/
		
		// Add Button Click Listener
		addButtonClickListener();

	}

	public void addButtonClickListener() {

		btnClick = (Button) findViewById(R.id.btnClick);

		btnClick.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				showDialog(TIME_DIALOG_ID);

			}

		});

	}

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

		}
		return null;
	}

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

		@Override
		public void onTimeSet(TimePicker view, int hourOfDay, int minutes) {
			// TODO Auto-generated method stub
			hour   = hourOfDay;
			minute = minutes;

			// set current time into output textview
			output.setText(new StringBuilder().append(utilTime(hour))
					.append(":").append(utilTime(minute)));
         }

	};

	private static String utilTime(int value) {
		
		if (value < 10)
			return "0" + String.valueOf(value);
		else
			return String.valueOf(value);
	}
}