How to Use Time Picker in Android to Pick Time By Hour and Minute

Time-picker-in-Android-624x276

Time Picker in Android is a widget that lets users to select the time of the day in either AM/PM or 24 hours mode.

The displayed time in the widget consists of hours, minutes, and the clock format. And, in order to show add this widget in your mobile app, you’ll have to use a TimePickerDialog class.

In today’s Android app tutorial, we’ll see how to add time picker in Android app to allow users pick time by hour, minute, and clock format.

Let’s Get Started

Create a new project “Android Time Picker”.

ss1-1024x726

Select targeted Android device.

ss2-1024x724

Add an activity in mobile => Select Empty Activity.

ss3-1024x724

Lastly, customize the activity.

ss4-1024x720

Now, create java class name “MainActivity”

public class MainActivity extends AppCompatActivity {
static final int TIME_DIALOG_ID = 1111;
private TextView view;
public Button btnClick;
private int hr;
private int min;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
view = (TextView) findViewById(R.id.output);
final Calendar c = Calendar.getInstance();
hr = c.get(Calendar.HOUR_OF_DAY);
min = c.get(Calendar.MINUTE);
updateTime(hr, min);
addButtonClickListener();
}
public void addButtonClickListener() {
btnClick = (Button) findViewById(R.id.btnClick);
btnClick.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
createdDialog(0).show();
}
});
}
protected Dialog createdDialog(int id) {
switch (id) {
case TIME_DIALOG_ID:
return new TimePickerDialog(this, timePickerListener, hr, min, 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
hr = hourOfDay;
min = minutes;
updateTime(hr, min);
}
};
private static String utilTime(int value) {
if (value < 10) return "0" + String.valueOf(value); else return String.valueOf(value); } 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);
String aTime = new StringBuilder().append(hours).append(':').append(minutes).append(" ").append(timeSet).toString();
view.setText(aTime);
}

activity_main.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:gravity="center"
android:orientation="vertical" >
<Button
android:id="@+id/btnClick"
android:padding="20dp"
android:background="@color/colorPrimary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me &amp; Change Time" />
<TextView
android:id="@+id/lblTime"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Current Time"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/output"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textColor="@color/colorAccent"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>

And Done!

final-1024x569

Once you successfully implement this code, you can easily add Android tabs in your application. Although, if an app is complex, then TimePickerDialog integration becomes bit difficult so if you’re not completely aware with it, you can consult with Android application development agency to add custom Time picker in Android app correctly.

Get a free copy of TimePicker in Android Example from Github.

You may also like,

This page was last edited on May 25th, 2020, at 5:23.

Author Bio

Amit Patolia

Amit Patolia

Designation: Android Team Lead

Amit Patolia is an Android Team Lead at Space-O Technologies. He has over 7 years of experience in Android app development. He has guided to develop over 100 Android apps with unique features and functionalities. He also has expertise in Kotlin-based apps.

 
 

Have an Idea for Android App? Let's Discuss!

Get your free consultation now

Leave a Reply

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

Get Your Free Quote