While writing an application for the BlackBerry, I wanted to emulate the layout of the Phone Log. Typically, this looks like a set of rows laid out in a two-column or multi-column format. Since there was no available field to achieve this, I had to write my own. The way I did it was to override the drawListRow method in an ObjectListField. One thing you can notice in your BlackBerry call log screen is that the last 10 characters are always reserved and never appear as an ellipsis. The last 10 characters are usually formatted thusly:
[Call Attribute: upto 4 characters][Date or Time: upto 6 characters]
Some examples of these are:
(W) 9:58p
(W2)11:22p
(H) 01/09
You will see in example 2 above, that all 10 characters are used up. After I figured this out, the rest was pretty straightforward. Here is the source code to my Field called PhoneNumberListField.
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.component.ListField;
import net.rim.device.api.ui.component.ObjectListField;
public class PhoneNumberListField extends ObjectListField
{
public PhoneNumberListField()
{
super();
}
public PhoneNumberListField(long style)
{
super(style);
}
public void drawListRow(ListField listField, Graphics graphics,
int index, int y, int width)
{
String tmpCol1 = (String) this.get(listField, index);
String col1 = tmpCol1.substring(0, (tmpCol1.length() - 10));
String col2 = tmpCol1.substring(tmpCol1.length()-10);
int col2Size = this.getFont().getAdvance(col2);
int col1Size = width - col2Size;
int col1Start = 0;
int col2Start = (width - col2Size);
graphics.drawText(col1, col1Start, y, DrawStyle.ELLIPSIS, col1Size);
graphics.drawText(col2, col2Start, y, DrawStyle.HDEFAULT, col2Size);
}
}
How to implement
You first create an array of Strings. This array can contain phone numbers or a combination of First Name, Last Name together with the phone attribute like (W), (H) for Work and Home numbers respectively. The last field will be the date or time. I check the time of the call to see if it is older than 24 hours. If it is, I use the date. The format for the time is “h:mma” and for date it is “MM/dd”. You can format these using SimpleDateFormat.
The full list of BlackBerry Attributes are represented as follows:
Home: (H)
Work:
Mobile: (M)
Pager: (P)
Fax: (F)
Other: (O)
Work2: (W2)
Home2: (H2)
The attribute constants can be found in the BlackBerryContact Interface. A typical array of mine would look like this:
String[] numList = {“Sheran Gunasekera (W) 09/09”, “Scott Mosier (M) 9:39a”,
“Kevin Smith (M)10:39p”, “+12120031337 10:04p};
Then, if I want to place this field on my MainScreen, I do this:
PhoneNumberListField numbers = new PhoneNumberListField();
numbers.setEmptyString(“*** No Calls Yet ***”, DrawStyle.HCENTER);
numbers.set(numList);
add(numbers);
This is what it looks like:
PhoneNumberListField