set focus on blackberry custom button field - blackberry

i am new in blackberry,
i am using custom Image Button field, like tabs, i have 3 image buttons(like tabs) and 3 screens.All 3 screens have that 3 image buttons, if i click 2 imagebutton it navigate to next screen. It focused first image button only(first tab), but i want, it will focus at second image button, any one can help me here my code,
ImageButtonField.java
public class ImageButtonField extends ButtonField {
Bitmap mNormal;
Bitmap mFocused;
Bitmap mActive;
String text;
int mWidth;
int mHeight;
public ImageButtonField(Bitmap normalImage, Bitmap focusedImage,
Bitmap activeImage,String text) {
super(CONSUME_CLICK);
this.text=text;
mNormal = normalImage;
mFocused = focusedImage;
mActive = activeImage;
mWidth = mNormal.getWidth();
mHeight = mNormal.getHeight();
setMargin(0, 0, 0, 0);
setPadding(0, 0, 0, 0);
setBorder(BorderFactory
.createSimpleBorder(new XYEdges(0, 0, 0, 0)));
setBorder(VISUAL_STATE_ACTIVE, BorderFactory
.createSimpleBorder(new XYEdges(0, 0, 0, 0)));
}
protected void paint(Graphics graphics) {
Bitmap bitmap = null;
switch (getVisualState()) {
case VISUAL_STATE_NORMAL:
bitmap = mNormal;
break;
case VISUAL_STATE_FOCUS:
bitmap = mFocused;
break;
case VISUAL_STATE_ACTIVE:
bitmap = mActive;
break;
default:
bitmap = mNormal;
}
graphics.drawBitmap(0, 0, bitmap.getWidth(), bitmap.getHeight(),
bitmap, 0, 0);
if(text!=null)
{
graphics.setColor(Color.WHITE);
graphics.setFont(getFont().derive(Font.BOLD, 28));
// graphics.
graphics.drawText(text,50, 30);
}
}
public int getPreferredWidth() {
return mWidth;
}
public int getPreferredHeight() {
return mHeight;
}
protected void layout(int width, int height) {
setExtent(mWidth, mHeight);
}
}
HomeScreen.java
public final class HomeScreen extends MainScreen implements FieldChangeListener {
private Bitmap homeImageOn;
private Bitmap activateImageOn;
private Bitmap socialImageOn;
private Bitmap homeImageOff;
private Bitmap activateImageOff;
private Bitmap socialImageOff;
private Bitmap logo;
private Bitmap driveNowTextBgImage;
private Bitmap webLink1Image;
private Bitmap webLink2Image;
ImageButtonField homeBitmapImageResource;
ImageButtonField activateBitmapImageResource;
ImageButtonField socialBitmapImageResource;
ImageButtonField logoBitmapResource;
ImageButtonField driveNowBgImageButtonField;
BitmapField webLink1BitmapResorce;
BitmapField webLink2BitmapResorce;
/**
* Creates a new HomeScreen object
*/
public HomeScreen() {
// Set the displayed title of the screen
HorizontalFieldManager tabHorizantalManager = new HorizontalFieldManager();
VerticalFieldManager contentVerticalManager = new VerticalFieldManager();
homeImageOn = Bitmap.getBitmapResource( "home-on.png");
activateImageOff = Bitmap.getBitmapResource( "activate-off.png");
socialImageOff = Bitmap.getBitmapResource( "social-off.png");
homeImageOff = Bitmap.getBitmapResource( "home-off.png");
activateImageOn = Bitmap.getBitmapResource( "activate-on.png");
socialImageOn = Bitmap.getBitmapResource( "social-on.png");
Bitmap bitmap = Bitmap.getBitmapResource("bg.png");
getMainManager().setBackground(
BackgroundFactory.createBitmapBackground(bitmap));
homeBitmapImageResource = new ImageButtonField(homeImageOff,
homeImageOn, homeImageOff, null);
activateBitmapImageResource = new ImageButtonField(activateImageOff,
activateImageOn, activateImageOff, null);
socialBitmapImageResource = new ImageButtonField(socialImageOff,
socialImageOn, socialImageOff, null);
tabHorizantalManager.add(homeBitmapImageResource);
tabHorizantalManager.add(activateBitmapImageResource);
tabHorizantalManager.add(socialBitmapImageResource);
add(tabHorizantalManager);
socialBitmapImageResource.setChangeListener(this);
activateBitmapImageResource.setChangeListener(this);
homeBitmapImageResource.setChangeListener(this);
}
public void fieldChanged(Field field, int context) {
// TODO Auto-generated method stub
if (field.equals(homeBitmapImageResource)) {
UiApplication.getUiApplication().pushScreen(new HomeScreen());
}
if (field.equals(activateBitmapImageResource)) {
ActivateScreen activateScreen = new ActivateScreen();
UiApplication.getUiApplication().pushScreen(activateScreen);
System.out.println("Button pressed: ");
}
if (field.equals(socialBitmapImageResource)) {
UiApplication.getUiApplication().pushScreen(new SocialScreen());
}
}
}

i got solution for above problem here my solution code,
protected void onDisplay(){
socialImageButtonField.setFocus();
}

Related

Set Background Image in Header in Blackberry?

I want to add header.In header,i want to add background image..and two buttons.. in Blackberry.
so what i have to do??
I have tried following.. but it did not work..
VerticalFieldManager vfm=new VerticalFieldManager();
vfm.setBackground(BackgroundFactory.createBitmapBackground(Bitmap.getBitmapResource("topbar.png")));
setTitle(vfm);
try this -
final Bitmap bg = Bitmap.getBitmapResource("bg.png");
VerticalFieldManager vfm_mid = new VerticalFieldManager(Manager.NO_HORIZONTAL_SCROLL | Manager.NO_HORIZONTAL_SCROLLBAR | Manager.VERTICAL_SCROLL | Manager.VERTICAL_SCROLLBAR | Field.USE_ALL_WIDTH){
public void paint(Graphics graphics) {
graphics.setBackgroundColor(0x040811);
graphics.clear();
graphics.drawBitmap(0, 0, bg.getWidth(),
bg.getHeight(), bg, 0, 0);
super.paint(graphics);
}
};
ButtonField b1=new ButtonField("Button 1");
ButtonField b2=new ButtonField("Button 2");
vfm_mid.add(b1);
vfm_mid.add(b2);
setTitle(vfm_mid);
try this custom header:
public class TopBar extends HorizontalFieldManager implements FieldChangeListener{
int width;
int height;
Bitmap bgBitmap;
Button btnHome;
Bitmap btnImage;
boolean isHome;
MainScreen screenFromNavigate;
LabelField lblTitle;
public TopBar(String title)
{
super(FIELD_HCENTER);
setLayout(title);
}
public void setLayout(String title)
{
this.width=Display.getWidth();
this.height=50;
this.bgBitmap= Bitmap.getBitmapResource("topbar.png");
this.isHome = isHomeBtn;
btnHome = new ButtonField("Button 1");
btnHome.setMargin(7, 0, 0, 15);
add(btnHome);
btnHome.setChangeListener(this);
lblTitle = new LabelField(title);
add(lblTitle);
lblTitle.setMargin(12, 0, 0,Display.getWidth()/2-160);
}
protected void sublayout(int maxWidth, int maxHeight)
{
maxWidth=width;
maxHeight=height;
super.sublayout(maxWidth, maxHeight);
setExtent(maxWidth, maxHeight);
}
protected void paint(Graphics graphics)
{
graphics.drawBitmap(0, 0, Display.getWidth(),50, bgBitmap, 0, 0);
// graphics.setBackgroundColor(Color.GRAY);
// graphics.clear();
super.paint(graphics);
}
public void fieldChanged(Field field, int context) {
// TODO Auto-generated method stub
}
}
}
call this:
TopBar topBar = new TopBar("Test");
setTitle(topBar);

How i can Make Status Bar in Blackberry?

i want to set status bar at the bottom of screen and it should display one button left side and one right side. you can see my Screen at below.
my code is like this..
private void BottomLayout()
{
Bitmap topBg = Bitmap.getBitmapResource(ImageName.topbar);
final Bitmap topBg1 = cmn_fun.resizeBitmap(topBg, SCREEN_WIDTH, topBg.getHeight());
HorizontalFieldManager hfmbottom = new HorizontalFieldManager(Field.USE_ALL_WIDTH)
{
protected void paintBackground(Graphics graphics)
{
graphics.drawBitmap(0,0,SCREEN_WIDTH,topBg1.getHeight(), topBg1,0,0 );
super.paint(graphics);
}
protected void sublayout(int maxWidth, int maxHeight) {
// TODO Auto-generated method stub
super.sublayout(topBg1.getWidth(), topBg1.getHeight());
setExtent(topBg1.getWidth(), topBg1.getHeight());
}
};
Bitmap imgprv=Bitmap.getBitmapResource(ImageName.btn_prev);
Bitmap imgprv_sel=Bitmap.getBitmapResource(ImageName.btn_prev_sel);
btn_prev=new CustomButtonField(0, "", imgprv_sel, imgprv, Field.FIELD_LEFT);
hfmbottom.add(btn_prev);
Bitmap imgnext=Bitmap.getBitmapResource(ImageName.btn_next);
Bitmap imgnext_sel=Bitmap.getBitmapResource(ImageName.btn_next_sel);
btn_next=new CustomButtonField(0, "", imgnext_sel, imgnext, Field.FIELD_RIGHT);
hfmbottom.add(btn_next);
setStatus(hfmbottom);
}
Thanks in advance.
I am posting you only the status bar panel.
Actually in the HorizontalFieldManager it does create problem.
Therefore found out the following way and it is working. It does add extra code but it works
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.container.GridFieldManager;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
class SampleBottomPanelScreen extends MainScreen {
private HorizontalFieldManager title_bar;
private VerticalFieldManager btn1_manager;
private ButtonField btn1;
private VerticalFieldManager btn2_manager;
private ButtonField btn2;
Bitmap bg_image;
public SampleBottomPanelScreen() {
//bg_image = Bitmap.getBitmapResource("backgroundImage.png");
btn1_manager = new VerticalFieldManager(VerticalFieldManager.FIELD_LEFT) {
protected void sublayout(int maxWidth, int maxHeight)
{
int displayWidth = Display.getWidth() / 2;
int displayHeight = Display.getHeight();
super.sublayout( displayWidth, displayHeight);
setExtent( displayWidth, displayHeight);
}
};
btn2_manager = new VerticalFieldManager(VerticalFieldManager.FIELD_RIGHT | VerticalFieldManager.USE_ALL_WIDTH) {
protected void sublayout(int maxWidth, int maxHeight)
{
int displayWidth = Display.getWidth() / 2;
int displayHeight = Display.getHeight();
super.sublayout( displayWidth, displayHeight);
setExtent( displayWidth, displayHeight);
}
};
title_bar = new HorizontalFieldManager(Manager.USE_ALL_WIDTH){
public void paint(Graphics graphics) {
graphics.setBackgroundColor(0x2bb1ff);
graphics.clear();
super.paint(graphics);
}
protected void sublayout(int maxWidth, int maxHeight)
{
int displayWidth = Display.getWidth();
int displayHeight = Display.getHeight() / 8;
super.sublayout( displayWidth, displayHeight);
setExtent( displayWidth, displayHeight);
}
};
btn1 = new ButtonField("Submit", ButtonField.LEFT | ButtonField.FIELD_LEFT);
btn2 = new ButtonField("Cancel", ButtonField.RIGHT| ButtonField.FIELD_RIGHT);
btn1_manager.add(btn1);
btn2_manager.add(btn2);
title_bar.add(btn1_manager);
title_bar.add(btn2_manager);
this.add(title_bar);
}
}
try layout manager class
package com.doapps.blackberry.layouts;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Manager;
public class LayoutManager extends Manager
{
int n;
public LayoutManager(int n)
{
super(Manager.NO_VERTICAL_SCROLL);
this.n =n;
}
protected void sublayout(int width, int height)
{
if(n==2)
{
Field first = getField(0);
Field second = getField(1);
layoutChild(first, this.getPreferredWidth(), this.getPreferredHeight());
layoutChild(second, this.getPreferredWidth(), this.getPreferredHeight());
setPositionChild(first,0, getPreferredHeight() -first.getHeight());
setPositionChild(second,getPreferredWidth()-second.getWidth(), getPreferredHeight() -first.getHeight());
setExtent(width, height);
}
if(n==1)
{
Field second= getField(0);
layoutChild(second, this.getPreferredWidth(), this.getPreferredHeight());
setPositionChild(second,getPreferredWidth()-second.getWidth(), getPreferredHeight() -second.getHeight());
setExtent(width, height);
}
}
public int getPreferredHeight() {
return 45;
}
public int getPreferredWidth() {
return Display.getWidth();
}
}
I think you need to use GridFieldManager. Rearrange the following code fragment to your needs and play with paddings ...
private HorizontalFieldManager createRightTab () {
HorizontalFieldManager hMgr = new HorizontalFieldManager(Field.FIELD_RIGHT);
ImageButtonField button = new ImageButtonField ( Bitmap.getBitmapResource(res.button_1), button_w, button_h);
button.setFocusListener(this);
hMgr.add(button);
return hMgr;
}
private HorizontalFieldManager createLeftTab () {
HorizontalFieldManager hMgr = new HorizontalFieldManager(Field.FIELD_LEFT);
ImageButtonField button = new ImageButtonField ( Bitmap.getBitmapResource(res.button_2), button_w, button_h);
button.setFocusListener(this);
hMgr.add(button);
return hMgr;
}
public void GridControlScreen() {
int w = net.rim.device.api.system.Display.getWidth();
gridMgr = new GridFieldManager(1, 3, 0);
gridMgr.setColumnProperty(0, GridFieldManager.FIXED_SIZE, (button_w));
gridMgr.setColumnProperty(1, GridFieldManager.FIXED_SIZE, (w-(2*button_w)));
gridMgr.setColumnProperty(2, GridFieldManager.FIXED_SIZE, (button_w));
HorizontalFieldManager hMgr_a = createLeftTab();
HorizontalFieldManager hMgr_center = new HorizontalFieldManager();
HorizontalFieldManager hMgr_b = createRightTab();
gridMgr.add(hMgr_a);
gridMgr.add(hMgr_center);
gridMgr.add(hMgr_b);
add(gridMgr);
}
Hope this can help.
Try this
private void BottomLayout()
{
Bitmap topBg = Bitmap.getBitmapResource(ImageName.topbar);
final Bitmap topBg1 = cmn_fun.resizeBitmap(topBg, SCREEN_WIDTH, topBg.getHeight());
HorizontalFieldManager hfmbottom = new HorizontalFieldManager(Field.USE_ALL_WIDTH)
{
protected void paintBackground(Graphics graphics)
{
graphics.drawBitmap(0,0,SCREEN_WIDTH,topBg1.getHeight(), topBg1,0,0 );
super.paint(graphics);
}
protected void sublayout(int maxWidth, int maxHeight) {
// TODO Auto-generated method stub
super.sublayout(topBg1.getWidth(), topBg1.getHeight());
setExtent(topBg1.getWidth(), topBg1.getHeight());
}
};
Bitmap imgprv=Bitmap.getBitmapResource(ImageName.btn_prev);
Bitmap imgprv_sel=Bitmap.getBitmapResource(ImageName.btn_prev_sel);
btn_prev=new CustomButtonField(0, "", imgprv_sel, imgprv, Field.FIELD_LEFT);
hfmbottom.add(btn_prev);
Bitmap imgnext=Bitmap.getBitmapResource(ImageName.btn_next);
Bitmap imgnext_sel=Bitmap.getBitmapResource(ImageName.btn_next_sel);
btn_next=new CustomButtonField(0, "", imgnext_sel, imgnext, Field.FIELD_RIGHT);
btn_next.setPadding(0, 0, 0, Display.getWidth()-btn_prev.getWidth()-btn_next.getWidth());
hfmbottom.add(btn_next);
setStatus(hfmbottom);
}
this another way which we can set Field Left and right side.
http://keraisureshvblackberry.blogspot.in/2012/02/there-are-very-common-there-there-are.html

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 add 2 vertical field managers on third vertical field manager in blackberry?

Hi please tell me how to make this screen in blackberry.
my data is not adding on it.i took vertical field manager to add all components.
Its working.. I created it for you. use this code
public class home extends UiApplication {
public static void main(String[] args)
{
home app = new home();
app.enterEventDispatcher();
}
MainScreen screen = new MainScreen();
private int deviceWidth = Display.getWidth();
private int deviceHeight = Display.getHeight();
LabelField lbl1 = new LabelField("label");
final Bitmap backgroundBitmap = Bitmap.getBitmapResource("bg1.jpg");
final Bitmap backgroundBitmap1 = Bitmap.getBitmapResource("bg2.jpg");
final BitmapField mybitmapField = new BitmapField(Bitmap.getBitmapResource("facebook-logo.jpg"),DrawStyle.HCENTER);
public home()
{
super();
pushScreen(screen);
VerticalFieldManager mainManager = new VerticalFieldManager(Manager.NO_VERTICAL_SCROLL | Manager.NO_VERTICAL_SCROLLBAR )
{
public void paint(Graphics graphics)
{
graphics.clear();
graphics.drawBitmap(0, 0, deviceWidth, deviceHeight, backgroundBitmap, 0, 0);
super.paint(graphics);
}
};
//this manger is used for adding the componentes
VerticalFieldManager subManager = new VerticalFieldManager(Manager.NO_VERTICAL_SCROLL | Manager.NO_VERTICAL_SCROLLBAR )
{
public void paint(Graphics graphics)
{
graphics.clear();
graphics.drawBitmap(0, 0, deviceWidth, deviceHeight, backgroundBitmap1, 0, 0);
super.paint(graphics);
}
};
screen.add(mainManager);
mainManager.add(lbl1);
mainManager.add(subManager);
subManager.add(mybitmapField);
subManager.add(new LabelField("Data 1"));
subManager.add(new LabelField("Data 1"));
subManager.add(new LabelField("Data 1"));
}
}
output will be like this.
Find an example of how to make this screen below:
VerticalFieldManager manager = new VerticalFieldManager();
VerticalFieldManager first = new VerticalFieldManager();
VerticalFieldManager second = new VerticalFieldManager();
first.add(label);
second.add(image);
second.add(data);
second.add(data);
second.add(data);
manager.add(first);
manager.add(second);
add(manager);

Reduce height and width of BlackBerry BasicEditField

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

Resources