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: 94
Registered: ‎05-29-2010
My Device: 9000 bold
My Carrier: asfas
Accepted Solution

change the background dynamically

hi all

i want to change the background of my applcation when i click on a button. This background should appear on all the screens. Is there any way to do that.

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

Re: change the background dynamically

[ Edited ]

There is no APi to do this in a global way, but of course you can have shared code in all your processing to set your background.

 

How do you set your background in the first place?

Regular Contributor
Posts: 94
Registered: ‎05-29-2010
My Device: 9000 bold
My Carrier: asfas

Re: change the background dynamically

Actually since i am using jde 4.5 , I cannot use Background Class so I am using the paint method as follows

 

VerticalFieldManager mainLayout = new VerticalFieldManager(){
            protected void paint(Graphics graphics) {
                graphics.drawBitmap(0, 0, 480, 320, backgoundBitmap, 0, 0);
                super.paint(graphics);
            }
        };

 

I am using a similar manager in each class.

 

Reagrding the shared code i have tried it. I used a static Bitmap in a Constants Class and on clicking on a button I change the bitmap as follows

 

Constants.ThemeCode=ThemeRadioGroup.getSelectedIndex()+1;

if(Constants.ThemeCode==Constants.THEME_BLUE){
                 background=Bitmap.getBitmapResource("bodyimg_blue.JPG");            

}
             else if(Constants.ThemeCode==Constants.THEME_GREEN){
                 background=Bitmap.getBitmapResource("green.png");

             }

 

But the problem is all the previous screens remain unaffected. Only the screens which are pushed from then onwards are affected.

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

Re: change the background dynamically

Three approaches to try, one of which will work, but is a lot of coding for you, one which might work but is a little less coding and one which I have no idea about, but which will be very little coding if it does work.

 

1) Least effort, but might not work:

When the background changes, issue:

UiApplication.getUiApplication.updateDisplay();

read the API doc to see what this will do.

 

2) Little effort and should work

In your change processing, work through each of the Screens in your display stack,

starting with the

UiApplication.getUiApplication.getActiveScreen()

and then move the the lower one using

<screen>.getScreenBelow()

calling

<screen>.invalidate()

I suspect the last "<screen>.getScreenBelow()" will return a null, if not, use

UiApplication.getUiApplication.getScreenCount()

as a limit to your loop

 

3) Most effort

on each screen override onExposed().  In there, check to see if the background has been changed and if it has, invalidate() the Screen. 

 

Let us know how you get on with these options.

Regular Contributor
Posts: 94
Registered: ‎05-29-2010
My Device: 9000 bold
My Carrier: asfas

Re: change the background dynamically

both the first and second approach did not work and i dont know how to see if the backgorund has been changed in the onExposed method

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

Re: change the background dynamically

[ Edited ]

I would suggest using a system-wide singleton object (Theme / Skin) which would contain the reference to the current background.  Your custom paint should always get the Bitmap from that reference and never store it directly inside your Screen.  Then, when you change the background, invalidate the active screen.  In addition, add invalidate to each Screen's onExposed and you should be all set.

 

Edit: I've re-read your first reply and I see you have already tried having background in your Constants. I think it is the right move - now just make the rest of your application aware of it.

 

So put:

 

protected void onExposed() {
    invalidate();
}

 in each of your Screens and

 

UiApplication.getUiApplication().getActiveScreen().invalidate();

inside the code which changes your theme (invalidate() is safe to call from other Threads so you don't need invokeLater() there).

 

 

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: change the background dynamically

Because onExposed is called frequently in situations where the background will not have changed, I would recommend having some quick way checking whether it has changed and only do the invalidate if it has. 

Regular Contributor
Posts: 94
Registered: ‎05-29-2010
My Device: 9000 bold
My Carrier: asfas

Re: change the background dynamically

Actually the second approach worked fine there was some logical error i made earlier.

 

i am the following approach

 

UiApplication.getUiApplication.getActiveScreen()

and then move the the lower one using

<screen>.getScreenBelow()

calling

<screen>.invalidate()

 

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

Re: change the background dynamically

Good, I think that is better than using onExposed.  I believe that you can change the solution, perhaps you should to make sure people don't using onExposed when they don't have to.