CustomButtonField is not getting selected - blackberry

I am not able select the custombuttonfield
Here is my code:-
public MenuScreen()
{
super(Screen.DEFAULT_CLOSE);
verticalOffset=Display.getWidth();
menuBackgroundImage=Bitmap.getBitmapResource("com/greetings/Images/MenuBackground.jpg");
categories=new CustomSmallButton(Bitmap.getBitmapResource("com/greetings/ButtonImage/categoryon.png"),Bitmap.getBitmapResource("com/greetings/ButtonImage/categoryoff.png"),0);
categories.setChangeListener(this);
help=new CustomSmallButton(Bitmap.getBitmapResource("com/greetings/ButtonImage/helpon.png"),Bitmap.getBitmapResource("com/greetings/ButtonImage/helpoff.png"),0);
help.setChangeListener(this);
developer=new CustomSmallButton(Bitmap.getBitmapResource("com/greetings/ButtonImage/aboutuson.png"),Bitmap.getBitmapResource("com/greetings/ButtonImage/aboutuspoff.png"),0);
developer.setChangeListener(this);
VerticalFieldManager manager = new VerticalFieldManager()
{
protected void sublayout(int width, int height)
{
width=fwidth;
height=fHeight;
super.sublayout(width,height);
setPositionChild(categories, width-240, height-350);
setPositionChild(help, width-240, height-290);
setPositionChild(developer, width-240, height-230);
setExtent(width, height);
}
};
manager.setBackground(BackgroundFactory.createBitmapBackground(menuBackgroundImage));
manager.add(categories);
manager.add(help);
manager.add(developer);
add(manager);
}
public void fieldChanged(Field field, int context) {
if(field==categories)
{
templateCategories=new TemplateCategories(2);
UiApplication.getUiApplication().pushScreen(templateCategories);
}
if(field== help)
{
helpScreen=new HelpScreen();
UiApplication.getUiApplication().pushScreen(helpScreen);
}
if(field == developer)
{
developerScreen=new DeveloperScreen();
UiApplication.getUiApplication().pushScreen(developerScreen);
}
}
when i am clicking help or developer button its not selecting.

override the
protected void drawFocus(Graphics graphics,boolean on) {
//code for custom focus like draw onFocusbitmap
super.drawFocus(graphics, on);
}
in your CustomSmallButton class

Related

How to set Orientation of Screen in Landscape and Portrait mode in Blackberry

How to Set Position of Components in Portrait Mode.and
when i Change it to Landscape Mode than Position of Components are
Shown in Different Way.
have you tried this?
http://docs.blackberry.com/en/developers/deliverables/11958/Specifying_display_direction_of_screen_647783_11.jsp
You can use subLayout() Method of MainSceen for this.
try this
protected void sublayout(final int width, final int height) {
switch (Display.getOrientation()) {
case Display.ORIENTATION_LANDSCAPE:
{
do something
}
break;
case Display.ORIENTATION_PORTRAIT:
{
do something
}
break;
}
super.sublayout(width, height);
}
Here is code for change Orientation of screen
This is my LoadingScreen.
Here StartUp is another className.where we make ScreenOrientation Method.
public class LoadingScreen extends MainScreen
{
public LoadingScreen()
{
createGUI();
}
protected void sublayout(int width, int height)
{
StartUp.screenOrientation();
if(StartUp.isLandscape)
{
deleteAll();
createGUI();
invalidate();
}
else
{
deleteAll();
orientGUI();
invalidate();
}
super.sublayout(width,height);
}
public void createGUI()
{
//For LANDSCAPE Display;
}
public void orientGUI()
{
//For PORTRAIT Display;
}
}
The ScreenOrientation Method:
public static void screenOrientation()
{
if(Display.getOrientation()==Display.ORIENTATION_LANDSCAPE)
{
isLandscape=true;
width=480;
height=360;
}
else
{
isLandscape=false;
width=360;
height=480;
}
}

Splash screen image size

I am creating a splash screen for my BlackBerry app. Right now the image is not properly placed in all the simulators on which I am testing.
What should be the size of my image for the splash screen, so that it fits in all the device sizes?
The main part is, creating Startup Class which extends UiApplication.
This is the StartUp.java
public class StartUp extends UiApplication
{
public static void main(String[]args)
{
StartUp start=new StartUp();
start.enterEventDispatcher();
}
public StartUp()
{
this.pushScreen(new SplashScreen());
invokeLater(new Runnable()
{
public void run()
{
try
{
Thread.sleep(2000);// Sleeps it for few seconds
UiApplication.getUiApplication().popScreen(UiApplication.getUiApplication().getActiveScreen());
pushScreen(new LoadingScreen());
}
catch (Exception e)
{
exceptionHandling(e.getMessage());
}
}
});
}
public static void exceptionHandling(final String exception)
{
UiApplication.getUiApplication().invokeLater(new Runnable()
{
public void run()
{
Dialog.alert(exception);
}
});
}
}
and the SplashScreen.java
public class SplashScreen extends MainScreen
{
Bitmap bitmap=Bitmap.getBitmapResource("loading-screen.png");//This is my company logo;
BitmapField loadingImage=new BitmapField(bitmap);
public SplashScreen()
{
createGUI();
}
private void createGUI()
{
try
{
VerticalFieldManager vertical=new VerticalFieldManager()
{
protected void paint(Graphics g)
{
g.drawBitmap(0, 0,Display.getWidth(),Display.getHeight(), bitmap, 0, 0);
super.paint(g);
}
protected void sublayout(int maxWidth, int maxHeight)
{
super.sublayout(Display.getWidth(),Display.getHeight());
setExtent(Display.getWidth(),Display.getHeight());
}
};
// Nothing to write;
add(vertical);
}
catch (Exception e)
{
StartUp.exceptionHandling(e.getMessage());
}
}
}
and your FirstScreen.java
public class FirstScreen extends MainScreen
{
VerticalFieldManager vertical;
public FirstScreen()
{
createGUI();
}
private void createGUI()
{
setTitle("Loading Screen");
vertical=new VerticalFieldManager()
{
protected void sublayout(int maxWidth, int maxHeight)
{
super.sublayout(Display.getWidth(),Display.getHeight());
setExtent(Display.getWidth(),Display.getHeight());
}
};
add(vertical);
}
public boolean onMenu(int instance)
{
return true;
}
}
Try this you can get.
Just take any image like 400X400 and put that image to your project's res folder.
Then
in splash screen class file you can resize the image with device's height & width.
Bitmap splashImage = Bitmap.getBitmapResource("Splash.png");//your splash screen's name with it's extension if it is PNG then .png & if JPG then .jpg
Bitmap scale = new Bitmap(Display.getWidth(),Display.getHeight());
splashImage.scaleInto(scale, Bitmap.FILTER_BILINEAR);
VerticalFieldManager mainManager = new VerticalFieldManager(
VerticalFieldManager.NO_VERTICAL_SCROLL
| VerticalFieldManager.USE_ALL_HEIGHT
| VerticalFieldManager.USE_ALL_WIDTH) {
public void paint(Graphics g) {
g.drawBitmap(0, 0, scale.getWidth(), scale.getHeight(), scale, 0, 0);
super.paint(g);
}
};
add(mainManager);
//This vertical field can set your converted image as screen background
// Note :- you must have to extends FullScreen insteadOf MainScreen

How can i create a popup for custom Choicefield and call it?

I have created my own simple ChoiceField by extending ObjectChoiceField.. It is like..
public class MyChoiceField extends ObjectChoiceField
{
public MyChoiceField(String label ,Object[] choices)
{
super(label, choices);
}
protected void layout(int width, int height)
{
setMinimalWidth(width/2-62);
super.layout(width, height);
}
public void paint(Graphics graphics)
{
super.paint(graphics);
}
}
Now i want to display the my choices when i select MyChoiceField in custom menu(not the default popup that blackberry provides when user clicks on a ChoiceField)..
How can i achieve it? Sample codes will be appreciable..
Thanks
Try Overriding the method fieldChangeNotify(int context) of ObjectChoiceField like this:
public class MyChoiceField extends ObjectChoiceField
{
public MyChoiceField(String label ,Object[] choices)
{
super(label, choices);
}
protected void layout(int width, int height)
{
setMinimalWidth(width/2-62);
super.layout(width, height);
}
protected void fieldChangeNotify(int context) {
int index = getSelectedIndex();
try{
VerticalFieldManager vfm = new VerticalFieldManager();
vfm.add(new LabelField("[Pop Up Content]\nChoice Field Changed #\n"+choices[index]+" selected."));
PopupScreen popUpScreen = new PopupScreen(vfm){
public boolean onClose()
{
this.close();
return true;
}
};
UiApplication.getUiApplication().pushScreen(popUpScreen);
} catch(Exception exc) {
System.out.println("Exception in choiceField fieldChangeNotify");
}
}
public void paint(Graphics graphics)
{
super.paint(graphics);
}
}
Use MyChoiceField now somewhat like this:
String[] choices; // Keep This Global
VerticalFieldManager vfm = new VerticalFieldManager(Manager.FIELD_VCENTER);
choices = new String[2];
choices[0] = new String("Choice1");
choices[1] = new String("Choice2");
vfm.add(new MyChoiceField("Choise", choices));
Let me know whether this solution works.

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

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