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
New Developer
Posts: 8
Registered: ‎04-02-2009
My Device: Not Specified

Storm 9500 - Image Button

Hi All,

 

I tried it quit a bit, but could not get a solution. It kind of freaks me out, it can't be that difficult!

Ok, what I'm talking about is an ImageButton. Noting special, just an Image, (PNG-file) which must be "clickable"!

No Frame, no nothing, just the Image!!!

 

Please provide me some sample code!!! I'm using JDE 4.7!

 

Thanks a lot!

S.

Developer
Posts: 170
Registered: ‎01-17-2009
My Device: 9630

Re: Storm 9500 - Image Button

[ Edited ]

Use a BitmapField and a FieldChangeListener

 

Edit: Make sure it has the attribute Field.FOCUSABLE 

Message Edited by gregzav on 04-03-2009 03:55 PM
Developer
Posts: 19,631
Registered: ‎07-14-2008
My Device: Not Specified

Re: Storm 9500 - Image Button

In addition t using a FieldChangeListener, you can also override navigationClick.

 

There is sample code towards the end of the following Thread:

 

http://supportforums.blackberry.com/rim/board/message?board.id=java_dev&message.id=656

 

Developer
Posts: 103
Registered: ‎12-01-2009
My Device: Not Specified

Re: Storm 9500 - Image Button

Hi,

 

I am using the following code display a image and to use as a button :

 

import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;
import java.io.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.math.Fixed32;
import net.rim.device.api.system.*;
public class imageDisplay extends MainScreen{

EncodedImage ei = EncodedImage.getEncodedImageResource("odometerGraphic.gif");
EncodedImage scaled_image = null;
public imageDisplay()
    {
       
    final LabelField title = new LabelField("image demo",
    LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
    final LabelField title1 = new LabelField("metallifwgfddfdfdfca",
             LabelField.USE_ALL_WIDTH );
    setTitle(title);   
   
   
   final FocusChangeListener listener = new FocusChangeListener(){
        public void focusChanged(Field field, int eventType)
        {
            if (eventType == FOCUS_CHANGED) {
           
       
            UiApplication.getUiApplication().pushScreen(new FinalScreen());
            }
        }
    };
       
   
   
    final HorizontalFieldManager horizontalFieldManager = new
    HorizontalFieldManager(NO_VERTICAL_SCROLL){
    protected void sublayout( int maxWidth, int maxHeight )

                            {
                           int width = Display.getWidth()/3;
                           int height = Display.getHeight() - title.getPreferredHeight();
                           super.sublayout( width, height);
                           setExtent( width, height);
                          

                           }

                    public void paint(Graphics graphics)

                    {

                           int width = Display.getWidth()/3;
                           int height = (Display.getHeight() - title.getHeight());
                           float a1 = (float)ei.getWidth()/width;
                           Float a = new Float(a1);
                           String s1 = a.toString();
                           float a2 = (float)ei.getHeight()/height;
                           Float b = new Float(a2);
                           String s2 = b.toString();
                           int scalex = Fixed32.parseFixed32(s1);
                           int scaley = Fixed32.parseFixed32(s2);
                           scaled_image = ei.scaleImage32(scalex, scaley);
                           graphics.drawBitmap(0,0 , width, height, scaled_image.getBitmap(), 0, 0);
                           BitmapField bmp = new BitmapField(scaled_image.getBitmap());
                           bmp.setFocusListener(listener);
                         
                          
                           super.paint(graphics);
                         
                    }

              };
         
            
         
     
             
           
                      
            add(horizontalFieldManager);
           
       
    }

}

 

 

It is displaying image properly but when i click on the image my next screen does not appear.

Whats the issue with this code??

 

Thanks,

rahul

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

Re: Storm 9500 - Image Button

You should ask a new question in a new Thread.

 

Please review my comments below, change/test as you can, then if you need further help, start a new Thread. 

 

I've tried to understand your code, but I'm very confused by it.  For example, you seem to push the new screen on focus change, rather than pressing the button.  You are also creating a new button in the middle of paint method - which is not an appropriate place.  And I can't see where this new Button is used. 

 

So I suggest you create a very simple button, with no fancy painting, add it to a normal MainScreen, and then see if you can get that pushing a new screen.  Assuming you can get that working, swap out your trivial button for a more complicated one with an image.  If you get that working put your HorizontalFieldManager in,  and so on until you build the functionality you require. 

 

If this step approach breaks at some point, then post the code that ceased it to stop working.

 

By the way, I would not scale the image in your paint routine.  If you do it there you scale it every time your picture is painted (which might be quite frequently, for example each time the menu is used),.  it would be much more efficient to Do it once (say at construction time) and reuse the scaled image.  On a Storm, do one in each orientation.

Developer
Posts: 103
Registered: ‎12-01-2009
My Device: Not Specified

Re: Storm 9500 - Image Button

ya, i want to push a new screen when focus on the image is changed.

 

i dont want to create a button, but use image as a button.

So what i am trying to do is to create a image and apply a focuschange listener to it. So when focus changes it pushes a new screen and hence act as a button.

 

Thanks,

rahul

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

Re: Storm 9500 - Image Button

I think my comments still stand, start off with something simple, just a plain BitmapField, and get the focus change thing working, before complicating it with your image.

 

Please post responses to your own Thread from now on:

http://supportforums.blackberry.com/t5/Java-Development/issue-with-using-image-as-button/td-p/393157

Trusted Contributor
Posts: 113
Registered: ‎09-27-2010
My Device: Not Specified

Re: Storm 9500 - Image Button

I made a custom bitmap field that takes care of this. It also supports hover effect on focus.

 

 

public class CSBitmapField extends BitmapField {

 

Bitmap hover;

Bitmap old;

private Command command;

 

public CSBitmapField(Bitmap source, Bitmap hover) {

super(source, Field.FOCUSABLE);

this.old = source;

this.hover = hover;

setBackground(BackgroundFactory.createSolidBackground(Color.BLACK));

}

 

protected void onFocus(int direction) {

setBitmap(hover);

super.onFocus(direction);

}

 

protected void onUnfocus() {

setBitmap(old);

super.onUnfocus();

}

 

public void setCommand(Command command) {

this.command = command;

}

 

protected boolean navigationClick(int status, int time) {

if (command.canExecute(this)) {

command.execute(this);

 return true;

}

 return false;

}

 

}

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

Re: Storm 9500 - Image Button

Think you might find this is OS 6.0 code only.

Trusted Contributor
Posts: 113
Registered: ‎09-27-2010
My Device: Not Specified

Re: Storm 9500 - Image Button

that might be Smiley Wink