Create TimePickerDialog to select time in 12 hours format with AM PM in android

How to use Time picker tool in android app to get selected time from user on button click and show into TextView.

TimePickerDialog is a type of pop up dialog box used to get time from user using pop up window activity method. In this tutorial we are opening TimePickerDialog  on button click then after entering time from user we will setting up the whole hours, minutes and AM/PM values into TextView. So here is the complete step by step tutorial for Create TimePickerDialog to select time in 12 hours format with AM PM in android.

android-project-download-code-button

How to Create TimePickerDialog to select time in 12 hours format with AM PM in android.

Code for MainActivity.java file.

package com.getselectedtimefromtimepicker_android_examples.com;
import java.util.Calendar;
import android.app.Activity;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;

public class MainActivity extends Activity {

 Button AccessTime;
 TextView DisplayTime;
 private int CalendarHour, CalendarMinute;
 String format;
 Calendar calendar;
 TimePickerDialog timepickerdialog;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 AccessTime = (Button)findViewById(R.id.button1);
 DisplayTime = (TextView)findViewById(R.id.textView1);
 
 AccessTime.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 
 calendar = Calendar.getInstance();
 CalendarHour = calendar.get(Calendar.HOUR_OF_DAY);
 CalendarMinute = calendar.get(Calendar.MINUTE);
 
 
 timepickerdialog = new TimePickerDialog(MainActivity.this,
 new TimePickerDialog.OnTimeSetListener() {
 
 @Override
 public void onTimeSet(TimePicker view, int hourOfDay,
 int minute) {
 
 if (hourOfDay == 0) {
 
 hourOfDay += 12;
 
 format = "AM";
 }
 else if (hourOfDay == 12) {
 
 format = "PM";
 
 } 
 else if (hourOfDay > 12) {
 
 hourOfDay -= 12;
 
 format = "PM";
 
 } 
 else {
 
 format = "AM";
 }
 

 DisplayTime.setText(hourOfDay + ":" + minute + format);
 }
 }, CalendarHour, CalendarMinute, false);
 timepickerdialog.show();
 
 }
 });
 }
}

Code for activity_main.xml layout file.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.getselectedtimefromtimepicker_android_examples.com.MainActivity" >

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="171dp"
 android:text="Selected Time will display here"
 android:textAppearance="?android:attr/textAppearanceLarge"
 android:gravity="center" />

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/textView1"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="31dp"
 android:text="Click Here To Open Time Picker Dialog To pic Time" />

</RelativeLayout>

Screenshots:

Create TimePickerDialog to select time in 12 hours format with AM PM in android

TimePickerDialog-2

TimePickerDialog-3

Click here to download Create TimePickerDialog to select time in 12 hours format with AM PM in android project with source code.

Leave a Reply

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