|
|
|
08-10-2007, 01:19 AM
|
#1
|
Knows Where the Search Button Is
Join Date: Jul 2007
Model: 8800
PIN: N/A
Carrier: CLDC
Posts: 29
|
MainScreen Background Color
Please Login to Remove!
Hi,
I m trying to change the MainScreen's background color by overriding the paintBackground method...
The code looks like this:
protected void paintBackground(Graphics g){
System.out.println("paint background called");
super.paintBackground(g);
g.setBackgroundColor(Color.BLUE);
}
but this code will not printing my background.....what should be the problem with this.....
any help.....
Thanks in advance.....
|
Offline
|
|
08-10-2007, 07:42 AM
|
#2
|
Thumbs Must Hurt
Join Date: Jan 2007
Location: India
Model: 8700g
Carrier: Airtel
Posts: 117
|
I had tried doing the same thing .. but it did not work.
A possible turn around could be put a Field Manager on the mainscreen and then give the manager the background color.
__________________
Thanks
Meenal
|
Offline
|
|
08-10-2007, 07:58 AM
|
#3
|
Knows Where the Search Button Is
Join Date: Jul 2007
Model: 8800
PIN: N/A
Carrier: CLDC
Posts: 29
|
Background Image
Thanks....
Hi,
Actually i want to place a background image on to the mainscreen. and some
Fields on it....but there is no method to put image on to the MainScreen.
So, i m overriding a Screen's paintBackground() but it will not working...
The code look likes:
protected void paintBackground(Graphics g){
System.out.println("paint Background called");
int XWidth=g.getScreenWidth();
int YHeight=g.getScreenHeight();
g.clear();
g.drawBitmap(0,0,XWidth,YHeight,bitmap,0,0);
}
but, the above code not working properly....
Any idea???????
|
Offline
|
|
08-10-2007, 02:05 PM
|
#4
|
Thumbs Must Hurt
Join Date: Aug 2007
Model: 8100
PIN: N/A
Carrier: xxx
Posts: 117
|
Quote:
Originally Posted by dangihitesh
Thanks....
Hi,
Actually i want to place a background image on to the mainscreen. and some
Fields on it....but there is no method to put image on to the MainScreen.
So, i m overriding a Screen's paintBackground() but it will not working...
The code look likes:
protected void paintBackground(Graphics g){
System.out.println("paint Background called");
int XWidth=g.getScreenWidth();
int YHeight=g.getScreenHeight();
g.clear();
g.drawBitmap(0,0,XWidth,YHeight,bitmap,0,0);
}
but, the above code not working properly....
Any idea???????
|
Hello, I have an idea
To set background image or background color you should:
1. implement Manager class (can be VerticalFieldManager)
2. implement method paint () {
g.drawBitmap (.. your backgound image or
g.fillRect (...fill with your background color
super.paint (); // draw form components
}
3. add component not to screen but to your manager
MyManager manager;
manager.add ( new EditField ());
screen.add (manager);
4. override trackwhellroll and call invalidate, so you repaint your background.
I know it is hard way, but it works.
XXXDev
|
Offline
|
|
08-14-2007, 08:43 AM
|
#5
|
Thumbs Must Hurt
Join Date: Jun 2007
Location: Bangladesh
Model: 8800
PIN: N/A
Carrier: EDGE
Posts: 93
|
Hi all,
Use the following code To set the background color of a screen.
Code:
public void paint(Graphics graphics) {
// Sets the BackgroundColor
graphics.setBackgroundColor(Color.RED);
// Clears the entire graphic area to the current background
graphics.clear();
}
The Clear() method from the Graphics class must immediately follow the setBackgroundColor(int Color) method, as the entire graphic area must be cleared to allow other objects to be drawn.
Cheers,
ARIF
|
Offline
|
|
11-22-2007, 04:36 AM
|
#6
|
Thumbs Must Hurt
Join Date: Jul 2007
Model: 8800
PIN: N/A
Carrier: TDC
Posts: 115
|
Quote:
Originally Posted by arifzaman
Hi all,
Use the following code To set the background color of a screen.
Code:
public void paint(Graphics graphics) {
// Sets the BackgroundColor
graphics.setBackgroundColor(Color.RED);
// Clears the entire graphic area to the current background
graphics.clear();
}
The Clear() method from the Graphics class must immediately follow the setBackgroundColor(int Color) method, as the entire graphic area must be cleared to allow other objects to be drawn.
Cheers,
ARIF
|
if i try this i just get a red screen... all of my buttons textfields seems to be "under" the red color
|
Offline
|
|
11-22-2007, 06:57 AM
|
#7
|
CrackBerry Addict
Join Date: Jun 2005
Location: Manchester, UK
Model: BOLD
Carrier: t-mobile
Posts: 714
|
you need to pass the graphics object to the super class, add:
super.paint(graphics);
but MainScreen is build up of layout managers so you might find only part of the screen is drawn red. am looking into this at the moment, it's a mess. will report back if i succeed.
__________________
new job doesn't allow a public profile - please do not contact this user with questions, you will not get a response. good luck!
|
Offline
|
|
11-22-2007, 07:28 AM
|
#8
|
CrackBerry Addict
Join Date: Jun 2005
Location: Manchester, UK
Model: BOLD
Carrier: t-mobile
Posts: 714
|
okay - you can get the field manager from a mainscreen using:
getMainManager();
anyone know how you'd then override the paint method of this manager?
__________________
new job doesn't allow a public profile - please do not contact this user with questions, you will not get a response. good luck!
|
Offline
|
|
12-06-2007, 12:01 PM
|
#9
|
New Member
Join Date: Sep 2006
Model: 8800
Carrier: O2
Posts: 9
|
Quote:
Originally Posted by jfisher
getMainManager();
anyone know how you'd then override the paint method of this manager?
|
You can't override it except in a derived class. So you need to create your own class that extends one of the Manager classes.
The problem then is that you can not set a new MainManager for the MainScreen. That's annoying. The solution I came up with was to create a derived MainScreen class that creates it's own new Manager and adds Fields to that Manager instead of the default one already created by the MainScreen.
It's a bit tricky, but it works. Any fields you add are painted over the top of the background bitmap. Here's some sample code...
First the new Manager class:
Code:
class NewVerticalFieldManager extends VerticalFieldManager {
Bitmap bkgrndBmp = Bitmap.getBitmapResource("background.png");
NewVerticalFieldManager() {
super();
}
protected void paintBackground(Graphics g){
if(bkgrndBmp != null){
g.rop(Graphics.ROP_SRC_ALPHA_GLOBALALPHA,
(this.getWidth() / 2) - (bkgrndBmp .getWidth() /2),
(this.getHeight() / 2) - (bkgrndBmp .getHeight() /2),
bkgrndBmp .getWidth(),
bkgrndBmp .getHeight(),
bkgrndBmp ,0,0);
}
}
}
Now the new MainScreen class.
Code:
class NewMainScreen extends MainScreen {
NewVerticalFieldManager manager = new NewVerticalFieldManager();
NewMainScreen() {
super();
super.add(manager);
}
public void add(Field field){
manager.add(field);
}
}
|
Offline
|
|
12-06-2007, 03:31 PM
|
#10
|
Knows Where the Search Button Is
Join Date: Nov 2007
Model: 8700c
PIN: N/A
Carrier: AT&T
Posts: 32
|
Thanks for that code, bwhelan It works great except one small thing. For me, it's not coloring the whole screen. I'm setting a background color instead of using an image. Any ideas? I tried setting the manager to "USE_ALL_HEIGHT" but then the screen has a scrollbar, so i tried "NO_HORIZONTAL_SCROLL | NO_VERTICAL_SCROLL" but there's still a scrollbar there.
Code:
protected void paintBackground(Graphics graphics)
{
// Sets the BackgroundColor
graphics.setBackgroundColor(Color.MEDIUMBLUE);
// Clears the entire graphic area to the current background
graphics.clear();
//super.paint(graphics);
}
|
Offline
|
|
12-07-2007, 09:38 AM
|
#11
|
New Member
Join Date: Sep 2006
Model: 8800
Carrier: O2
Posts: 9
|
Quote:
Originally Posted by predator
Thanks for that code, bwhelan It works great except one small thing. For me, it's not coloring the whole screen. I'm setting a background color instead of using an image. Any ideas? I tried setting the manager to "USE_ALL_HEIGHT" but then the screen has a scrollbar, so i tried "NO_HORIZONTAL_SCROLL | NO_VERTICAL_SCROLL" but there's still a scrollbar there.
|
The paintBackground code will only colour the screen area taken up by the new VerticalFieldManager, so you need to ensure that it is taking up the entire screen space. It probably means overriding the getPreferredHeight() method of the manager and returning Display.getHeight(), or something like that. But that alone may not do it; you might have to ensure the fields within the manager have heights that add up to Display.getHeight().
Hope that points you in the right direction.
|
Offline
|
|
12-07-2007, 12:02 PM
|
#12
|
Knows Where the Search Button Is
Join Date: Nov 2007
Model: 8700c
PIN: N/A
Carrier: AT&T
Posts: 32
|
Thank you Doesn't getPreferredheight just return the preferred height rather than setting it? I already have the screen height saved in a variable called "screenHeight" and am now overriding the gerPreferredheight function as follows.
Code:
public int getPreferredHeight()
{
return screenHeight;
}
That doesn't seem to work, though. Complete code for the new manager is as follows.
Code:
class NewVerticalFieldManager extends VerticalFieldManager
{
NewVerticalFieldManager()
{
super();
}
public int getPreferredHeight()
{
return screenHeight;
}
protected void paintBackground(Graphics graphics)
{
// Sets the BackgroundColor
graphics.setBackgroundColor(Color.MEDIUMAQUAMARINE);
// Clears the entire graphic area to the current background
graphics.clear();
//super.paint(graphics);
}
}
I also tried to have gerPreferredHeight return 240 directly, which is the secreen size on blackberry 8700. Still nothing. What am i missing?
|
Offline
|
|
04-10-2008, 01:24 PM
|
#13
|
Knows Where the Search Button Is
Join Date: Apr 2006
Model: 8300
Carrier: att
Posts: 42
|
help..
Can someone take me by the hand.. Im not having much luck getting these code snippits working.. jfisher - I really find your www site to be helpful and you allways seem to have insiteful samples.. It would be great if there were an entire sample of how to set the background including all the imports etc. Sorry Im such a newb but I am trying !
|
Offline
|
|
04-10-2008, 04:46 PM
|
#14
|
CrackBerry Addict
Join Date: Jun 2005
Location: Manchester, UK
Model: BOLD
Carrier: t-mobile
Posts: 714
|
to be honest i never found a good solid way of setting the background on a MainScreen, after talking with rim i'm 90% sure they'll add it to the api in future but for now... - i did need to do this for a project recently and the method i used might seem a little messy, but the code was actually much more elegant than trying to override field managers paint methods.
what i did was to keep track of each field added to the screen and increment a counter which gave me the total height in pixels used by the fields, if this was less than the height of the screen i added a custom spacerfield ( Blackberry - SpacerField) to pad the rest. the app was built up with custom fields so i had control over the colours of each and worked dynamically with both pearl and 320x240 devices. but it feels like a hack so wouldn't want to blog about it, i'm sure other more experienced proto geeks could do it better, rim certainly did in their facebook app.
like everyone else, i'd love to see a nice working example including full source of setting the background colour of a MainScreen that doesn't cause more issues than it solves (it'd be a candidate for an article as this issues comes up every week it seems - i'm going to open up the dev lessons on northcubed to any other developers who want to contribute).
[edit - bwhelan's example looks elegant, i'll have to try it when i get a spare minute]
__________________
new job doesn't allow a public profile - please do not contact this user with questions, you will not get a response. good luck!
Last edited by jfisher; 04-10-2008 at 04:48 PM..
|
Offline
|
|
04-10-2008, 06:17 PM
|
#15
|
Talking BlackBerry Encyclopedia
Join Date: Oct 2007
Location: Seattle, WA
Model: 9020
PIN: N/A
Carrier: T-Mobile
Posts: 212
|
You need to set the extent in your custom manager, otherwise the default implementation will set it to the height of your one field. Add this to NewVerticalFieldManager:
Code:
protected void sublayout(int width, int height) {
super.sublayout(width, height);
setExtent(width, height);
}
|
Offline
|
|
04-10-2008, 06:59 PM
|
#16
|
Knows Where the Search Button Is
Join Date: Apr 2006
Model: 8300
Carrier: att
Posts: 42
|
more of a sample..
like I said Im a newby .. can you post more of a sample? I have no idea how to use the sample ou just posted.
|
Offline
|
|
04-10-2008, 07:06 PM
|
#17
|
Talking BlackBerry Encyclopedia
Join Date: Oct 2007
Location: Seattle, WA
Model: 9020
PIN: N/A
Carrier: T-Mobile
Posts: 212
|
Here ya go, slick.
Code:
class MyAquaMarineApp extends UiApplication {
public static void main(String[] args) {
MyAquaMarineApp app = new MyAquaMarineApp();
app.enterEventDispatcher();
}
MyAquaMarineApp() {
pushScreen(new MyAquaMarineScreen());
}
}
class NewVerticalFieldManager extends VerticalFieldManager {
NewVerticalFieldManager() {
super();
}
protected void paintBackground(Graphics g){
// Sets the BackgroundColor
g.setBackgroundColor(Color.MEDIUMAQUAMARINE);
// Clears the entire graphic area to the current background
g.clear();
}
protected void sublayout(int width, int height) {
super.sublayout(width, height);
setExtent(width, height);
}
}
class NewMainScreen extends MainScreen {
NewVerticalFieldManager manager = new NewVerticalFieldManager();
NewMainScreen() {
super();
super.add(manager);
}
public void add(Field field){
manager.add(field);
}
}
class MyAquaMarineScreen extends NewMainScreen {
public MyAquaMarineScreen() {
add(new RichTextField("I'm aqua marine!!!"));
}
}
|
Offline
|
|
04-10-2008, 07:25 PM
|
#18
|
Knows Where the Search Button Is
Join Date: Apr 2006
Model: 8300
Carrier: att
Posts: 42
|
Thanks Dick.
or do you prefer Richard? Seriously thank you very much.. Ill give it a try, but knowing my luck Im gonna hit a boatload of symbol errors.. so I will just put in the imports hopefully. Its been awhile since anyone called me slick, my best freind called me that . hah
|
Offline
|
|
04-10-2008, 07:28 PM
|
#19
|
Talking BlackBerry Encyclopedia
Join Date: Apr 2007
Location: Brisbane, Australia.
Model: 8300
PIN: N/A
Carrier: Optus
Posts: 340
|
The RIM KB has an entry relating to changing the background color of a screen:
How to - Change the background color of a screen
And also, how you can set an image to be the background for a screen:
How To - Use a background image in application screens
Unfortunately, due to the poor quality of the KB search if you went there and typed in the keywords "mainscreen background color" you probably still wouldn't have been able to find the above articles
__________________
View HTML email today with the Best Selling BlackBerry application: BBSmart Email Viewer
|
Offline
|
|
04-21-2008, 04:42 AM
|
#20
|
New Member
Join Date: Mar 2008
Model: 8800
PIN: N/A
Carrier: aaaa
Posts: 7
|
to be frank all these are working but not to thar extent .i am new to this field and i want to set the backgroungd color .i did same as u mension i.e taking the manager and override the paint and add the fields to that manager instead of that screen its working with one problem i.e. scrolling.please tell me the solution to remove that scroll bar.
LabelField labelField = new LabelField("This is a label");
AutoTextEditField deviceNumber = new AutoTextEditField ("Device No : ", "") ;
backgroundBitmap = Bitmap.getBitmapResource("thanks8.png");
MainScreen mainScreen = new MainScreen();
mainScreen.setTitle("Example") ;
HorizontalFieldManager defaultbgd = new HorizontalFieldManager(HorizontalFieldManager.USE_ ALL_HEIGHT | HorizontalFieldManager.USE_ALL_WIDTH){
public void paint(Graphics g)
{
g.setBackgroundColor(Color.LIGHTPINK);
g.clear();
super.paint(g);
}
} ;
VerticalFieldManager verticalFieldManager = new VerticalFieldManager();
verticalFieldManager.add(labelField) ;
verticalFieldManager.add(deviceNumber) ;
defaultbgd.add(verticalFieldManager) ;
mainScreen.add(defaultbgd) ;
pushScreen(mainScreen);
|
Offline
|
|
|
|