BlackBerry – Custom label field

By -



Problem: How do i define Custom label field in BlackBerry?
Solution:

customLabelField.java

package mypackage;

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.FontFamily;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Ui;

public class customLabelField extends Field
{
	private String label;

	public customLabelField(String label, int foregroundColor,
			int backgroundColor)
	{
		super();
		this.label = label;
		this.foregroundColor = foregroundColor;
		this.backgroundColor = backgroundColor;
	}

	private int foregroundColor;
	private int backgroundColor;

	protected void layout(int width, int height) {
		// TODO Auto-generated method stub
		setExtent(width, getFont().getHeight());
	}

	protected void paint(Graphics graphics) {
		// TODO Auto-generated method stub
		graphics.setBackgroundColor(backgroundColor);
		graphics.clear();
		graphics.setFont(setMyFont());
		graphics.setColor(foregroundColor);
		graphics.drawText(label, 0, 0);
	}

	// Get font for the label field
	public Font setMyFont()
	{
		 try {
			 FontFamily alphaSansFamily = FontFamily.forName("BBAlpha Serif");
			 Font appFont = alphaSansFamily.getFont(Font.PLAIN, 9, Ui.UNITS_pt);
			 return appFont;

		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}

After defining this class, you just need to add the custom label field inside your main screen:

 // adding custom field
  add(new customLabelField("This is the demo of creating custom field", Color.WHITE, Color.GREEN));

 


Paresh Mayani

Lazy android developer, exploring the horizon of android development since 7 years, currently working as a Technical Lead - Android at Simform Solutions, India. Former Application Architect at KarConnect. He is the Head/Organizer of Google Developers Group (GDG), Ahmedabad

Loading Facebook Comments ...
Loading Disqus Comments ...