TimePicker View Tutorial

The TimePicker view enables users to select a time of the day, in either 24 Hour mode or AM/PM mode. You can use “android.widget.TimePicker” class to render a time picker component to select hour and minute in a pre-defined user interface.

You can create TimePicker View using <TimePicker> xml element like this -

<TimePicker
        android:id="@+id/timePicker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

The TimePicker view displays a standard UI to enable user to set a time. By default, it displays time in the AM/PM format. If you want to change time in the 24 hour format, then you can use the setIs24HourView() method. There are two other important methods of TimePicker -
  • getCurrentHour()
  • getCurrentMinute()
Please note that getCurrentHour() method always returns the hour in 24-hour format i.e. value from 0 to 23.

Example of TimePicker

Project Name : TimePickerDemo
Package : com.sunil.timepickerdemo
Build Target : Android 2.2


main.xml
(File: res/layout/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:orientation="vertical" >

      <TimePicker
        android:id="@+id/timePicker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
     
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Check Time" />

 </LinearLayout>


TimePickerDemoActivity
(File: TimePickerDemoActivity.java)

package com.sunil.timepickerdemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TimePicker;
import android.widget.Toast;

public class TimePickerDemoActivity extends Activity {
    /** Called when the activity is first created. */
   
    TimePicker timepicker;
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        timepicker = (TimePicker) findViewById(R.id.timePicker);
        timepicker.setIs24HourView(true);
       
      
        // Button View
       Button button = (Button) findViewById(R.id.btn);
       button.setOnClickListener(new View.OnClickListener() {
       
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
       
        Toast.makeText(getBaseContext(),"Time Selected : "+timepicker.getCurrentHour()+":"
                +timepicker.getCurrentMinute(), Toast.LENGTH_SHORT).show();

        }
    });
    }
}


Here is the output of this example-


That's its ! You can also download the source code of this tutorial-

Download Code





Be the first to comment

Post a Comment

Recent Posts

Site Stats

Our Apps

Get it on Google Play

©2012-2014 AndroidAspect - All rights reserved | diwali greetings | Powered by Blogger

Creative Commons License