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. New to the forum? Please visit the ‘Getting Started’ link below.
inside custom component

Java Development

Reply
Contributor
bobby_BB_developer
Posts: 18
Registered: ‎02-01-2011
My Carrier: New Developer

Orientation problem 9550

Hi All,

 

I am new to the BB world, and have started development some time back. I am developing an app for BB OS version 4.2 to 4.7.

 

I have ran into the problem on smart phones with touch capability. My app launches well in the portrait mode and while I am in the portrait mode on the app, if I switch to landscape mode the Title bar of my app would not extend to the length of width of the screen in the landscape mode. But when in this situation I hit return key, the Title bar would extend to the full width of the screen.

 

Also,if I launch my app in the landscape mode and switch to the portrait mode, the Title bar resets itself and there is no problem.

 

Kindly help me BB gurus......

 

Below is the snippet of code:-

 

 

protected void layout(int width, int height) 
{
        width = getPreferredWidth();
        height = getPreferredHeight();
        
        setExtent(width, height);                
 }
 public int getPreferredWidth() 
  {            
        
         //#ifdef JDE_4_7    
         if(Display.getOrientation() == Display.ORIENTATION_LANDSCAPE)
         {                  
         // when the cell is tilted the longer side becomes the width eg: 9550 n higher models..
         _labelWidth = Display.getWidth();        
         System.out.println("The cell is in landscape mode & the width is : "+_labelWidth);       
         return _labelWidth;
        
         }
         //#endif
        
         _labelWidth = Display.getWidth();
         System.out.println("The cell is in portrait mode & the width is : "+_labelWidth);
         return _labelWidth;                            
        }
                                 
    public int getPreferredHeight() 
    {
     _font = getFont();
        _labelHeight = _font.getHeight();
        return _labelHeight;
    }
    protected void paint(Graphics graphics)
    {
        if (_background == null)
 {        
            _background = new Bitmap(_labelWidth, 25);
            Graphics g = new Graphics(_background);
            Bitmap _temp = Bitmap.getBitmapResource("images/header_bg.png");
            if(_temp == null)
 {
                    System.out.println("Can't load background image");
                    return;
             }
            for (int i = 0; i < _background.getWidth(); i++)
            {
             System.out.println("The _backgroundWidth is : "+ _background.getWidth());
             System.out.println("The _backgroundHeight is : "+ _background.getHeight());
            
            
                g.drawBitmap(i, 0, _temp.getWidth(), _temp.getHeight(), _temp,
                             0, 0);
                
                System.out.println("The width from BitmapResource is : "+ _temp.getWidth());
             System.out.println("The heigth from BitmapResource is : "+ _temp.getHeight());
            }
        }
        graphics.drawBitmap(0, 0, _background.getWidth(), _background
                            .getHeight(), _background, 0, 0);
        invalidate();
   }
Thank you in advance for the help.
Regards,
New BB developer.

 

 

Please use plain text.
Developer
mlamagra
Posts: 204
Registered: ‎07-09-2009

Re: Orientation problem 9550

For 4.7+, sublayout(int new_width, int new_height) gets called when the orientation changes while the screen has the focus.  layout() does not get called when the orientation changes.

 

Please use plain text.
Contributor
bobby_BB_developer
Posts: 18
Registered: ‎02-01-2011
My Carrier: New Developer

Re: Orientation problem 9550

WellI changed the method from layout to sublayout() and impelemented it with the previous code posted. But it does not work properly.

 

 

protected void sublayout(int width, int height)
{
     width = getPreferredWidth();
        height = getPreferredHeight();
        
        setExtent(width, height);
 }

 

 

What the code does is, when I start the app in portrait mode the title bar stretches perfectly to fit the screen width, but now as soon as I tilt to the landscape mode, the title image does not stretch full....but if i hit return key (the one next to menu key), the image stretches itself to the width of the orientation.

 

Thanks for the help...

 

 

 

Please use plain text.
Developer
peter_strange
Posts: 17,722
Registered: ‎07-14-2008

Re: Orientation problem 9550

Use sublayout to detect an orientation change (the easiest way I found is to save the width when I last layed out the screen, then compare that in sublayout with the current width.  If there is a change, then you need to relayout the screen. 

 

Here is typical sublayout that I will use:


    protected void sublayout(int width, int height) {
        if ( _screenWidth != Display.getWidth() ) {
            createScreen();
        }
        super.sublayout(width, height);
    }

 

createScreen is also called from the constructor - in there I do everything that is width specific.  In your case, you might want to replace the title to make sure it is re-laid out. 

 

Perhaps this 'structure' will help you get over your problem?

Please use plain text.
Developer
mlamagra
Posts: 204
Registered: ‎07-09-2009

Re: Orientation problem 9550

[ Edited ]

edit:  I think it would be better to use the parameters rather than getting the dimensions some other way:

 

 

  protected void sublayout(int new_width, int new_height) {
setExtent(saved_width = new_width, saved_height = new_height);
setPosition(0, 0);
/*
Then call layout() and setPositionChild() for everything directly under this Manager.
  Each of those items must do its own setExtent() in its own layout()
*/

// edit:
super.sublayout(new_width, new_height); // lay out working fields
// Then, if you're using a custom title field on a FullScreen:
layoutChild(title, new_width, new_height / 12); // or whatever height
setPositionChild(title, 0, 0);
}

 

 

Then, overriding layout() is probably not necessary, since it's supposed to call sublayout().

 

Please use plain text.
Developer
peter_strange
Posts: 17,722
Registered: ‎07-14-2008

Re: Orientation problem 9550

Not sure if this was directed at my response:

"Why try to get the correct new dimensions from somewhere else"

 

Assuming it was directed at my post, I suspect you have misread it.  The processing uses the change in the value to detect an orientation change, not to process it.

 

Also as far as I know, in general for sublayout, the values passed in are the maximums available, and may, not be related to the Screen dimensions. 

 

Also, since this user does not actually wish to control the layout, I would have thought you should allow it to call super.sublayout, not just immediately impose an extent based on the maximums that this will have been supplied.  But this is not an area of expertise. 

Please use plain text.
Developer
mlamagra
Posts: 204
Registered: ‎07-09-2009

Re: Orientation problem 9550

[ Edited ]

You're right, I did misread it (s0rry), but I was looking more at bobby's getPreferredWidth and getPreferredHeight in sublayout rather than using the parameters.  They're probably the same for a FullScreen, but assuming the BB OS is updated someday and the app may eventually be in a window or virtual screen of some sort instead of the entire display, it seems better IMO not to assume it actually got the same maximums it asked for.  Even now, I think on a MainScreen the sublayout is the display size minus the title bar, because it's the Manager not the Screen doing the sublayout, and that's the VerticalFieldManager under the title.  Might as well get into the habit of doing it in a way compatible with everything.  A PopupScreen would be even worse, if it comes up as 80% width and height, but the app lays it out like it's got the entire display.

 

super.sublayout(), I'm not even sure it does anything -- does it?  The javadoc sounds like it's there only for "custom formatting".  Think every app where I've had to handle orientation changes myself was something where I was doing all my own drawing, in Manager.paint() or one big BitmapField, so all I had to do was update my dimension variables and paint took care of the rest.

 

edit: I was probably misreading the javadoc also (guess I need more sleep).  Obviously you're right, super.sublayout() should be called in this case, but I'd follow it up by laying out the title field explicitly in view of the problem.  Also, bobby, shouldn't the class variables being set in your sublayout() be named differently than the parameters?  As-is, does that really work?  I'd have thought it would have tried setting the parameter variables rather than the class variables.

 

Please use plain text.
Contributor
bobby_BB_developer
Posts: 18
Registered: ‎02-01-2011
My Carrier: New Developer

Re: Orientation problem 9550

Thanks guys for this discussion..I shall try to implement both the ways one by one...and post my reply...but as I am just a new-bee I have not got command over the GUI stuf.....

Please use plain text.
Contributor
bobby_BB_developer
Posts: 18
Registered: ‎02-01-2011
My Carrier: New Developer

Re: Orientation problem 9550

Well i used the sublayout(int width, int height), but it gives me JVM Error 104 illegal state exception.

 

Here is the snippet of the Code

 

public class MYAPP extends Fullscreen

 

implements DrawStyle 
{
    private String _label;
    private Font _font;
    private int _labelHeight;
    private int _labelWidth;
    private Bitmap _background;
    private Bitmap _bitmapLocker;    
    public TitleField(String label) 
    {
        this(label, 0);    
    }
    public TitleField(String label, long style) 
    {
        super(style);        
        _label = label;
        _font = getFont();
        _labelHeight = _font.getHeight();
        _labelWidth = Display.getWidth();
        System.out.println("The _labelWidth in sublayout Display.getWidth() : "+ _labelWidth);
        
    }
    
/*
when i see the log file it shows me _labelWidth = correct width
and than it fails
*/
    protected void sublayout(int width, int height)
    {
      System.out.println("The width in sublayout(width,height) is : "+ width);        
      if(_labelWidth != Display.getWidth())
      {
      System.out.println("Call the createScreen()");
      }
 super.sublayout(width, height);
    }            
    
    protected void paint(Graphics graphics)
    {
        if (_background == null) 
        {   
         _background = new Bitmap(_labelWidth, 25);
         Graphics g = new Graphics(_background);
            Bitmap _temp = Bitmap.getBitmapResource("images.png");
            if(_temp == null) 
            {
                    System.out.println("Can't load background image");
                    return;
            }
            for (int i = 0; i < _background.getWidth(); i++)
            {
             System.out.println("The _backgroundWidth is : "+ _background.getWidth());
             System.out.println("The _backgroundHeight is : "+ _background.getHeight());
            
             //g.drawBitmap
                graphics.drawBitmap(i, 0, _temp.getWidth(), _temp.getHeight(), _temp,
                             0, 0);
                
                System.out.println("The width from BitmapResource is : "+ _temp.getWidth());
             System.out.println("The heigth from BitmapResource is : "+ _temp.getHeight());
            }
        }
        graphics.drawBitmap(0, 0, _background.getWidth(), _background
                            .getHeight(), _background, 0, 0);
        
    }
    public String getLabel() {
        return _label;
    }
    public void setLabel(String label) {
        _label = label;
        // _labelWidth = _font.getAdvance(_label);
    }
}

 

 

 

Thanks guys for helping me earlier....If you gurus can help me a bit more it would be great.....

Please use plain text.