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
Posts: 27
Registered: ‎12-23-2008
My Device: 10000

How to know titleBar Height

Hello guys can You help me, I will want to know the height of a titlebar in any device and models with Rim API it is posible ?

My class extends of MainScreen and I use setTitle("") function.

Developer
Posts: 27
Registered: ‎12-23-2008
My Device: 10000

Re: How to know titleBar Height

This Height is the same for all phones and models, How many pixels it is?
BlackBerry Development Advisor
Posts: 15,017
Registered: ‎07-09-2008
My Device: BlackBerry Passport

Re: How to know titleBar Height

This isn't a constant value and can vary based on the content in the title bar and a user's current font.  If you want to know the height of the text you are placing within the field you can use the Font.getHeight method to get the height of the font.
Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
New Developer
Posts: 1
Registered: ‎02-24-2009
My Device: Not Specified

Re: How to know titleBar Height

Hi all. I'd like to raise this thread since i 've met the same problem. Getting font height is not enough because there is a separator between Title area and scrollable section. Does anybody know how to get separator height?

 

TIA.

Developer
Posts: 19,612
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to know titleBar Height

Actually it can be more complicated than that because you can put multiple Fields in the Title area, not just one line.

 

Treat the following with caution, I have NOT tried this.

 

The Title area is controlled by a 'hidden' manager in the MainScreen class.  You can find this manager by finding the first Field used by the Manager of MainScreen - this is not the delegate manager which is responsible for the Fields in the scrollable part of the MainScreen.  You should be able to get the the height of this Manager using getHeight().  So this is probably the most reliable way of determining the height of the Title.

 

But I'm not sure when this manager is laid out, so you may not be able to determine this during the Screens construction.  I would do this checking while laying out my Fields.

 

Hope this helps your understanding and hopefulyy your coding.

Developer
Posts: 330
Registered: ‎02-25-2009
My Device: Not Specified

Re: How to know titleBar Height

you can create your own title for your application

here is some help with code :

 

write the following code in the constructor of User Interface class: ---

final static public int TITLE_HEIGHT=25;

private final Bitmap titleImage=Bitmap.getBitmapResource("title.PNG");

private TitleBar title;

title=new TitleBar();
setTitle(title); 
title.setTitle("Application Title",titleImage);

 

 

this is inner class, write it outside of constructor, but in the user Interface class:--

 

class TitleBar extends HorizontalFieldManager{
        private final Bitmap titleImage=Bitmap.getBitmapResource("title.PNG");
        Bitmap logo;

       LabelField label=new LabelField("Application Title");
       
        public TitleBar(){
            super();
            add(label);          
        }
       
        protected void sublayout(int maxWidth,int maxHeight){
            super.sublayout(maxWidth,maxHeight);
            setExtent(Display.getWidth(), TITLE_HEIGHT);
        }
       
        public void paint(Graphics g) {
            g.setFont(fnt);
            g.drawBitmap(0, 0, g.getScreenWidth(), g.getScreenHeight(), titleImage, 0, 0);
            if (logo != null) {
                g.drawBitmap(label.getWidth()+10, 0, g.getScreenWidth(), g.getScreenHeight(), logo, 0, 0);
            }
           
            g.setColor(Color.BLACK);
            g.setFont(fnt);
            super.paint(g);
        }
       
        public void setTitle(String titleText, Bitmap titleLogo) {
            label.setText(titleText);
            logo = titleLogo;
        }       
    }//class TitleBar end 

 

try it out

 

Developer
Posts: 16,575
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: How to know titleBar Height

a field within the title bar can access its outlying manager using getManager.
anyhow, if you debug into mainscreen you can see that it is just a verticalfieldmanager with a spacer.
instead of writing complex logic just use your own vfm instead of settitle.
----------------------------------------------------------
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
New Developer
Posts: 35
Registered: ‎07-11-2009
My Device: Not Specified

Re: How to know titleBar Height

This is a dirty trick. It works but it will make the screen go blank for some milliseconds, so you should use it once at your app start.
I successfully tested it on JDE 4.0, 4.2, 4.6 and 4.7
 
Example use:
 
DummyMainScreen dummy = new DummyMainScreen("title");
System.out.println("The title height of a MainScreen is: " + dummy.getTitleHeight());
 
Feel free to use. Code follows.
 

import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.component.NullField;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.UiApplication;

public class DummyMainScreen extends MainScreen {
    
    private int _titleHeight;
    
    public DummyMainScreen() {
        setTitle(new NullField());
    }
    
    public DummyMainScreen(String title) {
        setTitle(title);
    }
    
    public DummyMainScreen(Field field) {
        setTitle(field);
    }

    public int getTitleHeight() {
        triggerPaint();
        return _titleHeight;
    }
    
    private void triggerPaint() {
        UiApplication app = UiApplication.getUiApplication();
        if (app.isEventThread()) {
            app.pushScreen(this);
            app.repaint();
            app.popScreen(this);
        }
    }
    
    protected void paint(Graphics graphics) {
        Manager manager = getMainManager().getManager();
        if (manager.getFieldCount() > 0) {
            _titleHeight = manager.getField(0).getHeight();
        }
    }
}

Contributor
Posts: 41
Registered: ‎07-28-2010
My Device: Not Specified

Re: How to know titleBar Height

[ Edited ]

Awesome! It's works on my Storm 2, OS 5.0.0

Developer
Posts: 2,268
Registered: ‎07-08-2009
My Device: various

Re: How to know titleBar Height

All those shenanigans, while being cute, don't really help you in the actual application (not to mention being unreliable - are you sure title manager is Field #0 in MainScreen's delegate manager? What about banner? What about future OS versions which might have a different MainScreen implementation?).

 

However, I'm absolutely sure the height of the title manager (as well as banner and status) is known during sublayout() of the main scrolling area - otherwise the delegate manager doing the layout would not know how big an extent (maxWidth, maxHeight - sublayout parameters) to allocate to it.

 

So the easy way would be to:

 

  • setTitle(Field) (or, better yet, setBanner(Field) - it doesn't have that bottom padding which people here are calling separator) rather than setTitle(String)
  • use that Field to getManager() and ask that Manager for its height using getHeight()

 

Do that during your layout/sublayout and you will get what you need.

 

As a follow-up, I would suggest reading the following knowledge base article:

MainScreen explained


----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!