Creating a BlackBerry custom field

Posted October 10th, 2009 by Nazmul

We believe in the power of ONE, at ScreamingToaster. We believe in innovation - ONE experience can change a person's life. We believe in partnerships - ONE endeavor can establish lasting trust & mutual success. We believe in education - ONE training course can empower a developer for life. We believe in giving back - ONE open-source project can inspire future entrepreneur. ONE person can become the leader that starts a revolution. ONE company can change the world and make it a better place - join us.

Introduction

This tutorial will show you how to create a very simple custom field (component) using the RIM API. If you’re familiar with Swing, then this code will not be a surprise to you. There are some similarities between AWT/Swing and RIM UI API.

Example

I’m going to create a custom label field that is different from the LabelField in that you can set a foreground color on it. Here’s the code to do this.

public class MyLabelField extends LabelField {

  private String text;
  private int fgColor;

  public MyLabelField(String text, int fgColor){
    this.text = text;
    this.fgColor = fgColor;
  }

  // @Override
  protected void layout(int w, int h){
    setExtent(w, Font.getDefault().getHeight());
  }

  // @Override
  protected void paint(Graphics g){
    g.setColor(fgColor);
    g.drawText(text, 0, 0);
  }

}

This is what’s going on in the code:

  1. The constructor of MyLabelField simply accepts a label text and foreground color. Note that this class extends LabelField.
  2. The paint() method has been overridden (just like in AWT or Swing) to provide custom painting code for this class. Here I just set the foreground color and write the string out with it.
  3. The layout() method has been overridden. You have to do something similar in Swing/AWT but the syntax here is totally different. The layout method is called by a layout manager to size and position this field on the screen. So all this method does is it returns its preferred dimensions, by calling setExtent(). The layout() method is called first, before getExtent(), by a layout manager, so it’s important that you set the extent in this method.

Feedback and comments

To share your thoughts on this tutorial click here.

We do not just build mobile “apps”, we build mobile experiences

Let us build you mobile experiences for Android, BlackBerry, and iPhone. We can also build you cloud-connected experiences that span Mobile (Android, BlackBerry, iPhone), Web (Safari, Firefox, Internet Explorer) and Desktop (PC, Mac, Linux) for a truly connected real-time experience. See our work in action: download RainOrShine for BlackBerry and CityRyde for Android.

  • We provide full service, full lifecycle consulting services (planning, graphic design, architecture, marketing, support, and implementation).
  • We provide Architecture guidance before implementation; marketing strategy after implementation. Click here to learn more.

Zen Application Framework

All our applications are built using our Zen Application Framework, which helps you avoid the common pitfalls around mobile development. Zen drastically reduces development time & cost for mobile, web, and desktop experiences. Click here to learn more.

Our training services make You self-sufficient & sustainable

Want to learn from the best? We offer Android and BlackBerry developer training programs. Whether you are just starting out, or want advanced training, we have courses for different skill levels & technology requirements. Contact us to learn more, and sign up.

Our values: Open-source. Democratization of knowledge. To lead, never follow

We are leaders in mobile technology. We have done and seen it all. We do not follow trends set by others & we do not make the mistakes made by others. We set the trends for mobile because we are mobile visionaries.


Comments are closed.