views:

41

answers:

1

I want to change the BitmapField image onFocus and onUnfocus and I need an onClick event on it. How can i do this? Here is my code but it is firing an event on focus change.

public class ClickbleImage extends BitmapField {
    public ClickbleImage(Bitmap bitmap,long style) {
        super(bitmap,style);
    }

    public boolean isFocusable() {
        return true;
    }

    protected boolean navigationClick(int status, int time) {
       fieldChangeNotify(0);
       return true;
    }

    protected void onFocus(int direction) {
       this.setBitmap(Bitmap.getBitmapResource("img/editfocus.jpg"));
       super.onFocus(direction);
    }

    protected void onUnfocus() {
       this.setBitmap(Bitmap.getBitmapResource("img/edit.png"));
       super.onUnfocus();
    }
}
A: 

I'm not entirely sure what you mean by "it is firing an event on focus change." Isn't that what you want?

I worked on a custom Bitmap button last month, and ended up creating a subclass of Field, and handling the drawing myself. I don't remember why a subclass of BitmapField didn't work, but you might want to look at that. You would have a little more control.

In addition, you need to add calls to invalidate() in your onFocus and onUnFocus methods, because this will cause the field to be repainted with the new Bitmap. Finally, think about overriding drawFocus() and doing nothing inside that method, because this will prevent the drawing of the standard focus style (a blue halo). Unless you want that, but if you have your own focus image it might be undesirable.

If you post some more details about the problem I can try to help, but I am not sure what the issue is.

Edited to add:

I took a look at my notes and found this link,

http://www.thinkingblackberry.com/archives/167

and on second look, there is a custombuttonsdemo in the samples\com\rim\samples\device\ directory of the BB JDE 5.0. It implements basically exactly what that blog post describes, it might be helpful. That blog post describes drawing a custom button, not using a Bitmap, but it's the same idea. In the paint(Graphics g) method you would do

protected void paint(Graphics g) {
    g.drawBitmap(0,0,getWidth(),getHeight(),bmp,0,0);
}

The RIM samples includes a PictureBackgroundButtonField which implements a Bitmap button. It's what I used and has more detail than what I wrote above.

spacemanaki