Friday, April 3, 2009

How To : Change the text color of a field

How To You can change the color of text in a field for applications that are designed to run on a color BlackBerry device. When you define a field, you can choose a color that will not change during the life of the field. To allow a field's color to change dynamically, extend one of the existing fields, or create your own field.

Below are samples that utilize the RichTextField and change the color of the text in the field. The color in Example 1 below will stay the same for the life of the field. The code in Example 2 below allows the color to be programmatically set, or set to a random value.

Example 1

//RichTextField that displays text in the specified color.
RichTextField fontText = new RichTextField("This text will be green.")
{
    public void paint(Graphics graphics)
    {
        graphics.setColor(0x00008800);
        super.paint(graphics);
    }
};

Example 2

/**
* Custom Field - FontField
*
*/
import net.rim.device.api.ui.*;
import net.rim.device.api.system.*;
import net.rim.device.api.ui.component.*;
import java.util.*;
public class FontField extends RichTextField
{
    //Set the initial text color to black.
    private int color = 0x00000000;
    //Initialize the random number generator.
    private Random randColor = new Random();
    //Only one RichTextField contstructor in this sample for brevity.
    public FontField(String text)
    {
        super(text);
    }
    //Set the color of the text in this field.
    public void setColor(int newColor)
    {
        color = newColor;
    }
    //Get the current text color.
    public int getColor()
    {
        return color;
    }
    //Randomly generate a new text color.
    public void randomizeColor()
    {
        int r = Math.abs(randColor.nextInt()) % 255;
        int g = Math.abs(randColor.nextInt()) % 255;
        int b = Math.abs(randColor.nextInt()) % 255;
        color = (255<<24) | (r<<16) | (g<<8) | b;
    }
    //Set the foreground and then call paint method of the parent class.
    public void paint(Graphics graphics)
    {
        graphics.setColor(color);
        super.paint(graphics);
    }
}

3 comments:

  1. hello Saytode!

    good work..good examples. Is it possible to generate event on LabelField???

    I mean i have a label in body and would like to generate even on it. I have tried with FieldChangeListener, but it is not working?

    FieldChangeListner is working with Button not with LabelFiled.

    Could you please tell me what i should do to make it work?

    please reply to my message on "ashwinkvairu@gmail.com"

    Regards,
    Ashwin.

    ReplyDelete
  2. Hello Ashwin,
    Yes, you can have focus based events of LabelField by implementing FocusChangeListener.
    Hence you will have its derived method

    protected void focusChanged(Field field, int eventType){}

    that automatically calls every time when
    any field(which has set FocusListener to it) gains focus or lost focus.

    For example :
    inside class that extends MainScreen and implements FocusChangeListener

    LabelField sampleLF = new LabelField("Title here",Field.FOCUSABLE);
    sampleLF.setFocusListener(this);
    ...
    ...

    protected void focusChanged(Field field, int eventType){
    if((eventType == FocusChangeListener.FOCUS_GAINED)
    && (field.getOriginal() == sampleLF)) {
    System.out.println("sampleLF currently got focused...");
    } else if ((eventType == FocusChangeListener.FOCUS_LOST)
    && (field.getOriginal() == sampleLF)) {
    System.out.println("sampleLF lost its focus...");
    }

    }

    ReplyDelete
  3. Math.abs(randColor.nextInt()) % 255 should be written as randColor.nextInt(255)

    However, don't you want 256?

    ReplyDelete

Place your comments here...