Android TimePicker

| Page Views: 23

Android TimePicker Tutorial and Examples.

A TimePicker is a widget for selecting the time of day, in either 24-hour or AM/PM mode.

TimePicker allows you select only time. If you want to select dates then you use DatePicker.

TimePicker for example is displayed in a Dialog to form TimePickerDialog.

TimePicker API Definition

TimePicker was added in API level 1.

As a clas it resides in the android.widget package and derives from FrameLayout.

public class TimePicker extends FrameLayout 

Here's it's inheritance tree:

   ↳    android.view.View
       ↳    android.view.ViewGroup
           ↳    android.widget.FrameLayout
               ↳    android.widget.TimePicker

Quick TimePicker Examples

Android TimePicker Examples

Example 1

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.DatePicker;
import android.widget.TimePicker;

import java . useful . Calendar ;

public class MainActivity extends AppCompatActivity {      

    private TimePicker timePicker ;   
    private DatePicker datePicker;
    Private Calendar calendar ;  
    private int year;
    private int month;
    private int day ;  
    private int hour;
    private int minute;
    protected void onCreate(Bundle savedInstanceState) {
        / / Get an object of the calendar 
        calendar = Calendar.getInstance();
        / / Get the year, month, day, minute, second 
        year = calendar.get(Calendar.YEAR);
        month = calendar.get(Calendar.MONTH);
        day = calendar.get(Calendar.DAY_OF_MONTH);
        hour = calendar.get(Calendar.HOUR);
        minute = calendar.get(Calendar.MINUTE);
        setTitle(year+"-"+ month%12 +"-"+day+"-"+" "+hour+":"+minute);
        datePicker = (DatePicker) findViewById(;
        timePicker = (TimePicker) findViewById(;

                year, month, day, new DatePicker.OnDateChangedListener() {
                    public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
            public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
                setTitle(hourOfDay + ":" + minute);

        new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                setTitle(year + "-"+(monthOfYear+1)+"-"+dayOfMonth);

        new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
    android:layout_width="match_parent" android:layout_height="match_parent"

        android:id="@+id/datePicker" />

        android:layout_gravity="bottom" />

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section. This feedback helps us gauge our progress.

Help me Grow.

I set myself some growth ambitions I desire to achieve by this year's end regarding this website and my youtube channel. Am halfway. Help me reach them by:


What do You Think

Previous Post Next Post