BlackBerry Forums Support Community
              

Closed Thread
 
Thread Tools
Old 08-10-2007, 01:19 AM   #1
dangihitesh
Knows Where the Search Button Is
 
Join Date: Jul 2007
Model: 8800
PIN: N/A
Carrier: CLDC
Posts: 29
Lightbulb 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  
Old 08-10-2007, 07:42 AM   #2
Meenal
Thumbs Must Hurt
 
Join Date: Jan 2007
Location: India
Model: 8700g
Carrier: Airtel
Posts: 117
Default

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  
Old 08-10-2007, 07:58 AM   #3
dangihitesh
Knows Where the Search Button Is
 
Join Date: Jul 2007
Model: 8800
PIN: N/A
Carrier: CLDC
Posts: 29
Post 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  
Old 08-10-2007, 02:05 PM   #4
xxxdev
Thumbs Must Hurt
 
Join Date: Aug 2007
Model: 8100
PIN: N/A
Carrier: xxx
Posts: 117
Default

Quote:
Originally Posted by dangihitesh View Post
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  
Old 08-14-2007, 08:43 AM   #5
arifzaman
Thumbs Must Hurt
 
Join Date: Jun 2007
Location: Bangladesh
Model: 8800
PIN: N/A
Carrier: EDGE
Posts: 93
Default

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  
Old 11-22-2007, 04:36 AM   #6
genvej
Thumbs Must Hurt
 
Join Date: Jul 2007
Model: 8800
PIN: N/A
Carrier: TDC
Posts: 115
Default

Quote:
Originally Posted by arifzaman View Post
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  
Old 11-22-2007, 06:57 AM   #7
jfisher
CrackBerry Addict
 
Join Date: Jun 2005
Location: Manchester, UK
Model: BOLD
Carrier: t-mobile
Posts: 714
Default

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  
Old 11-22-2007, 07:28 AM   #8
jfisher
CrackBerry Addict
 
Join Date: Jun 2005
Location: Manchester, UK
Model: BOLD
Carrier: t-mobile
Posts: 714
Default

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  
Old 12-06-2007, 12:01 PM   #9
bwhelan
New Member
 
Join Date: Sep 2006
Model: 8800
Carrier: O2
Posts: 9
Default

Quote:
Originally Posted by jfisher View Post
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  
Old 12-06-2007, 03:31 PM   #10
predator
Knows Where the Search Button Is
 
Join Date: Nov 2007
Model: 8700c
PIN: N/A
Carrier: AT&T
Posts: 32
Default

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  
Old 12-07-2007, 09:38 AM   #11
bwhelan
New Member
 
Join Date: Sep 2006
Model: 8800
Carrier: O2
Posts: 9
Default

Quote:
Originally Posted by predator View Post
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  
Old 12-07-2007, 12:02 PM   #12
predator
Knows Where the Search Button Is
 
Join Date: Nov 2007
Model: 8700c
PIN: N/A
Carrier: AT&T
Posts: 32
Default

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  
Old 04-10-2008, 01:24 PM   #13
tc490225
Knows Where the Search Button Is
 
Join Date: Apr 2006
Model: 8300
Carrier: att
Posts: 42
Default 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  
Old 04-10-2008, 04:46 PM   #14
jfisher
CrackBerry Addict
 
Join Date: Jun 2005
Location: Manchester, UK
Model: BOLD
Carrier: t-mobile
Posts: 714
Default

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  
Old 04-10-2008, 06:17 PM   #15
richard.puckett
Talking BlackBerry Encyclopedia
 
richard.puckett's Avatar
 
Join Date: Oct 2007
Location: Seattle, WA
Model: 9020
PIN: N/A
Carrier: T-Mobile
Posts: 212
Default

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);
}
__________________
Do your homework and know how to ask a good question.
Offline  
Old 04-10-2008, 06:59 PM   #16
tc490225
Knows Where the Search Button Is
 
Join Date: Apr 2006
Model: 8300
Carrier: att
Posts: 42
Default 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  
Old 04-10-2008, 07:06 PM   #17
richard.puckett
Talking BlackBerry Encyclopedia
 
richard.puckett's Avatar
 
Join Date: Oct 2007
Location: Seattle, WA
Model: 9020
PIN: N/A
Carrier: T-Mobile
Posts: 212
Default

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!!!"));
	}
}
__________________
Do your homework and know how to ask a good question.
Offline  
Old 04-10-2008, 07:25 PM   #18
tc490225
Knows Where the Search Button Is
 
Join Date: Apr 2006
Model: 8300
Carrier: att
Posts: 42
Default 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  
Old 04-10-2008, 07:28 PM   #19
eZainny
Talking BlackBerry Encyclopedia
 
eZainny's Avatar
 
Join Date: Apr 2007
Location: Brisbane, Australia.
Model: 8300
PIN: N/A
Carrier: Optus
Posts: 340
Default

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  
Old 04-21-2008, 04:42 AM   #20
harikak
New Member
 
Join Date: Mar 2008
Model: 8800
PIN: N/A
Carrier: aaaa
Posts: 7
Default

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  
Closed Thread



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


APPLE 630-0895-B  VRAM 128K X 8 BOARD CARD VINTAGE picture

APPLE 630-0895-B VRAM 128K X 8 BOARD CARD VINTAGE

$74.77



128K RAM - APPLE - ORIGINAL APPLE prototype BOARD picture

128K RAM - APPLE - ORIGINAL APPLE prototype BOARD

$408.75



APPLE 820-0522-A 630-0895-B LITE VRAM 128K X 8 BOARD  picture

APPLE 820-0522-A 630-0895-B LITE VRAM 128K X 8 BOARD

$149.99







Copyright © 2004-2016 BlackBerryForums.com.
The names RIM © and BlackBerry © are registered Trademarks of BlackBerry Inc.