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
colombus
Posts: 264
Registered: ‎06-04-2009
My Device: Not Specified

Displaying the top banner in BlackBerry

Hi

 

I am developing a blackberry application using RIM apis I am using the MainScreen and in my application i want to restrict the screen size so that the battery icon the network symbol etc is visible to the user. Is there any way i can restrict the fullscreen size of the Main screen ???

Please use plain text.
Developer
simon_hain
Posts: 15,872
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: Displaying the top banner in BlackBerry

no, this is not possible. you can implement your own header bar with the desired information. i posted a code sample from jfisher in some other thread, his side is offline but you can find it using archive.org

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Developer
colombus
Posts: 264
Registered: ‎06-04-2009
My Device: Not Specified

Re: Displaying the top banner in BlackBerry

hi

can u tell me where i can find the code sample

I couldnt find in the jonathan fisher site

If u have the link please send me

Please use plain text.
Developer
simon_hain
Posts: 15,872
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: Displaying the top banner in BlackBerry

http://web.archive.org/web/*/http://www.jonathanhfisher.co.uk/b2/

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Developer
rcmaniac25
Posts: 1,804
Registered: ‎04-28-2009
My Device: Z10 (STL100-4)-10.2.1.2174, Z10 (STL100-3)-10.3.0.700 Dev OS, Z30 (STA100-5)-10.3.0.700 Dev OS, PlayBook (16GB)-2.1.0.1917
My Carrier: Verizon

Re: Displaying the top banner in BlackBerry

I was looking to see if anyone has gotten a Banner that is avalible to anyone, came across his post. WebAchive was fine but very slow, also there is no real way to find the post. After some searching I found it and to save the trouble of anyone else looking for it I posted his source code file here:

import java.util.Timer;
import java.util.TimerTask;
import net.rim.device.api.i18n.SimpleDateFormat;
import net.rim.device.api.system.DeviceInfo;
import net.rim.device.api.system.RadioInfo;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.FontFamily;
import net.rim.device.api.ui.Graphics;

public class HeaderBar extends Field implements DrawStyle{
    private Font headerFont = SmallFont();
    private SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    private String timeString;
    private String dateString;
    private String title;
    private boolean showSignal;
    private boolean showBattery;
    private boolean showDate;
    private boolean showTime;
    private boolean showTitle;
    private int fieldWidth;
    private int fieldHeight;
    private int fontColour;
    private int backgroundColour;
    private int batteryBackground;
    private Timer headerTimer = new Timer();
    private TimerTask headerTask;
    
    public HeaderBar(String _title) {
        super(Field.NON_FOCUSABLE);
        title = _title;
        showSignal = true;
        showBattery = true;
        showDate = true;
        showTime = true;
        showTitle = true;
        fieldHeight = 12;
        fieldWidth = Graphics.getScreenWidth();
        fontColour = 0x888888;
        backgroundColour = 0;
        batteryBackground = 0x999999;
        
        headerTask = new TimerTask() {
            public void run() {
                invalidate();
            }
        };
        
        headerTimer.scheduleAtFixedRate(headerTask, 1000, 1000);
    }
    
    public void setTitle(String _title){
        title = _title;
        invalidate();
    }
    
    public void setFontColour(int _fontColour){
        fontColour = _fontColour;
        invalidate();
    }
    
    public void setBatteryBackground(int _batteryBackground){
        batteryBackground = _batteryBackground;
        invalidate();
    }
    
    public void setBackgroundColour(int _backgroundColour){
        backgroundColour = _backgroundColour;
        invalidate();
    }
    
    public void showSignal(boolean bool){
        showSignal = bool;
        invalidate();
    }
    
    public void showBattery(boolean bool){
        showBattery = bool;
        invalidate();
    }
    
    public void showDate(boolean bool){
        showDate = bool;
        invalidate();
    }
    
    public void showTime(boolean bool){
        showTime = bool;
        invalidate();
    }
    
    public void showTitle(boolean bool){
        showTitle = bool;
        invalidate();
    }
    
    protected void layout(int width, int height) {
        setExtent(getPreferredWidth(), getPreferredHeight());
    }
    
    public int getPreferredWidth() {
        return fieldWidth;
    }
    
    public int getPreferredHeight() {
        return fieldHeight;
    }
    
    protected void paint(Graphics graphics) {
        graphics.setFont(headerFont);
        int graphicsDiff = 0;
        
        if(backgroundColour != 0){
            graphics.setColor(backgroundColour);
            graphics.fillRect(0, 0, this.getPreferredWidth(), this.getPreferredHeight());
        }
               
        if(showSignal){
            graphicsDiff = graphicsDiff + 28;
            graphics.setColor(0x999999);
            //draw blank background
            graphics.fillRect(this.getPreferredWidth() - 26, 8, 6, 4);
            graphics.fillRect(this.getPreferredWidth() - 21, 6, 6, 6);
            graphics.fillRect(this.getPreferredWidth() - 16, 4, 6, 8);
            graphics.fillRect(this.getPreferredWidth() - 11, 2, 6, 10);
            graphics.fillRect(this.getPreferredWidth() -  6, 0, 6, 12);
            int signalLevel = RadioInfo.getSignalLevel();
            if(signalLevel > -77){
                //5 bands
                graphics.setColor(0xEFEFEF);
                graphics.fillRect(this.getPreferredWidth() - 25,9,4,2);
                graphics.fillRect(this.getPreferredWidth() - 20,7,4,4);
                graphics.fillRect(this.getPreferredWidth() - 15,5,4,6);
                graphics.fillRect(this.getPreferredWidth() - 10,3,4,8);
                graphics.fillRect(this.getPreferredWidth() - 5, 1, 4, 10);
            }else if(signalLevel > -86){
                //4 bands
                graphics.setColor(0xEFEFEF);
                graphics.fillRect(this.getPreferredWidth() - 25,9,4,2);
                graphics.fillRect(this.getPreferredWidth() - 20,7,4,4);
                graphics.fillRect(this.getPreferredWidth() - 15,5,4,6);
                graphics.fillRect(this.getPreferredWidth() - 10,3,4,8);
            }else if(signalLevel > -92){
                //3 bands
                graphics.setColor(0xEFEFEF);
                graphics.fillRect(this.getPreferredWidth() - 25,9,4,2);
                graphics.fillRect(this.getPreferredWidth() - 20,7,4,4);
                graphics.fillRect(this.getPreferredWidth() - 15,5,4,6);
            }else if(signalLevel > -101){
                //2 bands
                graphics.setColor(0xEFEFEF);
                graphics.fillRect(this.getPreferredWidth() - 25,9,4,2);
                graphics.fillRect(this.getPreferredWidth() - 20,7,4,4);
            }else if(signalLevel > -120){
                //1 band
                graphics.setColor(0xEFEFEF);
                graphics.fillRect(this.getPreferredWidth() - 25,9,4,2);
            }
        }
        if(showBattery){
            
            int batteryLevel = DeviceInfo.getBatteryLevel();
            graphics.setColor(batteryBackground);
            graphics.fillRect(this.getPreferredWidth()-23-graphicsDiff,2,20,8);
            graphics.fillRect(this.getPreferredWidth()-3-graphicsDiff,4,1,4);
            if(batteryLevel > 75){
                graphics.setColor(0x28f300);
            }else if(batteryLevel > 50){
                graphics.setColor(0x91dc00);
            }else if(batteryLevel > 25){
                graphics.setColor(0xefec00);
            }else{
                graphics.setColor(0xff2200);
            }
            double powerLong = ((18.00/100)*batteryLevel);
            int power = (int)powerLong;
            graphics.fillRect(this.getPreferredWidth()-22-graphicsDiff,3,power,6);
            graphicsDiff = graphicsDiff + 24;
        }
        
        graphics.setColor(fontColour);
        
        if(showTime){
            timeString = " " + timeFormat.formatLocal(System.currentTimeMillis()) + " ";
        }else{
            timeString = "";
        }
        
        
        if(showDate){
            dateString = " " + dateFormat.formatLocal(System.currentTimeMillis()) + " ";
        }else{
            dateString = "";
        }
        
        graphics.drawText(dateString + timeString, this.getPreferredWidth()-headerFont.getAdvance(dateString + timeString)-graphicsDiff, 1);
        
        if(showTitle){
            int limit = 0;
            if(showSignal)limit = limit + 28;
            if(showBattery)limit = limit + 25;
            if(showTime)limit = limit + headerFont.getAdvance(timeString);
            if(showDate)limit = limit + headerFont.getAdvance(dateString);
            if(headerFont.getAdvance(title) > this.getPreferredWidth() - limit){
                int elippsy = headerFont.getAdvance("...");
                int availableWidth = this.getPreferredWidth() - limit - elippsy;
                String _txt = "";
                String shorterTitle = "";
                for(int i = 1 ; i < title.length(); i++){
                    _txt = title.substring(0, i);
                    if(headerFont.getAdvance(_txt) < availableWidth){
                        shorterTitle = _txt;
                    }
                }
                title = shorterTitle + "...";
            }
            graphics.drawText(title, 1, 0);
        }
    }
    
    public static Font SmallFont(){
        try {
            FontFamily theFam = FontFamily.forName("SYSTEM");
            return theFam.getFont(net.rim.device.api.ui.Font.PLAIN, 10);
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
        return null;
    }
}

 Thank you Mr. Fisher

---Spends time in #blackberrydev on freenode (IRC)----
Three simple rules:
1. Please use the search bar before making new posts.
2. "Like" posts that you find helpful.
3. If a solution has been found for your post, mark it as solved.
--I code too much. Well, too bad.
Please use plain text.
Developer
peter_strange
Posts: 19,599
Registered: ‎07-14-2008
My Device: Not Specified

Re: Displaying the top banner in BlackBerry

I seem to remember there being a problem with this header.  Every header starts up a new timer and Timer task, but only to update the clock, which is displayed in minutes.  I think each Timer = a new Thread, so if you use this header on more than about 10 screens, you will run out of Threads.  Instead I suggest that you  have each Header implement RealTimeClockListener and use clockUpdated() to invalidate() and so update the time. 

Please use plain text.
Developer
rcmaniac25
Posts: 1,804
Registered: ‎04-28-2009
My Device: Z10 (STL100-4)-10.2.1.2174, Z10 (STL100-3)-10.3.0.700 Dev OS, Z30 (STA100-5)-10.3.0.700 Dev OS, PlayBook (16GB)-2.1.0.1917
My Carrier: Verizon

Re: Displaying the top banner in BlackBerry

[ Edited ]

I see many thing in it that I want to change but I was just posting the original code.

 

I did a little test and ran pushed 70 screens (because I can) with this on there and it the console does say "too many threads" and "unable to run <application name:thread> but it never throws an exception, it just makes the (in my case) Simulator slow to a halt and do a lot of cleanup when I restart it. It was just to see what would happen but it never throws an exception, just freezes the BlackBerry.

 

I wish RIM would expose the built-in Banner class (assuming they have one). Then theme, notifications, etc. can be shown without needing to do any complex coding (has anyone even managed to get the icon of a 3rd party notification?)

---Spends time in #blackberrydev on freenode (IRC)----
Three simple rules:
1. Please use the search bar before making new posts.
2. "Like" posts that you find helpful.
3. If a solution has been found for your post, mark it as solved.
--I code too much. Well, too bad.
Please use plain text.
Developer
dnepr
Posts: 723
Registered: ‎03-12-2009
My Device: Playbook

Re: Displaying the top banner in BlackBerry

Kill the thread on obscured and restart it when it is shown.

Please use plain text.
Developer
rcmaniac25
Posts: 1,804
Registered: ‎04-28-2009
My Device: Z10 (STL100-4)-10.2.1.2174, Z10 (STL100-3)-10.3.0.700 Dev OS, Z30 (STA100-5)-10.3.0.700 Dev OS, PlayBook (16GB)-2.1.0.1917
My Carrier: Verizon

Re: Displaying the top banner in BlackBerry

You don't have control of the Thread. You can kill the Timer and restart it when visible again but you would have to keep a timer going to so that the new Timer continues to update as if it never went away.

---Spends time in #blackberrydev on freenode (IRC)----
Three simple rules:
1. Please use the search bar before making new posts.
2. "Like" posts that you find helpful.
3. If a solution has been found for your post, mark it as solved.
--I code too much. Well, too bad.
Please use plain text.
Developer
dnepr
Posts: 723
Registered: ‎03-12-2009
My Device: Playbook

Re: Displaying the top banner in BlackBerry

[ Edited ]

What exactly do you mean?

 

Why would you need to have any thread or timer running while the header is not visible? (I'm assuming we're talking about the implementation in code above).

 

Upon visibility state change just enable, disable the timer, thread, whatever you're using.

 

 

On a seprate note,  I'm wondering what are the differences between implementing Runnable and using TimerTasks for this implementation.   Both are taking up a thread, whats the benefit of using TimerTask?

Please use plain text.