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
Developer
ashishjmeshram
Posts: 154
Registered: ‎01-08-2010
My Device: 8310 Rogers
My Carrier: Vodafone

How to use JPEG images as buttons ?

Hi All

 

We have a small blackberry application which consist of many screens. Each screens has many buttons.

 

Till now we were using EditButtonField but now we need to images for the buttons.

 

can anyone let me know how we can use images as buttons?

 

Please help.

 

Please use plain text.
Developer
ashishjmeshram
Posts: 154
Registered: ‎01-08-2010
My Device: 8310 Rogers
My Carrier: Vodafone

Re: How to use JPEG images as buttons ?

Below is the code we have written so far .

 

 

 

start= Bitmap.getBitmapResource("images/start.jpg");
BitmapField startField = new BitmapField(start,
		BitmapField.FOCUSABLE) {
	protected boolean navigationClick(int status, int time) {
		fieldChangeNotify(1);
		return true;
	}
};

startField.isFocus();

FieldChangeListener listener = new FieldChangeListener() {
	public void fieldChanged(Field field, int context) {


	}
}

 

 

Please use plain text.
Developer
luisfernando
Posts: 91
Registered: ‎11-24-2009
My Device: Storm

Re: How to use JPEG images as buttons ?

Please take a look on this post:

 

http://supportforums.blackberry.com/t5/Java-Development/Storm-9500-Image-Button/m-p/203313

 

Very useful

-Luis Fernando

Remember to give Kudos(click on the star at your left) if this helped you.
Click on "Accept Solution" if the problem is resolved.
Espanol? Escribeme un mensaje privado.
Please use plain text.
Developer
ashishjmeshram
Posts: 154
Registered: ‎01-08-2010
My Device: 8310 Rogers
My Carrier: Vodafone

Re: How to use JPEG images as buttons ?

I can show the image but when i click on the image nothing happens.

 

 

 

Please use plain text.
Developer
luisfernando
Posts: 91
Registered: ‎11-24-2009
My Device: Storm

Re: How to use JPEG images as buttons ?

Override  trackwheelClick as described in:

 

http://supportforums.blackberry.com/t5/Java-Development/How-to-put-Image-on-Buttonfield/m-p/21691

-Luis Fernando

Remember to give Kudos(click on the star at your left) if this helped you.
Click on "Accept Solution" if the problem is resolved.
Espanol? Escribeme un mensaje privado.
Please use plain text.
Developer
ashishjmeshram
Posts: 154
Registered: ‎01-08-2010
My Device: 8310 Rogers
My Carrier: Vodafone

Re: How to use JPEG images as buttons ?

Okay. When I click on the track wheel it works but when i use enter button it does not work.

 

How can I make my image button respond to both track wheel click as well as enter button?

 

My other normal button responds to enter click.

 

 

 

Please use plain text.
Developer
luisfernando
Posts: 91
Registered: ‎11-24-2009
My Device: Storm

Re: How to use JPEG images as buttons ?

Using:

 

 

protected boolean keyChar(char key, int status, int time) {
	boolean retVal = false;

	if(key == Characters.ENTER){
		System.out.println("pressed enter");
		retVal = true;
	}

	return retVal;
}

 

 

As described in:

 

http://supportforums.blackberry.com/t5/Java-Development/trying-to-capture-ENTER-or-wheel-press-on-li...

 

Remember to use Search, usually other people already asked similar things :smileyhappy:

 

Remember to mark this as solved if this helped you. And click on the star under my name to give kudos.

 

 

-Luis Fernando

Remember to give Kudos(click on the star at your left) if this helped you.
Click on "Accept Solution" if the problem is resolved.
Espanol? Escribeme un mensaje privado.
Please use plain text.
Developer
ashishjmeshram
Posts: 154
Registered: ‎01-08-2010
My Device: 8310 Rogers
My Carrier: Vodafone

Re: How to use JPEG images as buttons ?

Thanks for the quick replies. They all lead me in the right direction. So far so good.

 

Now the problem I have is that I need to have four image buttons on my one screen. There is different action to be invoked on click of each image button.

 

How do I check which image button was clicked? 

 

 

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Characters;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Graphics;

public class ImageButton extends Field {

	private Bitmap _currentPicture;
	private Bitmap _onPicture;
	private Bitmap _offPicture;
	private int id;

	public ImageButton(Bitmap onImage, Bitmap offImage) {
		super(Field.FOCUSABLE);
		_offPicture = offImage;
		_onPicture = onImage;
		_currentPicture = _onPicture;
	}

	public ImageButton(String image, String hoverImage) {
		super(Field.FOCUSABLE);
		_offPicture = Bitmap.getBitmapResource(hoverImage);
		_onPicture = Bitmap.getBitmapResource(image);
		_currentPicture = _onPicture;
	}
	
	public void setButtonId(int id) {
		this.id = id;
	}

	public int getButtonId() {
		return this.id;
	}

	public int getPreferredHeight() {
		return _onPicture.getHeight();
	}

	public int getPreferredWidth() {
		return _onPicture.getWidth();
	}

	protected void onFocus(int direction) {
		_currentPicture = _offPicture;
		invalidate();
	}

	protected void onUnfocus() {
		_currentPicture = _onPicture;
		invalidate();
	}

	protected void drawFocus(Graphics g, boolean on) {
		g.setBackgroundColor(Color.BLACK);
	}

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

	protected void paint(Graphics graphics) {
		graphics.setBackgroundColor(Color.BLACK);
		graphics.clear();
		graphics.fillRect(0, 0, getWidth(), getHeight());
		graphics.drawBitmap(0, 0, getWidth(), getHeight(), _currentPicture, 0,
				0);
	}

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

	public boolean keyChar(char key, int status, int time) {
		if (key == Characters.ENTER) {
			fieldChangeNotify(0);
			return true;
		}
		return false;
	}
}


ImageButton button1= new ImageButton("images/start.jpg",
				"images/start-Hover.jpg") {
			protected boolean navigationClick(int status, int time) {
				fieldChangeNotify(1);
                // some code.
                // 

     }
};


ImageButton button2= new ImageButton("images/start2.jpg",
				"images/start-Hover2.jpg") {
			protected boolean navigationClick(int status, int time) {
				fieldChangeNotify(1);
                // some code.
                // 

     }
};


 

Is there any better way to do this?

 

Can I just create all the buttons and have a single  navigationClick method in my screen which will be called on click of any of the image buttons, where i can check which button was clicked and execute proper code accordingly?

 

 

 

Please use plain text.
Developer
peter_strange
Posts: 19,599
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to use JPEG images as buttons ?

"Is there any better way to do this?"

 

"Better" is a matter of opinion.  In this case, I would have tried to put this code:

protected boolean navigationClick(int status, int time) {
fieldChangeNotify(1);

}

 

in the ImageButton, but is that better?  Not sure.

 

What you are doing looks fine.  Does it work?

 

"How do I check which image button was clicked? "

 

You will notice that fieldChanged is called and the Field that caused the change is passed in.  So you could have some code in there that checked each Field and performed the action dependent on the Field.

 

Alternatively each Button could notify using a different number.  The fieldChanged code could process according to the number.

 

Finally you could have some intelligence in each ImageButton and it could decide what to do. 

 

Again in my opinion, there is no right way, some will argue one way, some another.  Go with something you understand that can be maintained.   

 

But this is only my opinion. 

Please use plain text.
Developer
luisfernando
Posts: 91
Registered: ‎11-24-2009
My Device: Storm

Re: How to use JPEG images as buttons ?

I agree with peter. There is no right way to do it. And here is where your programmer skills comes. In my case what I did was to extend the field to create a ImageButton class with the properties I Needed. To identify everybutton I just created a method to assign a unique ID to each one and pass that ID to whoever is listening when I click on the button or focus the button.

 

Again, nothing is written in stone, you can write new code to handle your requirements.

-Luis Fernando

Remember to give Kudos(click on the star at your left) if this helped you.
Click on "Accept Solution" if the problem is resolved.
Espanol? Escribeme un mensaje privado.
Please use plain text.