DatePicker

DatePickers allow developers to enrich their applications with customizable date input fields. While Lwuit’s Calendar is a native cross platform component, the DatePicker component is available only on Nokia Asha software platform and offers a more native platform look. The DatePicker is displayed in full screen view that extends Form. Therefore it cannot be added as a component on a Form. However it can be launched from components already appended to a Form such as Commands, Buttons or FormItems by calling the show method. When both a DatePicker and a TimePicker are needed, it is advised to use a FormItem instance as launcher, because a single FormItem can wrap both components and display their values.

The DatePicker comes in three different variants: the Three Column DatePicker, the Two Column DatePicker and the Month View DatePicker.

Three Column DatePicker

The Three Column DatePicker type can display a given Date in the following formats:

  • DD MM YYYY

  • MM DD YYYY

  • YYYY MM DD

Figure 1. Three Column DatePicker

The developer can choose to enforce a Date format, or allow the DatePicker to automatically read and apply the device’s Date format. Please note that while there are more than 3 Date formats supported by the device, all of them fall under one of the three options shown above in terms of ordering the Date fields. It is therefore the order of the Date fields that is taken from the device, while the number of characters per field remains fixed. For example if the chosen device Date format is DD.MM.YYYY, DD/MMM/YYYY or DD-MM-YY, the DatePicker format will be set in all these cases to DD MM YYYY.

In order to enforce the DD MM YYYY format the following constructor should be used:

datePicker = new DatePicker("DD MM YYYY", DatePicker.TYPE_PICKER_DD_MM_YYYY);

In order to enforce the MM DD YYYY format this constructor should be used instead:

datePicker = new DatePicker("MM DD YYYY", DatePicker.TYPE_PICKER_MM_DD_YYYY);

In order to enforce the YYYY MM DD format, the correct initialization would be:

datePicker = new DatePicker("YYYY MM DD", DatePicker.TYPE_PICKER_YYYY_MM_DD);

In order to let the DatePicker get the order of the Date fields as defined by the device, we simply use the empty constructor:

datePicker = new DatePicker();

As mentioned above, the DatePicker component cannot be appended to a Form, so it needs to be launched from a Component that receives triggers such as the FormItem:

FormItem item = new FormItem("Select your birthday", datePicker.getValue(), false);

In order to launch the DatePicker in the first place from a FormItem, we need to set a FormItemListener, implement the notifyFormItemListener method, which is called every time the FormItem is tapped. When a tap is detected we simply show the DatePicker as follows:

item.setFormItemListener(this);

public void notifyFormItemListener(FormItem item, Component component, boolean actionButtonPressed) {
    if (item.getItemText().equals("Select your birthday")) {
        datePicker.show();
    }
}

The entire DatePicker view is automatically generated by LWUIT. However, it is up to the developer to add a DatePickerListener so that selecting the OK Command from the DatePicker view would tell the DatePicker what to do next:

datePicker.setDatePickerListener(this);

In order to return from the DatePicker view back to the main form, we need to show the Form instance again. Besides navigating away from the DatePicker view, the notifyDatePickerListener call back can be used to read and further process the date fields selected by the end user, with the getYear, getMonth and getDay methods as follows:

public void notifyDatePickerListener(DatePicker dp) {
    System.out.println(dp.getYear());
    System.out.println(dp.getMonth());
    System.out.println(dp.getDay());
    form.show();
}

Two Column DatePicker

The Two Colum DatePicker displays the month and the year of the chosen Date using 2 and 4 digits respectively. The display order is not customizable for this type of DatePicker.

Figure 2. Two Column DatePicker

The Two Column DatePicker can be initialized with the following constructor:

datePicker = new DatePicker("MM YYYY", DatePicker.TYPE_PICKER_MM_DD_YYYY);

Retrieving the month and year values and returning back to the main Form can be done as follows:

public void notifyDatePickerListener(DatePicker dp) {
    System.out.println(dp.getYear());
    System.out.println(dp.getMonth());
    form.show();
}

Month View DatePicker

The Month View DatePicker displays the days of the month in a 5 x 7 grid, in such a way so that the last four rows of the grid are always filled. By default, the current day, as it is read from the device, will be highlighted. Developers can enforce the Month View DatePicker to highlight another day, or retrieve the highlighted day by calling the methods setDay and getDay respectively.

Figure 3. Month View DatePicker

This type of DatePicker can be initialized with the following constructor:

datePicker = new DatePicker("DAY", DatePicker.TYPE_PICKER_DAY);

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.

×