×

TimePicker

The TimePicker component can be used to launch a 12-hour time selection view. If the time is not explicitly set by the developer, the TimePicker will read and display the device’s current time. Developers can set a custom time, which will then be displayed upon launching the TimePicker view.

Figure 1. TimePicker

There is only one constructor for initializing the TimePicker component, which takes no arguments. A TimePickerListener should be set to a given TimePicker instance in order for the application to receive a notification via the notifyTimePickerListener call back method, as soon as the TimePicker view has been dismissed by the end user.

The following snippet demonstrates how to initialize and launch a TimePicker from a FormItem. The call back notifyTimePickerListener method is used in this case to return back to the Form from where the TimePicker was originally launched:

timePicker = new TimePicker();
timePicker.setTimePickerListener(this);        
       
FormItem item = new FormItem("Alarm time", timePicker.getTimeString(), false);
item.setFormItemListener(this);
public void notifyFormItemListener(FormItem item, Component component, boolean actionButtonPressed) {
       if(item.getItemText().equals("Alarm time")) {
           timePicker.show();
       }
public void notifyTimePickerListener(TimePicker tp) {
    form.show(); 
}

Developers can prevent the current time from being displayed, by enforcing a specific time prior to calling the show method on the TimePicker instance as follows:

timePicker.set(TimePicker.HOUR, 10);
timePicker.set(TimePicker.MINUTE, 10);
timePicker.set(TimePicker.AM_PM, TimePicker.AM);

In order to retrieve the time selected by the end user, the get method should be used from within the notifyTimePickerListener method as follows:

public void notifyTimePickerListener(TimePicker tp) {
    System.out.println(tp.get(TimePicker.HOUR));
    System.out.println(tp.get(TimePicker.MINUTE));
    System.out.println((tp.get(TimePicker.AM_PM) == TimePicker.AM)?"AM":"PM");
}

Last updated 22 July 2013

Back to top

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×