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
Developer
technolgia
Posts: 96
Registered: ‎06-11-2009
My Device: Not Specified
Accepted Solution

Background color of Custom Field

Hello,

I am creating a custom field a Bar Graph which is as shown in the attached screen.

Pie.png

 

I would want to change the background color of the field to some other color. I tried overridding the paintBackground method of the field as  shown below:

 

protected void paintBackground(Graphics arg0) {
		arg0.setColor(Color.DARKGRAY);
		super.paintBackground(arg0);
	}

 but there seems to be no effect. Could someone kindly help me with this. Thanks in advance.

 

Regards,

S.A.Norton Stanley

Please use plain text.
Developer
rabiray
Posts: 141
Registered: ‎07-13-2011
My Device: BB Bold
My Carrier: Vodafone

Re: Background color of Custom Field

Try this..

 

Create a class which extends Field and implements DrawStyle

 

and override its paint method... to set the back ground color

 

Draw the shape you want with graphics object and either fill the area or set the background color.

 

Thx

Rabi Ray

 

Please use plain text.
Developer
technolgia
Posts: 96
Registered: ‎06-11-2009
My Device: Not Specified

Re: Background color of Custom Field

Hi Rabi,

 

Thank you. setBackground worked for me and it does not require the DrawStyle to be implemented.

 

Regards,

S.A.Norton Stanley

Please use plain text.
Developer
peter_strange
Posts: 19,602
Registered: ‎07-14-2008
My Device: Not Specified

Re: Background color of Custom Field

Personally I would not recommend overriding paint to do this.

 

You can achieve this in paintBackground.  You need to set the Background color rather than the color and then do a clear.  I believe this to be the most efficient approach.

 

Alternatively, if you are developing for OS 4.6 and above, use the decor class to set a solid color as the background.

 

I would recommend either of these approaches in preference to overriding paint().

 

For the record, here is how to do it in paintBackground()

 

protected void paintBackground(Graphics g) {

int currBackgroundColor = g.getBackgroundColor();

try {

g.setBackgroundColor(Color.DARKGRAY);
g.clear();

} finally {

g.setBackgroundColor(currBackgroundColor);

}

}

Please use plain text.
Developer
technolgia
Posts: 96
Registered: ‎06-11-2009
My Device: Not Specified

Re: Background color of Custom Field

Hello Peter,

 

Thank you. Works like a charm :Clap: . If i would have to set a gradient color to the background is that possible. I am aware that the following works:

 

	setBackground(BackgroundFactory.createLinearGradientBackground(
				Color.BLACK, Color.BLUE, Color.BLACK, Color.BLUE));

 Kindly advice. Thanks again.

 

Regards,

S.A.Norton Stanley

Please use plain text.
Developer
peter_strange
Posts: 19,602
Registered: ‎07-14-2008
My Device: Not Specified

Re: Background color of Custom Field

I think I have some code that uses the graphics commands to draw a gradient directly, which you could use in patinBackground(), if you want me to dig it out. 

 

But I think that code is based loosely on this code:

http://www.deepgraysea.com/bbroundedrect.htm

 

And then there is this code:

http://supportforums.blackberry.com/t5/Java-Development/Gradient-Poor-Quality/m-p/778325#M143271

 

However for my gradients now, I use the Background class, since we have, in all but name, stopped supporting OS's pre OS 4.6. 

Please use plain text.
Developer
technolgia
Posts: 96
Registered: ‎06-11-2009
My Device: Not Specified

Re: Background color of Custom Field

Hello Peter,

 

Thank you for the pointers will try on similar lines. Would be great if you could share your code too. :Flowers:

 

Regards,

S.A.Norton Stanley

Please use plain text.