Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Contributor
prabhatshishodia
Posts: 33
Registered: ‎12-03-2011
My Device: 9800
My Carrier: Blackberry
Accepted Solution

how to format resource string

i have a screen on which i apply label field . in that label field i put string from resource.how to format that string.

Please use plain text.
Developer
pradeep_ch
Posts: 543
Registered: ‎12-23-2010
My Device: Z10
My Carrier: Airtel

Re: how to format resource string

Format means,

What you want excatly?

Please use plain text.
Contributor
prabhatshishodia
Posts: 33
Registered: ‎12-03-2011
My Device: 9800
My Carrier: Blackberry

Re: how to format resource string

I want to show resource String in to Screen .

format means :

 change some text  size and font and color of resource string.

Please use plain text.
Developer
pradeep_ch
Posts: 543
Registered: ‎12-23-2010
My Device: Z10
My Carrier: Airtel

Re: how to format resource string

Then create a LabelField which contains that string as label.
set font size and color to that label field.
now add that labelfield to screen.

This is the code snippet.

 

LabelField label = new LabelField(resourcstring)
{
protected void paint(Graphics graphics) 
			{
				graphics.setColor(Color.BLUE);//set your color here
				super.paint(graphics);
			}
};

//set your font like this

label.setFont(Font.getDefault().derive(Font.BOLD,12));//set your own font
//now add this field to screen

 

Thanks.

Please use plain text.
Contributor
prabhatshishodia
Posts: 33
Registered: ‎12-03-2011
My Device: 9800
My Carrier: Blackberry

Re: how to format resource string

Thanks sir it change complete resource string .i want to bold some part of string like name of developer.
Please use plain text.
Developer
pradeep_ch
Posts: 543
Registered: ‎12-23-2010
My Device: Z10
My Carrier: Airtel

Re: how to format resource string

Then try like this

 

1. String data = "Hello World!";  
   2.                int offsets[] ={0,5,data.length()};  
   3.                Font[] fonts = new Font[] { Font.getDefault(), Font.getDefault()};  
   4.                int bg[] = new int[] { Color.WHITE, Color.WHITE};  
   5.                int fg[] = new int[] { Color.BLACK, Color.RED};  
   6.                byte attributes[] = {0,1};  
    7.                add(new ActiveRichTextField(data, offsets, attributes, fonts, fg, bg, 0)); 

 

 

Thanks.

Please use plain text.
Contributor
prabhatshishodia
Posts: 33
Registered: ‎12-03-2011
My Device: 9800
My Carrier: Blackberry

Re: how to format resource string

Thanks alot sir code work fine show hello word text but font size color is not change because ActiveRichTextField has only one parameter.

public ActiveRichTextField(String text)

Creates a new ActiveRichTextField instance from provided string after scanning for active "links".

 

Parameters: text - String to scan and display. one more question how to add web link into screen with this text.

Please use plain text.
Developer
pradeep_ch
Posts: 543
Registered: ‎12-23-2010
My Device: Z10
My Carrier: Airtel

Re: how to format resource string

You need to change the font size

see this,

 

 String data = "Hello World!";  
         int offsets[] ={0,5,data.length()};  
         Font[] fonts = new Font[] { Font.getDefault().derive(Font.BOLD,20), Font.getDefault().derive(Font.BOLD,15)};  
         int bg[] = new int[] { Color.WHITE, Color.WHITE};  
         int fg[] = new int[] { Color.BLACK, Color.RED};  
         byte attributes[] = {0,1};  
         add(new ActiveRichTextField(data, offsets, attributes, fonts, fg, bg, 0));

 

And also you need to change foreground and background colors,

 

For hyperlink you have to write your own field,

This is the sample.

 

import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.system.Characters;


public class HrefField extends Field 
{

    private String content;
    private Font fieldFont;
    private int fieldWidth;
    private int fieldHeight;
    private boolean active = false;
    private int backgroundColour = 0xffffff;
    //private int textColour = 0x333333;
    private int textColour = Color.DARKBLUE;
    private int maskColour = 0xffffff;
    
    private int buttonId;
    private String buttonName;
    
    public HrefField(String content) 
    {
        super(Field.FOCUSABLE);
        this.content = content;
        fieldFont = Font.getDefault().derive(Font.UNDERLINED);
        fieldWidth = fieldFont.getAdvance(content)+2;
        fieldHeight = fieldFont.getHeight();
    }
    
    public void setColours(int backgroundColour, int textColour, int maskColour) 
    {
        this.backgroundColour = backgroundColour;
        this.textColour = textColour;
        this.maskColour = maskColour;
        invalidate();
    }
    
    public void setBackgroundColour(int backgroundColour)
    {
        this.backgroundColour = backgroundColour;
        invalidate();
    }
    
    public void setTextColour(int textColour)
    {
        this.textColour = textColour;
        invalidate();
    }
    
    public void setMaskColour(int maskColour)
    {
        this.maskColour = maskColour;
        invalidate();
    }
    
    public void setFont(Font fieldFont)
    {
        this.fieldFont = fieldFont;
    }
    
    public int getPreferredWidth() 
    {
        return fieldWidth;
    }
    
    public int getPreferredHeight() 
    {
        return fieldHeight;
    }
    
    protected void layout(int arg0, int arg1) 
    {
        setExtent(getPreferredWidth(), getPreferredHeight());
    }
    
    protected void paint(Graphics graphics) 
    {
    
        if (active) 
        {
            graphics.setColor(maskColour);
            graphics.fillRect(0, 0, fieldWidth, fieldHeight);
        } 
        else 
        {
            graphics.setColor(backgroundColour);
            graphics.fillRect(0, 0, fieldWidth, fieldHeight);
        }
        
        graphics.setColor(textColour);
        graphics.setFont(fieldFont);
        graphics.drawText(content, 1, 1);
       // graphics.drawLine(1, fieldHeight-2, fieldWidth-2, fieldHeight-2);
    }
        

    
    public void setButtonId(int buttonId)
    {
        this.buttonId = buttonId;
    }
    
    public void setButtonName(String buttonName)
    {
        this.buttonName = buttonName;
    }
    
    public int getButtonId()
    {
        return buttonId;
    }
    public String getButtonName()
    {
        return buttonName;
    }
    public String getLabel()
    {
    	return buttonName;
    }
    public boolean keyChar(char key, int status, int time)
    {
        if (key == Characters.ENTER)
        {
            fieldChangeNotify(0);
            return true;
        }
        
        return false;
    }
    
    
    protected void onFocus(int direction) 
    {
        active = true;
        invalidate();
    }
    
    protected void onUnfocus() 
    {
        active = false;
        invalidate();
    }
    
    public static Font defaultFont() 
    {
        return Font.getDefault();
    }
}

 And in this field navigation click connect browser by getting this field's link.

Please use plain text.
Contributor
prabhatshishodia
Posts: 33
Registered: ‎12-03-2011
My Device: 9800
My Carrier: Blackberry

Re: how to format resource string

in above code error on :Font.getDefault()-->quick suggestion is change to getDefaultFont()

and derive(Font.BOLD,20)-->BOLD cannot be resolved or is not a field

add(new ActiveRichTextField(data, offsets, attributes, fonts, fg, bg, 0));
above line also error-parameter change and change type of fonts to font[]

if i change as quick suggestion still have error
please please sir give me running code.again thanks sir
Please use plain text.
Developer
pradeep_ch
Posts: 543
Registered: ‎12-23-2010
My Device: Z10
My Carrier: Airtel

Re: how to format resource string

On which code, Hello world or href field code
Please use plain text.