Touch Event in Blackberry? - 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.

Related

I'm using **Libgdx**, input events don't work when using **Multi-OS Engine**

It works fine in Android and Desktop. But when running on iOS, it doesn't respond any input event.
I'm using gdxVersion 1.9.5, multi-os engine 1.2.3
I wrote a test class extends input adapter and override touchDown and touchUp method. But it doesn't respond any of the input events which work fine in Android.
public class TestScreen extends ScreenAdapter implements InputProcessor {
private CardGame game;
private OrthographicCamera guiCam;
public TestScreen(CardGame game){
this.game = game;
guiCam = new OrthographicCamera(Consts.WORLD_WIDTH, Consts.WORLD_HEIGHT);
Gdx.input.setInputProcessor(this);
}
public void draw () {
GL20 gl = Gdx.gl;
gl.glClearColor(1, 0, 0, 1);
gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
guiCam.update();
}
public void render (float delta) {
draw();
}
#Override
public boolean keyDown(int keycode) {
System.out.println("key down");
return true;
}
#Override
public boolean keyUp(int keycode) {
System.out.println("key up");
return true;
}
#Override
public boolean keyTyped(char character) {
System.out.println("key typed");
return true;
}
#Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
System.out.println("touch down");
return true;
}
#Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
System.out.println("touch up");
return true;
}
#Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
System.out.println("touch dragged");
return true;
}
#Override
public boolean mouseMoved(int screenX, int screenY) {
System.out.println("mouse moved");
return true;
}
#Override
public boolean scrolled(int amount) {
System.out.println("scrolled");
return true;
}
}

Blackberry custom ButtonField with icon and text

How would I go about creating a custom ButtonField that includes an icon? I have found several different sources for a ButtonField with a custom background Bitmap, but I want to have a button with an Icon and text under it. I can not find anyt informaiton about this, maybe someone here could help me out?
Cheers
Try this
package com.MYAPP.controls;
import com.MYAPP.bll.Log;
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.Font;
import net.rim.device.api.ui.Graphics;
public class ButtonControl extends Field {
private Bitmap selectedBitmap;
private Bitmap unSelectedBitmap;
private String _label;
protected boolean focus = false;
protected int bWidth = 0;
protected int bHeight = 0;
int padding = 0;
Font font=Font.getDefault();
public ButtonControl(String icon, String label) {
super(Field.FOCUSABLE);
_label = label;
try {
selectedBitmap = Bitmap.getBitmapResource(icon + "_focus.png");
unSelectedBitmap =Bitmap.getBitmapResource(icon + ".png");
padding = 0;
} catch (Exception ex) {
Log.Error(ex, "Cannot load icon {" + icon + "}");
}
bWidth = selectedBitmap.getWidth();
bHeight = selectedBitmap.getHeight();
}
protected void onFocus(int direction) {
focus = true;
invalidate();
}
protected void onUnfocus() {
focus = false;
invalidate();
}
public void drawFocus(Graphics g, boolean on) {
// do nothing... this is handled in paint...
}
protected int moveFocus(int amount, int status, int time) {
// This ensures background is redrawn properly if this control is
// the first or last in a series (eliminate white background issue)
invalidate();
return super.moveFocus(amount, status, time);
}
protected void moveFocus(int x, int y, int status, int time) {
// This ensures background is redrawn properly if this control is
// the first or last in a series (eliminate white background issue)
invalidate();
super.moveFocus(x, y, status, time);
}
int _labelWidth;
int posbitmapX=0;
int x=0;
int y = 0;
protected void paint(Graphics g) {
int oldColor = g.getColor();
Font oldFont = g.getFont();
g.setFont(getTextFont());
x = (getWidth() - bWidth) / 2;
if (focus) {
g.setColor(Color.WHITE);
g.drawBitmap(x, y, bWidth, bHeight, selectedBitmap, 0, 0);
} else {
g.setColor(Color.BLACK);
g.drawBitmap(x, y, bWidth, bHeight, unSelectedBitmap, 0, 0);
}
g.setFont(getTextFont());
_labelWidth = getTextFont().getAdvance(_label);
g.drawText(_label, 0, bHeight + 2, Graphics.HCENTER, getWidth());
g.setColor(oldColor);
g.setFont(oldFont);
}
protected void layout(int width, int height) {
setExtent(getPreferredWidth(), getPreferredHeight());
}
private Font getTextFont() {
return font;
}
public int getPreferredWidth() {
return selectedBitmap.getWidth();
}
public int getPreferredHeight() {
return (bHeight + 2 + font.getHeight() + 2);
}
protected boolean keyChar(char key, int status, int time) {
if (key == Characters.ENTER) {
return this.navigationClick(status, time);
}
return super.keyChar(key, status, time);
}
protected boolean navigationClick(int status, int time) {
fieldChangeNotify(1);
return true;
}
protected void fieldChangeNotify(int context) {
try {
this.getChangeListener().fieldChanged(this, context);
} catch (Exception e) {
Log.Debug("Error responding to field change: " + e);
}
} }

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 Bitmap listener

I have a code similar to the one below, painting over the mapfields this mIcon several times.
How can I add a click listener to this bitmap ? I am using bb 5.0
public Bitmap mIcon;
mIcon = Bitmap.getBitmapResource("pcture1.png");
protected void paint(Graphics g) {
super.paint(g);
mDest = new XYRect(....);
g.drawBitmap(mDest, mIcon, 0, 0);
}
Override BitmapField and modify the isFocusable(), navigationClick(), keyChar(), and trackwheelClick() methods.
public class ImageButtonField extends BitmapField
{
public ImageButtonField(Bitmap image)
{
super(image);
}
public boolean isFocusable()
{
return true;
}
protected boolean navigationClick(int status, int time)
{
fieldChangeNotify(0);
return true;
}
protected boolean trackwheelClick(int status, int time)
{
fieldChangeNotify(0);
return true;
}
protected boolean keyChar(char character, int status, int time)
{
if(Characters.ENTER == character || Characters.SPACE == character)
{
fieldChangeNotify(0);
return true;
}
return super.keyChar(character, status, time);
}
}

CustomButton Field not aligning in the center

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

Resources