views:

518

answers:

2

Hi all,

I m displaying a .jpg image in a BitmapField & adding it in a VerticalFieldManager. It is getting displayed but I cannot click on it.

I m doing it like..

Bitmap bitmap = Bitmap.getBitmapResource("image.jpg");

BitmapField bitmapField = new BitmapField(bitmap , BitmapField.FOCUSABLE)        
    {
        protected boolean navigationClick(int status, int time )
        {
            //handle click event                
            return true;
        }
    };
VerticalFieldManager vfm = new VerticalFieldManager();
vfm.add(bitmapField);
add(vfm_stamp);

But if I take a .png image like

Bitmap bitmap = Bitmap.getBitmapResource("image.png");

then it is clickable. Why so?

Blackberry 4.5 supports .jpg image format. It displays a .jpg image then why its not clickable ? I want to make it clickable.

How to do it ?

+1  A: 

You could override the method onFocus() and change it's behavior while selected.

You could also create a BitmapButton that extends net.rim.device.api.ui.Field

You will need to setChangeListener() for your bitmapField

Michael B.
I was not geeting seen the focus if the JPG is not transparent or does not have a transparent border.So i added some margin around the bitmap using the method setSpace(int,int)..Its working now..Thanks..
Shreyas
A: 

You can extand ButtonField:

package org.ci.blackberry.ui;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.component.ButtonField;

public class BitmapButtonField45 extends ButtonField {
        private static final int STATE_NORMAL = 0;
        private static final int STATE_FOCUS = 1;
        private static final int STATE_ACTIVE = 2;
        Bitmap mNormal;
        Bitmap mFocused;
        Bitmap mActive;

        int mWidth;
        int mHeight;
        private int mState = STATE_NORMAL;

        public BitmapButtonField45(Bitmap normal, Bitmap focused, 
                Bitmap active) {
                super(CONSUME_CLICK);
                mNormal = normal;
                mFocused = focused;
                mActive = active;
                mWidth = mNormal.getWidth();
                mHeight = mNormal.getHeight();
        }
        public BitmapButtonField45(Bitmap normal, Bitmap focused) {
                this(normal, focused, focused);
        }
        protected void paint(Graphics graphics) {
                Bitmap bitmap = null;
                switch (getState()) {
                case STATE_NORMAL:
                        bitmap = mNormal;
                        break;
                case STATE_FOCUS:
                        bitmap = mFocused;
                        break;
                case STATE_ACTIVE:
                        bitmap = mActive;
                        break;
                default:
                        bitmap = mNormal;
                }
                graphics.drawBitmap(0, 0, bitmap.getWidth(), bitmap.getHeight(),
                                bitmap, 0, 0);
        }

        public int getState() {
                return mState;
        }

        protected void applyTheme()
        {}
        protected void onFocus(int direction) {
                mState = STATE_FOCUS;
                super.onFocus(direction);
        }

        protected void onUnfocus() {
                mState = STATE_NORMAL;
                super.onUnfocus();
        }

        protected boolean navigationClick(int status, int time) {
                mState = STATE_ACTIVE;
                return super.navigationClick(status, time);
        }

        protected boolean navigationUnclick(int status, int time) {
                mState = STATE_NORMAL;
                return super.navigationUnclick(status, time);
        }

        public int getPreferredWidth() {
                return mWidth;
        }

        public int getPreferredHeight() {
                return mHeight;
        }

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

Examples of use: PuzzleApp.java

Max Gontar