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
Regular Contributor
Posts: 92
Registered: ‎03-09-2013
My Device: bold 9800
My Carrier: blackberry
Accepted Solution

How to refresh page on changing orientation using Blackberry 9800 Bold

Hello Every One,

                              I am working on an application in which i am using boht modes Landscape and portrait. But the problem is that if i start the application in protrait mode then it will adjust the image according to portrait style but i change the mobile orientation to landscape onthe same screen vice versa it will not change the image size and according to this.. Like the Following image:

 

LandPort.PNG

Although i am usinng such formula for resizing and i have such images according to different sizes but it is not updating. Kindly tell me how can i do that.

 

final Bitmap _backgroundBitmap = Bitmap.getBitmapResource(Display.getWidth()+"x"+Display.getHeight()+".png");

 

e.g 480x240.png , 640x360.png

 

VerticalFieldManager    CurvedSquareImage = new VerticalFieldManager(VerticalFieldManager.USE_ALL_WIDTH |VerticalFieldManager.USE_ALL_HEIGHT)
        {
            protected void sublayout(int mWidth, int mHeight)
                {

                   super.sublayout( Display.getWidth(),  Display.getHeight());
                   setExtent( Display.getWidth(),  Display.getHeight());

                }
            public void paint(Graphics graphics)
             {
                graphics.drawBitmap(0, 0, mWidth,mHeight,_backgroundBitmap, 0, 0);
                super.paint(graphics);
                }

        };

 

Thanks in advance.

 

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

Re: How to refresh page on changing orientation using Blackberry 9800 Bold

I keep seeing this code, I'd love to know where it came from:

 

            protected void sublayout(int mWidth, int mHeight)
                {

                   super.sublayout( Display.getWidth(),  Display.getHeight());
                   setExtent( Display.getWidth(),  Display.getHeight());

                }

 

This sublayout() is not correct for two reasons:

 

a) If you are going to restrict the Manager's height and width, you need to make sure that your Manager is not already restricted to something less that what you are trying.  So you should do something like:

super.sublayout( Math.min(Display.getWidth(), mWidth),  Math.min(Display.getHeight(), mHeight));

In this circumstance, it appears you have tool bar at the top, presumably in the title or banner area of a MainScreen, and so you don't actually have the full height of the screen anyway.

 

b) If you ask a Manager to lay itself out, then you should respect the size that it uses.  If you force a size using

setExtent( Display.getWidth(),  Display.getHeight());

then you potentially have confused it because it thinks it has the size it asked for, and you are actually forcing it to use a different size. 

 

So please correct that code, and correct it wherever you use it.

 

I recommend you look round for documentation and samples for Managers to understand how you use one before you start overriding methods.  I particularly recommend you review this:

 

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

 

as it will help you understand how to tell a Manager to use all the space available to it. 

 

Now on to your problem.

 

On re-orientation, sublayout for your Manager will be called, so it will have to choose the correct image.  Now if you change the image during layout, you are effectively asking the device to relayout during layout, and it will tell you it can't do it - it will throw an exception.  So you have to, in sublayout, use code like the following:

 

UiApplication.getUiApplication().invokeLater(new Runnable() {

public void run() {

// update the bitmap in here

}

});

 

What this will do is update the image, and the update will force a another layout of all the Fields - in other words sublayout will be called again, but this time with the correct image. 

 

Because sublayout will be called again, you need to be sure that you don't initiate the Bitmap update again.  So what I usually do is have a flag that specifies the orientation I expect, and if it changes, set it to the new orientation and initiate any updates that are required as a result of the change.

 

Put logging statements in your sublayout code to make sure that you don't run into infinite loop problems in there. 

Regular Contributor
Posts: 92
Registered: ‎03-09-2013
My Device: bold 9800
My Carrier: blackberry

Re: How to refresh page on changing orientation using Blackberry 9800 Bold

Dear Peter,

                     Literally thanx alot for your response with such detail. Peter i am very new to Blackberry Development. Please it should be very helpful for me if you tell me with code sample that how can i use UiApplication.getUiApplication().invokeLater(new Runnable() {  into my code snippet for updating the image if i change my phone orientation on the same screen. Please help me out there, it should be very helpful for me.

I am using defferent Custom Managers in the same class. And i want to make my image should be fit on the screen.

public class SecondScreen extends MainScreen{
    Bitmap _backgroundBitmap;
    public SecondScreen()
    {    
        //setTitle("SecondScreen");
        
        //Ui.getUiEngineInstance().setAcceptableDirections(Display.DIRECTION_PORTRAIT);
        
        TabHorizontalManager abcd = new TabHorizontalManager("TabManager1");//LockedHeaderHorizontalManager
        HeaderLogoHorizontalManager head = new HeaderLogoHorizontalManager("Access Mobile Banking");
        LockedHeaderHorizontalManager head1 = new LockedHeaderHorizontalManager("Account Balance");
        HorizontalLine line = new HorizontalLine("Account Balance");
        
        final int mWidth = Display.getWidth();
        final int mHeight = Display.getHeight();
    
        
        

/*UiApplication.getUiApplication().invokeLater(new Runnable() {

public void run() {

// update the bitmap in here

}

});*/    // Please tell me with sample code how to use this to update on orientation changing.


        
        VerticalFieldManager    CurvedSquareImage = new VerticalFieldManager(VerticalFieldManager.USE_ALL_WIDTH |VerticalFieldManager.USE_ALL_HEIGHT)
        {
            
            protected void sublayout(int mWidth, int mHeight)
                {
                super.sublayout( Math.min(Display.getWidth(), mWidth),  Math.min(Display.getHeight(), mHeight));
                   /*super.sublayout( Display.getWidth(),  Display.getHeight());
                   setExtent( Display.getWidth(),  Display.getHeight());*/

                }
            public void paint(Graphics graphics)
             {
                graphics.drawBitmap(0, 0, mWidth,mHeight,_backgroundBitmap, 0, 0);
                super.paint(graphics);
                }

        };
        CurvedSquareImage.add(head);
        CurvedSquareImage.add(head1);
        CurvedSquareImage.add(abcd);
        CurvedSquareImage.add(line);
        add(CurvedSquareImage);
    
    }
}

 

 

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

Re: How to refresh page on changing orientation using Blackberry 9800 Bold

I suggest you don't mark a solution until the problem is actually solved.

 

Anyway, in brief, here are the changes that you could make to detect and take action on orientation change.  I suggest you review these, make sure you understand what the changes are trying to achieve, and then implement as you decide is appropriate.  Do not just cut/paste my code - understand the code you put in your program. 

 

After this:

Bitmap _backgroundBitmap;

add a variable to indicate current orientation:

boolean landscape = false;

 

After this:

final int mHeight = Display.getHeight();

add the following

if ( mHeight < mWidth ) {

landscape = true;

 

In sublayout after

super.sublayout(...)

add the following

boolean nowLandscape = false;

if ( Display.getHeight(), < Display.getWidth() ) {

nowLandscape = true;

}

if ( nowLandscape != landscape ) {

// We have changed orientation

landscape = nowLandscape;

UiApplication.getUiApplication().invokeLater(new Runnable() {

public void run() {

if ( landscape ) {

_backgroundBitmap.setBitmap(<landscape format bitmap>);

else {

_backgroundBitmap.setBitmap(<portrait format bitmap>);

}

});

}

 

 

Can I recommend that you also review this:

http://supportforums.blackberry.com/t5/Java-Development/What-is-the-Event-Thread/ta-p/446865