CustomButton Field not aligning in the center - blackberry

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);

Related

Not able to set the focus on a HorizontalFieldManager

I am not able to set the focus on the HorizontalFieldManager having two LabelFields. I want to highlight the HorizontalFieldManager when it gets focus. I am using the following code, but it is not working.
HorizontalFieldManager hrzMgrTimeLabel = new HorizontalFieldManager(
Manager.USE_ALL_HEIGHT|Manager.FOCUSABLE) {
protected void onFocus(int direction) {
focussed = true;
invalidate();
super.onFocus(direction);
}
protected void onUnfocus() {
focussed = false;
invalidate();
super.onUnfocus();
}
protected void paint(Graphics g) {
g.setBackgroundColor(0x646060);
if (focussed) {
g.setColor(Color.BLUE);
}
g.clear();
super.paint(g);
}
};
hrzMgrTimeLabel.add(a);
hrzMgrTimeLabel.add(b);
horizontalFieldManager_left15.add(hrzMgrTimeLabel);
Here is the LabelField implementation.
final LabelField a= new LabelField("") {
protected void paint(Graphics graphics) {
graphics.setColor(Color.WHITE);
graphics.setBackgroundColor(0x646060);
graphics.clear();
super.paint(graphics);
}
};
final LabelField b= new LabelField("") {
protected void paint(Graphics graphics) {
graphics.setColor(Color.WHITE);
graphics.setBackgroundColor(0x646060);
graphics.clear();
super.paint(graphics);
}
};
The getFocusRect() function might give you the answer.
It gives the extent for which the focus is applied in Manager and then the Graphics.paint() method or Border class can be used.
You need to at least one focusable field on the HorizontalFieldManager. The following line will do that.
hrzMgrTimeLabel.add(new NullField(NullField.FOCUSABLE));
Check the following code.
HorizontalFieldManager hrzMgrTimeLabel = new HorizontalFieldManager(Manager.USE_ALL_HEIGHT | Manager.FOCUSABLE) {
protected void paint(Graphics g) {
g.setBackgroundColor(isFocus() ? Color.BLUE : 0x646060);
g.clear();
super.paint(g);
}
protected void onFocus(int direction) {
invalidate();
super.onFocus(direction);
}
protected void onUnfocus() {
invalidate();
super.onUnfocus();
}
protected boolean navigationMovement(int dx, int dy, int status, int time) {
invalidate();
return super.navigationMovement(dx, dy, status, time);
}
protected boolean navigationClick(int status, int time) {
invalidate();
return super.navigationClick(status, time);
}
protected boolean navigationUnclick(int status, int time) {
invalidate();
return super.navigationUnclick(status, time);
}
};
final LabelField a = new LabelField("First Label Field") {
protected void paint(Graphics graphics) {
graphics.setColor(Color.WHITE);
super.paint(graphics);
}
};
final LabelField b = new LabelField("Second Label Field") {
protected void paint(Graphics graphics) {
graphics.setColor(Color.WHITE);
super.paint(graphics);
}
};
hrzMgrTimeLabel.add(new NullField(NullField.FOCUSABLE));
hrzMgrTimeLabel.add(a);
hrzMgrTimeLabel.add(b);
Note
onFocus(...), onUnfocus(), navigationMovement(...), navigationClick(...), navigationUnclick(...) - these methods were just overrided for refreshing the drawing.
If you want to focus your label field then use final LabelField a= new LabelField("".FOCUSABLE).

How to setBackground of Field in Blackberry 4.5?

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;
}

BlackBerry: Button with Images for normal, focused and pressed state

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.

How to highlight button when get focus on it in blackberry?

I want to make a blackberry button with images, the one image should be shown when focused and another when unfocused.
or even you can say a blue color when focus is off(by default) and red color when focus is on, how can I achieve such thing ?
see my custom class
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 Font fieldFont;
private boolean btn_text = true;
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, String focusedpath){
this(_text, onpath, offpath);
}
public MyButtonField(String _text, String onpath, String offpath, boolean btn_text) {
this(_text, onpath , offpath);
this.btn_text = btn_text;
}
public MyButtonField(String _text, String onpath, String offpath, boolean btn_text, int style, int size)
{
this(_text, onpath , offpath, btn_text);
fieldFont = FieldFont(style, size);
}
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.PLAIN, 14);
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
return null;
}
public static Font FieldFont(int style, int size)
{
try {
FontFamily theFam = FontFamily.forName("SYSTEM");
return theFam.getFont(style, size);
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
return null;
}
protected void drawFocus(Graphics graphics, boolean on) {
if (on) {
//graphics.setColor(Color.RED);
int width = getWidth();
int height = getHeight();
graphics.drawBitmap(0, 0, fieldWidth, fieldHeight, button, 0, 0);
//Draw a border around the field.
/*
graphics.fillRoundRect(0, 0, 3, height,10,10); //Left border
graphics.fillRoundRect(0, 0, width, 3,10,10); //Top border
graphics.fillRoundRect(width-3, 0, 3, height,10,10); //Right border
graphics.fillRoundRect(0, height-3, width, 3,10,10); //Bottom border
*/
graphics.setColor(Color.GRAY);
}
invalidate();
}
public void setFocusImage()
{
button = on;
}
public void setUnFocusImage()
{
button = off;
}
protected void fieldChangeNotify(int context) {
this.getChangeListener().fieldChanged(this, context);
}
protected void paint(Graphics graphics)
{
graphics.drawBitmap(0, 0, fieldWidth, fieldHeight, button, 0, 0);
graphics.setFont(getFont().derive(Font.PLAIN, 8));
graphics.setColor(Color.LIGHTGRAY);
// graphics.fillRoundRect(0, 0, fieldWidth, fieldHeight, 8, 8);
// graphics.setColor(Color.GRAY);
//graphics.drawRoundRect(0, 0, fieldWidth, fieldHeight, 8, 8);
if(btn_text)
graphics.drawText(text, 10, 5);
}
public String getLabel() {
return text;
}
}
You should override onFocus(), onUnfocus() and paint().
Please refer to Custom Button Field article.
You are probably missing this point:
public boolean isFocusable() {
return true;
}
This should be in your custom Field in order the BB UI framework to understand it is a focusable field. Yes, this is odd, since you're already passing Field.FOCUSABLE in constructor, however just give it a try.
Why are you overriding drawFocus() method. Since you are overriding paint method and onFocus & onUnFocus to change bitmap, you don't need to do anything in drawFocus(). Use paint method to draw anything. Everytime you call Invalide(), it will ask screen/field to repaint itself. So remove all code from drawFocus and add it in paint method.
protected void drawFocus(Graphics graphics, boolean on)
{
// Do nothing
}
protected void paint(Graphics graphics)
{
graphics.drawBitmap(0, 0, fieldWidth, fieldHeight, button, 0, 0);
graphics.setFont(getFont().derive(Font.PLAIN, 8));
graphics.setColor(Color.LIGHTGRAY);
if(btn_text)
graphics.drawText(text, 10, 5);
}
Hope it solves your problem.

Touch Event in Blackberry?

I am developing one blackerry application both touch and non-touch device. I am using custom button in my app. This is my code
package CustomControls;
import net.rim.device.api.ui.*; import net.rim.device.api.ui.component.*; import net.rim.device.api.ui.container.*; import net.rim.device.api.system.*;
public class ImageButton extends Field {
private int width;
private int height;
private Bitmap focusImage;
private Bitmap unfocusImage;
private boolean focusFlag=false;
private Bitmap image;
private String label;
private Font font;
public ImageButton()
{
}
public ImageButton(Bitmap focusImage,Bitmap unfocusImage,int width,int height,long style)
{
super(style);
label="";
this.focusImage=focusImage;
this.unfocusImage=unfocusImage;
image=unfocusImage;
this.width=width;
this.height=height;
}
public ImageButton(String label,Font font,Bitmap focusImage,Bitmap unfocusImage,int width,int height,long style)
{
super(style);
this.label=label;
this.font=font;
this.focusImage=focusImage;
this.unfocusImage=unfocusImage;
image=unfocusImage;
this.width=width;
this.height=height;
}
public int getPreferredHeight()
{
return height;
}
public int getPreferredWidth()
{
return width;
}
protected void onFocus(int direction)
{
image=focusImage;
invalidate();
}
protected void onUnfocus()
{
image=unfocusImage;
invalidate();
}
public void setChangeImage(Bitmap fImage,Bitmap uImage)
{
focusImage=fImage;
unfocusImage=uImage;
image=fImage;
invalidate();
}
protected void drawFocus(Graphics graphics, boolean on)
{
}
protected void layout(int width, int height)
{
setExtent(Math.min( width, getPreferredWidth()),Math.min(height, getPreferredHeight()));
}
protected void paint(Graphics graphics)
{
graphics.drawBitmap(0, 0, getWidth(),getHeight(), image, 0, 0);
if(label.length()>0)
{
graphics.setFont(font);
graphics.drawText(label,(width-(label.length()*2))/2,(height-font.getHeight()));
}
}
protected boolean navigationClick(int status, int time)
{
fieldChangeNotify(1);
return true;
}
protected void fieldChangeNotify(int context)
{
try
{
this.getChangeListener().fieldChanged(this,context);
}
catch (Exception exception)
{
System.out.println("==> Exception in Touch "+exception.toString());
}
}
protected boolean navigationMovement(int dx, int dy, int status,int time)
{
return true;
}
protected boolean touchEvent(TouchEvent message)
{
if (TouchEvent.CLICK == message.getEvent())
{
FieldChangeListener listener = getChangeListener();
if (null != listener)
listener.fieldChanged(this, 1);
}
return super.touchEvent(message);
}
protected boolean trackwheelRoll(int dir, int status, int time)
{
return true;
}
public void setBounds(int xPosition,int yPosition)
{
FieldPosition.setXPosition(this,xPosition);
FieldPosition.setYPosition(this,yPosition);
} }
I don't have problem in testing by using Simulator. But i am not able to navigate to button in Real device. I am using 9780 Blackberry bold device. I dont know where the problem occur
Try to do the following:
1). Remove this stuff:
protected boolean navigationMovement(int dx, int dy, int status,int time)
{
return true;
}
protected boolean trackwheelRoll(int dir, int status, int time)
{
return true;
}
2). Make sure your field IS focusable. As the docs say:
If you want your field to receive the
focus, then you must override
isFocusable to return true.
P.S. Actually you don't need to override the touchEvent(TouchEvent message). Check my another post on this.

Resources