Friday, January 4, 2013

Android-TimePicker


Implement onCreateDialog method in your activity for timepicker:-

@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case TIME_DIALOG_ID:
timePicker = new TimePickerDialog(this,timePickerListener, hours, minutes,
false);
return timePicker;
}
return null;
}

Call whereever necessary the dialog by calling:-

showDialog(DATE_DIALOG_ID);

After selecting the time in time picker you can get the control to this listener


private TimePickerDialog.OnTimeSetListener timePickerListener
= new TimePickerDialog.OnTimeSetListener() {
             public void onTimeSet(TimePicker arg0, int hour, int minute) {
// TODO Auto-generated method stub
hours = hour;
minutes = minute;
};
};


When you want to call dialog again implement onPrepareDialog method like this:-
When you call again the time picker dialog the control will not go in to oncreatedialog method it comes to onPrepareDialog method. So implement this method if you want to call time picker at different contexts in activity.



@Override
 protected void onPrepareDialog(int id, Dialog dialog) {
  switch (id) {
 
  case TIME_DIALOG_ID: ((TimePickerDialog)dialog).show();
  }
 }



No comments:

Post a Comment