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
divit
Posts: 115
Registered: ‎10-24-2009

place an image on a Button

Hi all

I want to place image on a button so that on click of that image (button) i can handle that event by displaying another screen (navigating to another screen).

so is it possible to place a image on a button??????

if yess please can i know how to achive that?????

 

or if that is not possible how can i do this (i.e on click of the image(6 images are placed in a grid view ) i want to navigate to another screen, )

here how can i know which image is clicked ?????

 

Please help me 

 

Regards

Divya

 

 

Please use plain text.
Developer
BBDeveloper
Posts: 3,951
Registered: ‎07-15-2008

Re: place an image on a Button

Check the custombuttonsmenu shipped with JDE.


Use Search. "Accept Solution" If the problem is resolved.
Please use plain text.
Developer
ksykulev
Posts: 58
Registered: ‎07-09-2009

Re: place an image on a Button

try something like this...
class IconButtonField extends Field {
        private Bitmap icon;
        private String label;
        private int fieldHeight;
        private int fieldWidth;
        private int padding = 10;
        private int bgColor;
       private int txtColor;
       private int bgHighlightColor = 0x185ab5;
       private int txtHighlightColor = 0xffffff;
       private Font defaultFont = Font.getDefault();
       private Font smallerFont = defaultFont.derive(Font.PLAIN,22);

        public IconButtonField(String label, Bitmap icon) {  
        this.label = label;
        this.icon = icon;
        
        fieldHeight = smallerFont.getHeight() + padding;
        fieldWidth = smallerFont.getAdvance(label) + (padding * 4)+ icon.getWidth();
        this.setPadding(2,2,2,2);

        bgColor = 0x000008;
        txtColor = 0xd6d6d6;
        }
        public int getPreferredHeight(){
            return fieldHeight;
        }
    public int getPreferredWidth(){
        return fieldWidth;
    }
    
    protected void layout(int maxWidth, int maxHeight){
        setExtent(getPreferredWidth(), getPreferredHeight());
    }
    
    protected void paint(Graphics g){         
            g.setColor(bgColor);
            g.fillRoundRect(0,0, getPreferredWidth(), getPreferredHeight(),10,10);
            
            g.setColor(txtColor);
            g.drawRoundRect(0,0, getPreferredWidth(), getPreferredHeight(),10,10);
            
            g.drawBitmap( (padding*2),((padding/2)+(smallerFont.getHeight()/2))-(icon.getHeight()/2)+1,icon.getWidth(),icon.getHeight(),icon,0,0);
            
            g.setFont(smallerFont);
            g.drawText(label, (padding*2)+icon.getWidth()+5,  (padding/2)+1);
    }
}
Please use plain text.
Developer
Blanc
Posts: 638
Registered: ‎07-02-2009

Re: place an image on a Button

This code is working fine and i hope it is helpful to you..........

 package test;

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

 public class MyButtonField extends Field 
 {

 private int backgroundColour = 0xFFFFFF;
 private Bitmap button;
 private Bitmap on;  
Bitmap.getBitmapResource("img/pink_scribble.bmp");
 private Bitmap off; 
Bitmap.getBitmapResource("img/blue_scribble.bmp");
 private int fieldWidth;
 private int fieldHeight;
// private int buffer = (Display.getHeight() - 105) / 2;
 private String text;
 
 
 private static long style = Field.FIELD_HCENTER | Field.FIELD_VCENTER;

 public MyButtonField(String _text, String onpath, String offpath) 
 {
 super(Field.FOCUSABLE | style);
 text = _text;
 on = Bitmap.getBitmapResource(onpath);
 off = Bitmap.getBitmapResource(offpath);
 fieldWidth = on.getWidth();
 fieldHeight = on.getHeight();
 button = off;
// fieldFont = FieldFont();
 } 
 public MyButtonField(String _text, String onpath, String offpath, boolean background)
 {
	 this(_text, onpath , offpath);
	 this.background = true;
 }
 public MyButtonField(String _text, String onpath, String offpath, long style)
 {
	 this(_text, onpath , offpath);
 }
 public MyButtonField(String _text, String onpath, String offpath, int background)
 {
	 this(_text, onpath , offpath);
	 backgroundColour = background;
 }
 protected boolean navigationClick(int status, int time) {
 fieldChangeNotify(1);
 return true;
 }

 protected void onFocus(int direction) {
 button = on;
 invalidate();
 }

 protected void onUnfocus() {
 button = off;
 invalidate();
 }

 public int getPreferredWidth() {
 return fieldWidth;
 }

 public int getPreferredHeight() {
 return fieldHeight;
 }

 protected void layout(int arg0, int arg1) {
 setExtent(getPreferredWidth(), getPreferredHeight());
 } 

 public static Font FieldFont() {
 try {
 FontFamily theFam = FontFamily.forName("SYSTEM");
 return theFam.getFont(net.rim.device.api.ui.Font.BOLD, 14);
 } catch (ClassNotFoundException ex) {
 ex.printStackTrace();
 }
 return null;
 }

  
  
  
 protected void fieldChangeNotify(int context) {
  this.getChangeListener().fieldChanged(this, context);
 }

 protected void paint(Graphics graphics) 
 {
	 graphics.setBackgroundColor(backgroundColour);
	 graphics.clear();
	 if(background)
		 graphics.setColor(Color.SKYBLUE);
	 else
		 graphics.setColor(backgroundColour);	 
 graphics.fillRect(0, 0, fieldWidth, fieldHeight);
 graphics.drawBitmap(0, 0, fieldWidth, fieldHeight, button, 0, 0);
 }

public String getLabel() {
	return text;
}
 }

 

 

Thanks and Regards,
PraveenGoparaju.
Please use plain text.
Developer
divit
Posts: 115
Registered: ‎10-24-2009

Re: place an image on a Button

thanks for the reply but i am getting error  n these lines 

 

1)Bitmap.getBitmapResource("help.bmp"); //in ur code Bitmap.getBitmapResource("img/pink_scribble.bmp"); i have chnaged the image and its path

2)Bitmap.getBitmapResource("help1.bmp"); // n ur code Bitmap.getBitmapResource("img/blue_scribble.bmp"); i have chnaged the image and its path


3) this.background = true;  //here is the error public MyButtonField(String _text, String onpath, String offpath, boolean background) in this constructor

 

Please help me

 

Please use plain text.
Developer
BBDeveloper
Posts: 3,951
Registered: ‎07-15-2008

Re: place an image on a Button

Check with the declaration of variables.


Use Search. "Accept Solution" If the problem is resolved.
Please use plain text.
Developer
rakesh86shankar
Posts: 979
Registered: ‎05-22-2009

Re: place an image on a Button

Check for Whether  u have selected the  specified path,that's place where i use to make the problem \

 

Please use plain text.
Developer
divit
Posts: 115
Registered: ‎10-24-2009

Re: place an image on a Button

ya i checked all other things are correct but still showing error in those  lines 

Please use plain text.
Developer
BBDeveloper
Posts: 3,951
Registered: ‎07-15-2008

Re: place an image on a Button

Post the complete error details.


Use Search. "Accept Solution" If the problem is resolved.
Please use plain text.
Developer
Blanc
Posts: 638
Registered: ‎07-02-2009

Re: place an image on a Button

@this.background = false/true;

 

it is my own constructor and it is not necessary to implement the same constructor in your prgm. comment that variable background and try to execute it.

 

for your sake use the cons

public MyButtonField(String _text, String onpath, String offpath) 

 

 

Thanks and Regards,
PraveenGoparaju.
Please use plain text.