This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Image Button in BlackBerry
Is there a way to have a picture be a button in blackberry? Trying to write a matching game.
Ted
below code may help you:
public class ImageButtonFieldTest extends Field {
private Bitmap image;
private Bitmap selectedImage;
private int height;
private int width;
public ImageButtonField(Bitmap image, Bitmap selectedImage) {
this.image = image;
this.selectedImage = selectedImage;
this.height = image.getHeight();
this.width = image.getWidth();
}
protected void layout(int maxWidth, int maxHeight) {
setExtent(image.getWidth(), image.getHeight());
}
protected void paint(Graphics graphics) {
// Draw img
if (isFocus()) {
graphics.drawBitmap(0, 0, width, height, selectedImage, 0, 0);
} else {
graphics.drawBitmap(0, 0, width, height, image, 0, 0);
}
}
public boolean isFocusable() {
return true;
}
protected void drawFocus(Graphics graphics, boolean on) { // Don't draw the default focus
}
protected void onFocus(int direction) {
super.onFocus(direction);
invalidate();
}
protected void onUnfocus() {
super.onUnfocus();
invalidate();
}
}
Related
I have defined a BitmapButtonField in Blackberry. But the default grey color background does not go. And the Bluish onFocus border?
How to change them in BB 4.5
Hi this is bitmap button and background color Red
manager=new Field(Field.FOCUSABLE){
protected void onFocus(int direction) {
super.onFocus(direction);
flag1=true;
invalidate();
}
protected void onUnfocus() {
flag1=false;
invalidate();
}
protected void layout(int width, int height) {
setExtent(image.getWidth(),image.getHeight());
}
protected void paint(Graphics graphics) {
if(flag1){
graphics.setBackgroundColor(Color.RED);
graphics.clear();
graphics.drawBitmap(0, 0, image.getWidth(),image.getHeight(), image_hover,0,0);
}else{
graphics.drawBitmap(0, 0, image.getWidth(),image.getHeight(), image,0,0);
}
}
protected boolean navigationClick(int status, int time) {
fieldChangeNotify(1);
return true;
}
};
manager.setChangeListener(this);
add(manager);
output as following image
This is background image and with default background color
manager1=new Field(Field.FOCUSABLE){
protected void onFocus(int direction) {
super.onFocus(direction);
flag2=true;
invalidate();
}
protected void onUnfocus() {
button=image;
flag2=false;
invalidate();
}
protected void layout(int width, int height) {
setExtent(image.getWidth(),image.getHeight());
}
protected void paint(Graphics graphics) {
if(flag2)
graphics.drawBitmap(0, 0, image.getWidth(),image.getHeight(), image_hover,0,0);
else
graphics.drawBitmap(0, 0, image.getWidth(),image.getHeight(), image,0,0);
}
protected boolean navigationClick(int status, int time) {
fieldChangeNotify(1);
return true;
}
};
manager1.setChangeListener(this);
add(manager1);
out put as following
Fully functional class is following for better understanding
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.FieldChangeListener;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.container.MainScreen;
public class StartUp extends UiApplication{
public static void main(String[] args) {
StartUp up=new StartUp();
up.enterEventDispatcher();
}
public StartUp()
{
pushScreen(new Demoscreen());
}
class Demoscreen extends MainScreen implements FieldChangeListener
{
Bitmap image,image_hover;
boolean flag1,flag2;
Field manager=null,manager1=null;
public Demoscreen() {
image=Bitmap.getBitmapResource("Download_48x48.png");
image_hover=Bitmap.getBitmapResource("Download_48x48.png");
manager=new Field(Field.FOCUSABLE){
protected void onFocus(int direction) {
super.onFocus(direction);
flag1=true;
invalidate();
}
protected void onUnfocus() {
flag1=false;
invalidate();
}
protected void layout(int width, int height) {
setExtent(image.getWidth(),image.getHeight());
}
protected void paint(Graphics graphics) {
if(flag1){
graphics.setBackgroundColor(Color.RED);
graphics.clear();
graphics.drawBitmap(0, 0, image.getWidth(),image.getHeight(), image_hover,0,0);
}else{
graphics.drawBitmap(0, 0, image.getWidth(),image.getHeight(), image,0,0);
}
}
protected boolean navigationClick(int status, int time) {
fieldChangeNotify(1);
return true;
}
};
manager.setChangeListener(this);
add(manager);
manager1=new Field(Field.FOCUSABLE){
protected void onFocus(int direction) {
super.onFocus(direction);
flag2=true;
invalidate();
}
protected void onUnfocus() {
flag2=false;
invalidate();
}
protected void layout(int width, int height) {
setExtent(image.getWidth(),image.getHeight());
}
protected void paint(Graphics graphics) {
if(flag2)
graphics.drawBitmap(0, 0, image.getWidth(),image.getHeight(), image_hover,0,0);
else
graphics.drawBitmap(0, 0, image.getWidth(),image.getHeight(), image,0,0);
}
protected boolean navigationClick(int status, int time) {
fieldChangeNotify(1);
return true;
}
};
manager1.setChangeListener(this);
add(manager1);
}
public void fieldChanged(Field field, int context) {
if(field==manager)
{
displayDialog("I am first field");
}else if(field==manager1)
{
displayDialog("I am second field");
}
}
public void displayDialog(final String message)
{
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
Dialog.inform(message);
}
});
}
}
}
For more details please visit following link for BackgroundsBackground logic for 4.5 OS
Check following links for customized Button Field:
Implement advanced buttons, fields, and managers.
User Defined Buttons - Create a Custom Button From an Image
Blackberry Custom Button Field
How to create a Custom Button Field in Blackberry
Normally overriding applyTheme(..) (an empty implementation) removes default styling of a field. Also we need to override paint(..), paintBackground(..), drawFocus(..) and change their implementation according to current focus status of the field. Below is a code snippet for the case when I extend a Field for making Custom Button Field.
private final Bitmap focusedBitMap = Bitmap.getBitmapResource("focused.png");
private final Bitmap unfocusedBitmap = Bitmap.getBitmapResource("unfocused.png");
protected void paint(Graphics graphics) {
// paint background
graphics.setBackgroundColor(isFocus() ? Color.RED : Color.GREEN);
graphics.clear();
// draw button image
Bitmap myBitmap = isFocus() ? focusedBitMap : unfocusedBitmap;
if (myBitmap != null) {
// image position calculation
int imageX = 0;
int imageY = 0;
graphics.drawBitmap(imageX, imageY,
myBitmap.getWidth(), myBitmap.getHeight(),
myBitmap, 0, 0);
}
}
protected void drawFocus(Graphics graphics, boolean on) {
}
public boolean isFocusable() {
return true;
}
I'd like to create an Image Button that has three states:
Normal
Focused
Pressed (or "down" or "active" whatever you call it)
Normal and Focused is pretty straightforward. I used the well-known classes BaseButtonField and BitmapButtonField as a bases. My Problem is that
protected boolean trackwheelClick(int status, int time)
is not called. My Button extends from Field and has Field.FOCUSABLE | Field.EDITABLE as styles. What am I missing?
You can try with "Tutorial: Creating a custom button" of the official RIM docs.
I think it is what your looking for
The below code is a custom button field for bottom menu bar. This will be useful for your task.
public class PictureBackgroundButtonField extends BitmapField {
MyTooltip _tooltip;
Bitmap mNormal;
Bitmap mFocused;
Bitmap mActive;
String text;
int mWidth;
int mHeight;
int xpos1;
public PictureBackgroundButtonField(String text,Bitmap normal, Bitmap focused, int xpos)
{
super(normal,FOCUSABLE);
mNormal = normal;
mFocused = focused;
mWidth = mNormal.getWidth();
mHeight = mNormal.getHeight();
this.text=text;
setMargin(0, 0, 0, 0);
setPadding(0, 0, 0, 0);
xpos1 = xpos;
}
public String getText()
{
return text;
}
public void setText(String text)
{
this.text=text;
}
protected void paint(Graphics graphics) {
Bitmap bitmap = mNormal;
if(isFocus())
{
bitmap = mFocused;
}
else
{
bitmap = mNormal;
}
graphics.drawBitmap(0, 0, bitmap.getWidth(), bitmap.getHeight(), bitmap, 0, 0);
}
protected void drawFocus(Graphics graphics, boolean on) {
}
protected void onFocus(int direction) {
//lbt.setText(text);
invalidate();
super.onFocus(direction);
if ( _tooltip != null ) {
_tooltip.removeToolTip();
_tooltip = null;
}
// Display tooltip at 50,50 for 5 seconds
_tooltip = MyTooltip.addToolTip(UiApplication.getUiApplication(), text, xpos1, 270, 1);
}
protected void onUnfocus() {
//lbt.setText("");
invalidate();
super.onUnfocus();
if ( _tooltip != null ) {
// We have displayed a Tooltip - remove it
_tooltip.removeToolTip();
_tooltip = null;
}
}
public int getPreferredWidth() {
return mWidth;
}
public int getPreferredHeight() {
return mHeight;
}
protected void layout(int width, int height) {
setExtent(mWidth, mHeight);
}
}
When a field is in the 'active' or 'pressed' state its visual state is set to Field.VISUAL_STATE_ACTIVE. If you check for this in your paint() method by calling Field.getVisualState() you'll be able to change how your button is displayed when it's pressed.
hi all The below is my code .i try to remove black border color from popup window .i tried this but still i get small black border .
public class S2SPopup extends PopupScreen
{
public S2SPopup()
{
super(new VerticalFieldManager()
{
public void paint(Graphics g)
{
int a = g.getGlobalAlpha();
int c = g.getColor();
g.setColor(c);
g.drawBitmap(0, 0, HomeScreen.popupimg.getWidth(),HomeScreen.popupimg.getHeight(),HomeScreen.popupimg, 0, 0);
super.paint(g);
}
protected void sublayout(int maxWidth, int maxHeight)
{
super.sublayout( HomeScreen.popupimg.getWidth(),HomeScreen.popupimg.getHeight());
setExtent( HomeScreen.popupimg.getWidth(),HomeScreen.popupimg.getHeight());
}
});
setBackground(BackgroundFactory.createSolidTransparentBackground(Color.WHITE, 0));
VerticalFieldManager vfm_Label = new VerticalFieldManager(VerticalFieldManager.VERTICAL_SCROLL)
{
protected void sublayout(int maxWidth, int maxHeight)
{
super.sublayout(maxWidth,150);
setExtent(maxWidth, 150);
}
};
vfm_Label.setMargin(80, 0, 0, 0);
ButtonField btnclose = new ButtonField("",ButtonField.RIGHT)
{
public void paint(Graphics g)
{
//setBackground(BackgroundFactory.createSolidTransparentBackground(Color.WHITE, 0));
int c = g.getGlobalAlpha();
g.setBackgroundColor(c);
super.paint(g);
}
};
btnclose.setMargin(0, 0, 0, 250);
btnclose.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context)
{
close();
}
});
LabelField s2sLabelField = new LabelField()
{
public void paint(Graphics g)
{
g.setColor(Color.BLUE);
super.paint(g);
}
protected void layout(int width, int height)
{
super.layout(width, 150);
setExtent(width, 150);
}
};
s2sLabelField.setPadding(0, 50, 0, 30);
s2sLabelField.setText("lasjdfljlsjlfj ljsfdl jsflljfiowurnowncnvouern.zvovn ljlsfdjj jlj" +
"jsljfdlj ljsfl sjfl jfjsljdfljslfu jsjf;ujerpljsfdjpn sdflsajf ss23s mail jsldfjlfdju nsfjljljlfjmnn,nsf,n,nlojljlsndf,n,ljnsjfdljjufsn jj" +
"sjfd;jjljlsduflja;sfj ljsldfujrqnfqperiujf.zvnpqrue 33333333333333333333333333333333333333 ");
add(btnclose);
vfm_Label.add(s2sLabelField);
add(vfm_Label);
} //end of constructor
} // end of main screen
Override applyTheme with an empty implementation, and the black border goes away. You can't fix this from the constructor alone:
protected void applyTheme(){}
I doubt that it will correct this, but you may try giving your delegate manager a margin of 0 just to be certain that there isn't one on it and background color is creeping in from the theme. A solution that I have implemented in the past is to create your own custom Screen
public class CustomPopup extends Screen {
public CustomPopup() {
super(new VerticalFieldManager() {
//your custom code or a custom manager class (which is what I did)
);
setBackground(BackgroundFactory.createSolidTransparentBackground(0x000000, 0));
}
protected void sublayout(int width, int height) {
//make this screen take up the entire display
setPosition(0, 0);
setExtent(Display.getWidth(), Display.getHeight());
//and layout the delegate
setPositionDelegate(some_x, some_y);
layoutDelegate(some_width, some_height);
}
}
You'll have to add your own buttons and other graphics to it, but This should at least help point you in the right direction.
I am trying to change the width and height of a BlackBerry BasicEditField.
But its not displaying the BasicEditField as i mention.
HorizontalFieldManager HFMreg =
new HorizontalFieldManager(
HorizontalFieldManager.USE_ALL_WIDTH
| HorizontalFieldManager.USE_ALL_HEIGHT) {
//Override the paint method to draw the background image.
public void paint(Graphics graphics) {
//Draw the registration background image
graphics.drawBitmap(0,0,Display.getWidth(),Display.getHeight(),BMregbg, 0, 0);
super.paint(graphics);
}
};
BEFfirstname = new BasicEditField("","",5,EditField.NO_NEWLINE) {
protected void paint(Graphics graphics) {
graphics.fillRect(0,0,80,25);
graphics.setBackgroundColor(Color.WHITE);
graphics.clear();
super.paint(graphics);
}
protected void layout() {
super.layout(getPreferredWidth(),getPreferredHeight());
setExtent(80,25); //width,height
}
public int getPreferredWidth() {
int fieldWidth = 80; //required width
return fieldWidth;
}
public int getPreferredHeight() {
int fieldHeight = 25; // required height
return fieldHeight;
}
};
//BEFfirstname.setMargin(200,0,0,60);
HFMreg.add(LFfirstname);
HFMreg.add(BEFfirstname);
add(HFMreg);
just an other way round:
BasicEditField BEFfirstname = new BasicEditField("","",5,EditField.NO_NEWLINE);
MyManager obj = new MyManger();
obj.add(BEFfirstname);
add(obj);
class MyManager extends Manager
{
MyManager()
{
super(Manager.USE_ALL_WIDTH | Manager.USE_ALL_HEIGHT);
}
protected void sublayout(int width, int height)
{
Field f = getField(0);
layoutChild(f,80,25);
setPositionChild(f, 10, 10);
}
}
I have created a custom button and i am placing the bunch of Custombuttons in a verticalfieldManager , I have aligned the verticalField Manager in the center. when i am creating a default buttonField then verticalfield Manager is able to align the buttonfield in the center. but when i am assigning custombuttonfield in the verticalField Manager it's not aligning in the center.
here is my custombuttoncode
public CustomButtonField(String label,long style) {
super(style);
this.label = label;
onPicture = Bitmap.getBitmapResource(onPicturePath);
font = getFont();
this.setPadding(5,5, 5, 5);
}
public String getLabel() {
return label;
}
public int getPreferredHeight() {
return onPicture.getHeight();
}
public int getPreferredWidth() {
return onPicture.getWidth();
}
protected void layout(int width , int height) {
setExtent(Math.min(width, Display.getWidth()), Math.min(height,getPreferredHeight()));
}
protected void paint(Graphics graphics) {
int texty =(getHeight()-getFont().getHeight())/2;
if (isFocus()) {
graphics.setColor(Color.BLACK);
graphics.drawBitmap(0, 0, getWidth(), getHeight(),onPicture , 0, 0);
graphics.setColor(Color.WHITE);
graphics.setFont(font);
graphics.drawText(label,0,texty,DrawStyle.ELLIPSIS,getWidth());
} else {
graphics.drawBitmap(0, 0, getWidth(), getHeight(),onPicture , 0, 0);
graphics.setColor(Color.WHITE);
graphics.setFont(font);
graphics.drawText(label,0,texty,DrawStyle.ELLIPSIS,getWidth());
}
}
public boolean isFocusable() {
return true;
}
protected void onFocus(int direction) {
super.onFocus(direction);
invalidate();
}
protected void onUnfocus() {
super.onUnfocus();
invalidate();
}
protected boolean navigationClick(int status, int time) {
fieldChangeNotify(0);
return true;
}
protected boolean keyChar(char character, int status, int time) {
if (character == Keypad.KEY_ENTER) {
fieldChangeNotify(0);
return true;
}
return super.keyChar(character, status, time);
}
}
When you create the button you need to pass the Field.FIELD_HCENTER flag as one of your style flags.
CustomButtonField button = new CustomButtonField("TestLabel", Field.FIELD_HCENTER);