Android: Time picker in android example



        Lets see how to code time picker in android. It is exactly the same as datepicker explained in the previous tutorial. But anyway I will be explaining here as well.
Its very simple to program this time picker dialogbox functionality. You need to have an editText, ImageButton components in activitymain.xml.

Step 1

        When you click on Imagebutton it should open a dialog box with today's time and while clicking on "set" it will set the value in editText component. To get today's time  we need to create instance for Calendar class and get hourofday, minute values.

hour = cal.get(Calendar.HOUR_OF_DAY);
min = cal.get(Calendar.MINUTE);
AM/PM values will be set by default.(will be explained in detail below)

Step 2

Then we need to set the ImageButton to invoke calendar dialogbox.

ib.setOnClickListener(this);

This will return the TimePickerDialog with today's time.

 return new TimePickerDialog(this, timePickerListener, hour, min, false);

This is a constructor with some defined parameters.

timePickerListener- This is a callback to indicate the time has been set.

Last parameter is "is24hourView" which is of boolean type.  False -indicates 12 hour format, True - indicates 24 hour format. Now we have displayed time in 12 hour format.

Time picker Android
Step 3

Object for this listener is created as follows,

private TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener()

Once the time  is set this will invoke , "onTimeSet(TimePicker view, int hourOfDay, int minute)" method to perform user define functionality where we have assigned the time value to editText.


But before that an important point to note here is "hourofDay" will be returned in 24 hour format, ie., if you select 1:30 PM, it will return as 13:30, AM/PM values will not be returned and these are calculated as follows,

if (hourOfDay > 12)         //hourofDay =13
{
hour = hourOfDay - 12;     //hour=1
am_pm = "PM";                   //PM

else 
{
hour = hourOfDay;
am_pm = "AM";
}


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"
    tools:context=".MainActivity"    
     >
     <EditText
         android:id="@+id/editText"
         android:layout_width="250dp"
         android:layout_marginTop="50dp"
         android:layout_marginLeft="5dp"
         android:layout_height="wrap_content"
         android:editable="false" >
</EditText>
     
     <ImageButton
         android:id="@+id/imageButton1"
         android:layout_width="wrap_content"
   android:layout_height="wrap_content"
         android:layout_alignBottom="@+id/editText"
         android:layout_toRightOf="@+id/editText"
         android:contentDescription="@string/selectdate"
         android:cropToPadding="true"
         android:src="@drawable/one" />
</RelativeLayout>



MainActivity.java


package com.example.androiddatepicker;

import java.util.Calendar;

import android.app.Activity;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TimePicker;

public class MainActivity extends Activity implements OnClickListener {
 private ImageButton ib;
 private Calendar cal;
 private int hour;
 private int min;
 private EditText et;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  ib = (ImageButton) findViewById(R.id.imageButton1);
  cal = Calendar.getInstance();
  hour = cal.get(Calendar.HOUR_OF_DAY);
  min = cal.get(Calendar.MINUTE);
  et = (EditText) findViewById(R.id.editText);
  ib.setOnClickListener(this);
 }

 @Override
 public void onClick(View v) {
  showDialog(0);
 }

 @Override
 @Deprecated
 protected Dialog onCreateDialog(int id) {
  return new TimePickerDialog(this, timePickerListener, hour, min, false);
 }

 private TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener() {
  @Override
  public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
   int hour;
   String am_pm;
   if (hourOfDay > 12) {
    hour = hourOfDay - 12;
    am_pm = "PM";
   } else {
    hour = hourOfDay;
    am_pm = "AM";
   }
   et.setText(hour + " : " + minute + " " + am_pm);
  }
 };

}

Thats it, simple..!! We are done with timepicker in Android.

Download eclipse project here

Have a look at Android Date picker example.

 



Reactions:

0 comments :

Post a Comment