Visit Sponsor

Written by 5:58 pm Core Java

Making BlackBerry Applications Portable Across Multiple Devices

1. Introduction

Building portable BlackBerry Java applications that work consistently across multiple devices requires careful handling of UI layout, screen resolution, and font scaling. Unlike modern platforms, BlackBerry devices vary significantly in screen size, resolution, and aspect ratio—especially across legacy JDE 4.5+ devices, which are still widely used in enterprise environments.

This post explains how to handle UI portability in BlackBerry Java applications, with a focus on dynamic font resizing to support multiple screen widths such as Pearl, Curve, Torch, and Storm devices.

2. Key Challenge: Screen Resolution Differences

BlackBerry devices differ mainly in:

  • Screen width and height
  • DPI (dots per inch)
  • Font rendering behavior
  • Available UI real estate

Example Device Differences

DeviceScreen Width
PearlSmall
CurveMedium
TorchLarge
StormTouch + Wide

Because of this variation, hardcoded UI dimensions or fixed fonts often break layouts, especially for:

  • Labels
  • ChoiceFields
  • Custom UI components

3. Why Font Scaling Is Critical

Consider a business application containing:

  • Labels
  • ChoiceFields with dynamic values
  • Localized text (variable length)

If text exceeds screen width:

  • UI gets clipped
  • Fields overlap
  • Application looks broken

To avoid this, font size must be calculated dynamically based on screen width and content length.

4. Dynamic Font Calculation Strategy

The recommended approach is:

  1. Calculate available row width based on screen resolution
  2. Measure text width using Font.getAdvance()
  3. Reduce font height dynamically until content fits
  4. Apply the calculated font before rendering

This approach ensures:

  • UI consistency across devices
  • No clipped text
  • Better readability

5. Customizing ChoiceField Rendering

Below is a custom paint implementation for dynamically resizing text in a ChoiceField.

Custom paint() Method

protected void paint(Graphics graphics) {

    Resolutions r = new Resolutions();
    Font labelFont = r.getFont();
    Font font = r.getFont();

    int width = r.CustomRowWidth - 3;
    int calcWidth = 0;

    while (width < (font.getAdvance(label) + maxWidth(font))) {

        calcWidth = labelFont.getAdvance(label) + maxWidth(font);
        int height = font.getHeight() - 2;

        FontFamily fontFamily = Font.getDefault().getFontFamily();
        font = fontFamily.getFont(Font.PLAIN, height);
    }

    graphics.setFont(font);
    this.setFont(font);

    if (mFontColor != -1) {
        graphics.setColor(mFontColor);
    }

    super.paint(graphics);
}

6. Calculating Maximum Choice Width

The maxWidth() method determines the longest string among all available choices in the ChoiceField. This ensures font scaling accounts for worst-case content length.

public int maxWidth(Font font) {

    int max = -1;

    for (int i = 0; i < this.getSize(); i++) {
        String choice = (String) this.getChoice(i);
        int width = font.getAdvance(choice);

        if (width > max) {
            max = width;
        }
    }
    return max;
}

7. Best Practices for BlackBerry UI Portability

  • Avoid hardcoded font sizes
  • Calculate UI dimensions dynamically
  • Always consider smallest screen first (Pearl)
  • Test on multiple simulators and real devices
  • Handle localization text expansion

8. Applicability in Real Projects

This approach is particularly useful for:

  • Enterprise BlackBerry applications
  • Legacy app maintenance
  • Multi-device deployments
  • Apps targeting JDE 4.5 – 7.x

Understanding these UI techniques remains valuable when maintaining or upgrading existing BlackBerry Java applications.

Visited 6 times, 1 visit(s) today
Close