Android TimePicker Example

By | April 8, 2013
In this tutorial we will see about Android TimePicker Dialog  ,we will see an example of selecting Time from Timepicker dialog and display it in TextView.This dialog will disable the background activity until the user select the time and click ‘done’ tab of the dialog shown.


Firstly We get the instance of calender class and get the minute and hour of the system and set it to textview.

Constructing Dialog

To construct Dialog we make use “TimePickerDialog” class , A dialog that prompts the user for the time of day using a TimePicker.

TimePickerDialog (Context context, TimePickerDialog.OnTimeSetListener callBack, int hourOfDay, int minute, boolean is24HourView)

when the dialog gets prepared the onPrepareDialog(int id, Dialog dialog) is called and it updates the  time which is set by user .

Lets See An Example

Project Detail

Project Name TimepickerDemo
Package com.pavan.timepickerdemo
Minimum SDK API 8
Target SDK API 17
Theme Holo Light with Dark Action Bar

1. XML Layout

 file:- activity_main.xml

<Relativelayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:paddingbottom="@dimen/activity_vertical_margin"
android:paddingleft="@dimen/activity_horizontal_margin"
android:paddingright="@dimen/activity_horizontal_margin"
android:paddingtop="@dimen/activity_vertical_margin"
tools:context=".TimePicker" >

<TextView
android:id="@+id/display_id"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="" />

<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/change_button_id"
android:layout_alignleft="@+id/display_id"
android:layout_below="@+id/display_id"
android:layout_margintop="28dp"
android:text="@string/changetimebutton" />

</RelativeLayout>

2. Activity

 file :- TimePicker.java

package com.pavan.timepickerdemo;

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.Button;
import android.widget.TextView;

public class TimePicker extends Activity {

Button change_time_but;
TextView display_txt;
private int mHour;
private int mMinute;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.time_picker);
display_txt = (TextView) findViewById(R.id.display_id);
change_time_but = (Button) findViewById(R.id.change_button_id);
change_time_but.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
TimePickerDialog TPD = new TimePickerDialog(TimePicker.this,
mTimeSetListener, mHour, mMinute, false);
TPD.show();
}
});
final Calendar c = Calendar.getInstance();
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);

updateDisplay();
}

@Override
@Deprecated
protected void onPrepareDialog(int id, Dialog dialog) {
// TODO Auto-generated method stub
super.onPrepareDialog(id, dialog);
((TimePickerDialog) dialog).updateTime(mHour, mMinute);
}

private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {

@Override
public void onTimeSet(android.widget.TimePicker view, int hourOfDay,
int minute) {
// TODO Auto-generated method stub
mHour = hourOfDay;
mMinute = minute;
updateDisplay();

}
};

private void updateDisplay() {
// TODO Auto-generated method stub
display_txt.setText(new StringBuilder().append(mHour).append(":")
.append(mMinute));
}

}

3. RUN