Android TimePicker


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:

java.lang.Object
   ↳    android.view.View
       ↳    android.view.ViewGroup
           ↳    android.widget.FrameLayout
               ↳    android.widget.TimePicker

Quick TimePicker Examples

Android TimePicker Examples

Example 1
(a). MainActivity.java

import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.support.v7.app.AppCompatActivity; 
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;
    @Override 
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main); 
        setContentView(R.layout.main);
        / / 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(R.id.datePicker);
        timePicker = (TimePicker) findViewById(R.id.timePicker);

        datePicker.init(
                year, month, day, new DatePicker.OnDateChangedListener() {
                    @Override 
                    public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                        setTitle(year+"-"+monthOfYear%12+"-"+dayOfMonth);
                    } 
                }); 
        timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
            @Override 
            public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
                setTitle(hourOfDay + ":" + minute);
            } 
        }); 

        new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @Override 
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                setTitle(year + "-"+(monthOfYear+1)+"-"+dayOfMonth);
            } 
        },year,calendar.get(Calendar.MONTH),day).show();

        new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
            @Override 
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                setTitle(hourOfDay+":"+minute);
            } 
        },hour,minute,true).show();
    } 

} 

(b).main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <DatePicker
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/datePicker" />

    <TimePicker
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/timePicker"
        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:




Recommendations


What do You Think


Previous Post Next Post