3

How do we set the background and font colors in a RichTextField? I tried to override the paint() method in addition to what has been described here, but when I scroll down in, the background gets erased or reset to a white background

Community
  • 1
  • 1
Prabhu R
  • 13,836
  • 21
  • 78
  • 112

2 Answers2

4

In RIM 4.6 and greater you can use Background:

class ExRichTextField extends RichTextField {

    int mTextColor;

    public ExRichTextField(String text, int bgColor, int textColor) {
        super(text);
        mTextColor = textColor;
        Background background = BackgroundFactory
                .createSolidBackground(bgColor);
        setBackground(background);
    }

    protected void paint(Graphics graphics) {
        graphics.setColor(mTextColor);
        super.paint(graphics);
    }
}

For RIM 4.5 and lower use paint event to draw background youreself:

class ExRichTextField extends RichTextField {

    int mTextColor;
    int mBgColor;

    public ExRichTextField(String text, int bgColor, int textColor) {
        super(text);
        mTextColor = textColor;
        mBgColor = bgColor;
    }

    protected void paint(Graphics graphics) {
        graphics.clear();
        graphics.setColor(mBgColor);
        graphics.fillRect(0, 0, getWidth(), getHeight());
        graphics.setColor(mTextColor);
        super.paint(graphics);
    }
}
Maksym Gontar
  • 22,765
  • 10
  • 78
  • 114
  • Thanks, works like a breeze! However, when I add it to the VerticalFieldManager, i don't see the scrollbars, I have set the Manager's style to VERTICAL_SCROLL } VERTICAL_SCROLLBAR. Any reason why it does not appear? – Prabhu R Sep 16 '09 at 10:18
  • Can't resolve this.. Seems like scrollbar not appears when preferred size of manager is set. – Maksym Gontar Sep 17 '09 at 09:46
  • One more problem that I encounter was when I add the ExRichTextField to the VerticalFieldManager after the BitmapField, the whole screen scrolls instead of the ExRichTextField alone scroll, which is the expected behaviour. Would adding two managers to the class resolve the problem? – Prabhu R Sep 19 '09 at 20:32
  • Hi Ram! seems like no workaround to display standard scrollbar, so I just draw it custom. see update http://stackoverflow.com/questions/1426081/creating-custom-layouts-in-blackberry/1428106#1428106 – Maksym Gontar Sep 21 '09 at 10:20
0
RichTextField mes_=new RichTextField("texto de ejemplo",Field.NON_FOCUSABLE){
    protected void paint(Graphics g){ 
        g.setColor(0x00e52f64);
        super.paint(g);
    }
};
mes_.setBackground(BackgroundFactory.createSolidBackground(0xFFFADDDA));

The method incrustaded in the declaration its for changing the color of the font. The method called after created its for changing the background to a solid color.

Lucifer
  • 29,392
  • 25
  • 90
  • 143
jmlv21104
  • 89
  • 12