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
New Contributor
ohpleasehelp
Posts: 3
Registered: ‎03-24-2013
My Device: 8310 4.5
My Carrier: Rogers

Screen background color, field background color same. Field foreground text different.

Hello, some may sigh, some may roll their eyes, but I have been at this for way too long today.  I have now reached the point of insanity.  My question is, how can I have the main screen color and field background colors the same, while the field foreground is different? For example, black background for screen and fields, bright green foreground on text.

 

I found this article and I tried implementing it into the sample helloworld application shipped with the blackberry jde 4.5

 

http://supportforums.blackberry.com/t5/Java-Development/Change-the-background-color-of-a-screen/ta-p...

 

The modified code tries to make Mainscreen's background all black.  The richtextbox background is set to black, foreground white, but all i see is total black. Even if i change the richtext background to ... red for example, the screen is still all black, like its painted over all the fields.

 

Please help.

The intended goal is to have a screen with all black background, fields all black background, bright foreground text for the field ie: lime green.

/**
package com.rim.samples.device.helloworlddemo;

import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.RichTextField;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.Manager;
 
public class HelloWorldDemo extends UiApplication
{
    public static void main(String[] args)
    {
        HelloWorldDemo theApp = new HelloWorldDemo();
      
        theApp.enterEventDispatcher();
    }

    public HelloWorldDemo()
    {
        pushScreen(new HelloWorldScreen());
    }    
}

final class HelloWorldScreen extends MainScreen
{

public void paint(Graphics graphics)
    {
    graphics.setBackgroundColor(0x00FF0000); //red gets painted over entire screen, over fields
    graphics.clear();
    }    
    
    public HelloWorldScreen()
    {

        RichTextField myField = new RichTextField("White on black" ,Field.NON_FOCUSABLE)
        {
        public void paint(Graphics graphics)
        {
        graphics.setBackgroundColor(0x00000000);  //does not appear black. painted over with red.
        graphics.setColor(0x00FFFFFF);
        graphics.clear();
        super.paint(graphics);
        }
        };
        add(myField);

    }
    
    public void close()
    {
        Dialog.alert("Goodbye!");
        System.exit(0);
        super.close();
    }   
}

 

 

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

Re: Screen background color, field background color same. Field foreground text different.

I think you have missed the fact that the processing is hierarchical.  So the 'outer' Manager controls the painting of the contained Fields and Managers.  In this case the 'outer' manager is the screen.  So if you override paint() in Screen, and don't call super.paint() then none of the Fields will get painted.  You need to call super.paint(). 

 

In your description you talk about the entire screen being black, looking at your code, the entire Screen should be Red.  So perhaps this is not the only problem, but I suspect your description doesn't match this specific code.

 

If you are talking about setting a Background, can I suggest a few things:

 

a) Use the Background class and 'set' Background for Fields and managers.  You can't do this in OS 4.5, it becomes available in OS 4.6, but very few phones out there use OS 4.5 now. 

b) If you really want to override code, then you could in fact use paintBackground.  This just does the background and is invoked by standard paint() processing. 

c) Try to make sure your code does not have side effects.  so for example, I would code this:

        public void paint(Graphics graphics)
        {
        graphics.setBackgroundColor(0x00000000);  //does not appear black. painted over with red.
        graphics.setColor(0x00FFFFFF);
        graphics.clear();
        super.paint(graphics);
        }

 

as

 

        public void paint(Graphics graphics)
        {

int currBackColor = graphics.getbackgroundColor();

try {
        graphics.setBackgroundColor(0x00000000);  //does not appear black. painted over with red.
        graphics.clear();

} finally {

graphics.setBackgroundColor(currBackColor);

}

int currForeColor = graphics.getColor();

try {
        graphics.setColor(0x00FFFFFF);

        super.paint(graphics);
} finally {

graphics.setColor(currForeColor);

}

        }

 

Please use plain text.
New Contributor
ohpleasehelp
Posts: 3
Registered: ‎03-24-2013
My Device: 8310 4.5
My Carrier: Rogers

Re: Screen background color, field background color same. Field foreground text different.

I finally got the desired result. Adding a vertical field manager did the job.  Here's what it looks like on the blackberry.

Screenshot of results

Here is the code for anyone interested:

package com.rim.samples.device.helloworlddemo;

import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.RichTextField;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Font;

public class HelloWorldDemo extends UiApplication
{
    public static void main(String[] args)
    {
        HelloWorldDemo theApp = new HelloWorldDemo();
        theApp.enterEventDispatcher();
    }

    public HelloWorldDemo()
    {
        pushScreen(new HelloWorldScreen());
    }    
}

final class HelloWorldScreen extends MainScreen
{
    public HelloWorldScreen()
    {
        
        LabelField title = new LabelField("Hello World Demo", LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH | LabelField.FIELD_TOP)
        {
        public void paint(Graphics graphics)
        {
        graphics.setBackgroundColor(Color.BLACK);
        graphics.setColor(Color.HOTPINK);
        graphics.clear();
        super.paint(graphics);
        }
        };
        add(title);    
        
        LabelField title2 = new LabelField("Hello World Demo 2" , LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH | LabelField.FIELD_TOP)
        {
        public void paint(Graphics graphics)
        {
        graphics.setBackgroundColor(Color.BLACK);
        graphics.setColor(Color.LIMEGREEN);
        graphics.clear();
        super.paint(graphics);
        }
        };
        add(title2);    
        
        LabelField title3 = new LabelField("Hello World Demo 3" , LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH | LabelField.FIELD_TOP)
        {
        public void paint(Graphics graphics)
        {
        graphics.setBackgroundColor(Color.GRAY);
        graphics.setColor(Color.MAGENTA);
        graphics.clear();
        super.paint(graphics);
        }
        };
        title3.setText("This is some new text for hello world demo 3. I hope you like it");
        add(title3);    
        
        
        VerticalFieldManager VFM = new VerticalFieldManager(VerticalFieldManager.USE_ALL_HEIGHT | VerticalFieldManager.USE_ALL_WIDTH | VerticalFieldManager.NO_VERTICAL_SCROLLBAR | VerticalFieldManager.NO_VERTICAL_SCROLL )
        {
        public void paint(Graphics graphics)
        {
        graphics.setBackgroundColor(0x00000000);
        graphics.setColor(0x00FFFFFF);
        graphics.clear();
        super.paint(graphics);
        }
        };
        add(VFM);
        
    }
    
      
    public void close()
    {
        // Display a farewell message before closing application.
        Dialog.alert("Goodbye!");
        System.exit(0);
        
        super.close();
    }   
   
}

 It appears that the sequence in which the fields are placed into the code matters.  the verticalfieldmanager must be added last for the background to have a top to bottom effect.

Correct me if I am wrong but I think that without the labelfields having their style OR'ed to LabelField.FIELD_TOP, they appear at the bottom of the verticalfieldmanager "container"

 

I was having no luck achieving these results on just the main screen.  Only the text withthe cursor had the desired results. ie:white on black.  Only by using the verticalfieldmanager was I able to get the results.


Is there any benefit to not using  the verticalfieldmanager, and just the screen to create the top to bottom color?

 

Please use plain text.
New Contributor
ohpleasehelp
Posts: 3
Registered: ‎03-24-2013
My Device: 8310 4.5
My Carrier: Rogers

Re: Screen background color, field background color same. Field foreground text different.

So in other words, the verticalfieldmanager's background acts as the common background color. The other fields are held inside the field manager, and can use the same background color, or another.
Would be nice to just use text fields ontop of mainscreen with the same background color, but all i can get is a text fields ontop of verticalfieldmanager ontop of mainscreen.
Please use plain text.
Developer
peter_strange
Posts: 19,602
Registered: ‎07-14-2008
My Device: Not Specified

Re: Screen background color, field background color same. Field foreground text different.

MainScreen is a special construct and in fact has at least three Managers that you never see unless you look carefully.  These managers provide a top, middle and bottom area that you can use.  Look at the the setTitle, setBanner and setStatus methods that MainScreen has.

 

If you don't wan to use these, then FullScreen will probably give you want you want with less hassle.  Or you can spend some time working with MainScreen to understand its way of working.  This KB article is a great introduction to these intricacies:

http://supportforums.blackberry.com/t5/Java-Development/MainScreen-explained/ta-p/606644

Please use plain text.