Time Picker With AM/PM Values - Android Example


Simulator Screenshots

Timepicker main screen
Timepicker main screen

Android Example Screenshot 1

Select time picker time
Select time picker time

Android Example Screenshot 2

Show time on 12 hr format with AM PM
Show time on 12 hr format with AM PM

Android Example Screenshot 3

Select  again time
Select  again time

Android Example Screenshot 4

Show time on 12 hr format with AM PM
Show time on 12 hr format with AM PM

Android Example Screenshot 5

Download Code

Login To Download Source Code
    
 
Related Examples
Dynamically_Create_View_Elements__-_Android_Example  Dynamically Create View Elements - Android Example
Get_Registered_Email_Accounts_-_Android_Example  Get Registered Email Accounts - Android Example
Create_Notification_Alert_-_Android_Example  Create Notification Alert - Android Example
Skew_Or_Bind__Image_On_SDCARD_-_Android_Example  Skew Or Bind Image On SDCARD - Android Example
Swipe_screen_left__right__top_bottom  Swipe screen left right top bottom
Create_Repeating_Alarm_Start_After_Each_2_Minutes  Create Repeating Alarm Start After Each 2 Minutes
In_this_example_creating_a_date_picker_to_pick_day__month_year_of_date  In this example creating a date picker to pick day month year of date.
Time_Picker_Basics_Part_1_-_Android_Example  Time Picker Basics Part 1 - Android Example
Spinner_Basics_-_Android_Example  Spinner Basics - Android Example
Checkbox_Basics_With_Example  Checkbox Basics With Example
 
 

In this example creating time picker with 12 hours format with AM/PM values.

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);
		
		// set current time into output textview
		updateTime(hour, minute);
		
	 /********* 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;

			updateTime(hour,minute);
			
         }

	};

	private static String utilTime(int value) {
		
		if (value < 10)
			return "0" + String.valueOf(value);
		else
			return String.valueOf(value);
	}
	
	// Used to convert 24hr format to 12hr format with AM/PM values
	private void updateTime(int hours, int mins) {
		
		String timeSet = "";
		if (hours > 12) {
			hours -= 12;
			timeSet = "PM";
		} else if (hours == 0) {
			hours += 12;
			timeSet = "AM";
		} else if (hours == 12)
			timeSet = "PM";
		else
			timeSet = "AM";

		
		String minutes = "";
		if (mins < 10)
			minutes = "0" + mins;
		else
			minutes = String.valueOf(mins);

		// Append in a StringBuilder
		 String aTime = new StringBuilder().append(hours).append(':')
				.append(minutes).append(" ").append(timeSet).toString();

		  output.setText(aTime);
	}
}

 

    
 
videos
Google