This tutorial will show you how to implement the android timepicker as a popup, select time, set the pickers time manually and change between 24hour and 12hour format.
The android timepicker is an elegant way of asking the user for time input. Let’s start by adding code to the MainActivity.
File: MainActivity.java
package com.ahotbrew.timepicker; import android.app.Dialog; import android.app.TimePickerDialog; import android.support.v4.app.DialogFragment; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.text.format.DateFormat; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.TimePicker; import java.util.Calendar; public class MainActivity extends ActionBarActivity { public static TimePicker timePicker; public static class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { // Use the current time as the default values for the picker final Calendar c = Calendar.getInstance(); int hour = c.get(Calendar.HOUR_OF_DAY); int minute = c.get(Calendar.MINUTE); // Create a new instance of TimePickerDialog and return it return new TimePickerDialog(getActivity(), this, hour, minute, DateFormat.is24HourFormat(getActivity())); } public void onTimeSet(TimePicker view, int hourOfDay, int minute) { timePicker.setCurrentHour(hourOfDay); timePicker.setCurrentMinute(minute); } } public void showTimePickerDialog(View v) { DialogFragment newFragment = new TimePickerFragment(); newFragment.show(getSupportFragmentManager(), "timePicker"); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); timePicker = (TimePicker) findViewById(R.id.timePicker); //Comment it back in if you want the view to be 24 Hour format //timePicker.setIs24HourView(true); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
You can set the 24hour or 12hour format with this one line of code. Value can be true or false.
timePicker.setIs24HourView(true);
File: activity_main.xml
<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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context="com.ahotbrew.timepicker.MainActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/pick_time" android:onClick="showTimePickerDialog" android:id="@+id/button" /> <TimePicker android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/timePicker" android:layout_below="@+id/button" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_marginTop="66dp" /> </RelativeLayout>
File: strings.xml
<resources> <string name="app_name">TimePicker</string> <string name="title_activity_main">Ahotbrew.com TimePicker</string> <string name="action_settings">Settings</string> <string name="pick_time">Open Time Picker</string> </resources>
Sit back, take a sip from your hot brew and run your project.
Don’t forget to join or newsletter and get free android tutorials to enjoy with your hot brew.