views:

2836

answers:

3

Hi there,

How to change color of specific text only in textbox for Blackberry applications ?

Please help.

+3  A: 

Have you checked out ActiveRichTextField? I've only used RichTextField myself but it looks like ActiveRichTextField lets you specify colors for the text regions in addition to fonts & formatting. It takes a little bit of set up to get these fields going but check out the javadoc for RichTextField, it gives a pretty good explanation.

Here's some sample code:

int offsets[] = new int[]{0, 5, 11};
Font[] fonts = new Font[]{Font.getDefault(), Font.getDefault()};
int bg[] = new int[]{Color.WHITE, Color.WHITE};
int fg[] = new int[]{Color.BLACK, Color.GREEN};
byte attributes[] = new byte[]{0, 1};

add(new ActiveRichTextField("Hello world", offsets, attributes, fonts, fg, bg, 0));
Adam B
I tried with ActiveRichTextField.Its giving me perfect thing what I wanted.But the above text field doesnt allow me editing of text...I tried setting property of this ActiveRichTextField as EDITABLE,but it didnt work...
imMobile
Jazz, you want the field to be editable by the user? or by you? and you want some of this user-entered text to be red??
Pat
Pat, I want the textbox editable by the user.
imMobile
+1  A: 

As Fostah said in his comment, the TextBox class isn't very flexible in terms of changing the look and feel. Even the look and feel of many of the "Field" classes are pretty hard to change, too.

However, if you have some flexibility and can use a Field such as EditField (or variant thereof) instead of TextBox, then it should be just case of overriding the paint method. Something like:

protected void paint(Graphics graphics) {
    graphics.setColor(Color.RED);
    super.paint(graphics);
}
Marc Novakowski
+4  A: 

Unfortunately the answer is roll your own - there's no editable text field control that lets you change the font color at all. @Mark Novakowski's answer is a standard way of working around that, but will change all the text to one color.

If you're not worried about supporting the Pearl-style SureType keyboard, then you can override keyChar on ActiveRichTextField and manually add characters to the text in ActiveRichTextField.

For SureType fields, the answer is even worse - you don't have low level access to the SureType APIs (for correctly dealing with the predictive text popup) for non-qwerty phones (the Pearl series) so you have to resort to some real trickery to get a fully custom text field to function correctly on those devices. It may come down to having to have multiple EditFields arranged in a manager, with e.g. one EditField overridden to show read (as in Marks' answer). The trick there will be dynamically creating and adding text fields as necessary.

Yes, sometimes the RIM API makes easy things easy, and hard things nearly impossible.

Anthony Rizk