CustomTextField (Does get a StackOverFlow error) - blackberry

Hi i have typed a code but it doesn't seem to work will you let me know what's the error?
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FocusChangeListener;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.component.BasicEditField;
import net.rim.device.api.ui.component.EditField;
import net.rim.device.api.ui.container.VerticalFieldManager;
public class CustomTextField extends VerticalFieldManager implements FocusChangeListener{
private int textWidth=0;
private Font font=Font.getDefault();
private int textHeight=0;
public EditField basicEditField;
public CustomTextField(int width,int height) {
textWidth=width;
textHeight=height;
VerticalFieldManager vfm=new VerticalFieldManager(Manager.FOCUSABLE);
basicEditField=new EditField(null,null,200, Field.EDITABLE|Field.FOCUSABLE|BasicEditField.NO_NEWLINE);
basicEditField.setFocusListener(this);
vfm.add(basicEditField);
add(vfm);
}
protected void sublayout(int maxWidth, int maxHeight) {
if(textWidth==0)
{
textWidth=maxWidth;
}
if(textHeight==0)
{
textHeight=maxHeight;
}
super.layout(textWidth, textHeight);
setExtent(textWidth, textHeight);
}
protected void paint(Graphics graphics) {
graphics.setColor(Color.BLACK);
graphics.drawRect(basicEditField.getLeft(),basicEditField.getTop(), textWidth, textHeight);
}
public void setHeight(int height) {
textHeight= height;
}
public void setWidth(int width) {
textWidth= width;
}
public Font getFont() {
return font;
}
public void setFont(Font font) {
this.font = font;
}
public void focusChanged(Field field, int eventType) {
if(eventType==FOCUS_GAINED)
{
if(field==basicEditField)
{
basicEditField.setCursorPosition(basicEditField.getText().length());
}
}
}
}
The typed thing doesn't get visible on the Screen
I have just created a CustomTextField which extends VerticalFieldManager
inside it
I have added an EditField which gets added in VerticalFieldManager
But it does'nt work
I mean it doesn't show what i type on the screen
What's the error can you check it out
I do invoke the CustomTextField using
CustomTextField ctf=new CustomTextField(100,200):

check
protected void paint(Graphics graphics)
add
super.paint(g);
at the end in it.

Related

wrapContent of Custom HorizontalFieldManager Blackberry

I am developing an app in which I need to display products in an expandable list. So I have tried an example which was given in stackoverflow... So, it is working good, but my problem is the divider that will not wrap (expand) based on content expanding, like in screen ...
as you can see, in the screen the date2 field is not displayed properly ..
for that, is there any scrolling facility or can this expand automatically?
Here is my code
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.FontFamily;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.TouchEvent;
import net.rim.device.api.ui.Touchscreen;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.NullField;
import net.rim.device.api.ui.component.SeparatorField;
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 UiMainscreen extends MainScreen implements FieldChangeListener
{
private CustomListField cu_field[];
private Bitmap image=null;
int size=8;
public UiMainscreen() {
VerticalFieldManager vmanager=new VerticalFieldManager(VERTICAL_SCROLL|VERTICAL_SCROLLBAR){
protected void sublayout(int maxWidth, int maxHeight) {
super.sublayout(Display.getWidth(),Display.getHeight());
setExtent(Display.getWidth(),Display.getHeight());
}
};
cu_field=new CustomListField[size];
for(int i=0;i<size;i++){
image=Bitmap.getBitmapResource("sample_"+i+".jpg");
cu_field[i]=new CustomListField("BlackBerry models that had a built-in mobile phone, were the first models that natively ran Java, and transmitted data over the normal 2G cellular network. RIM began to advertise these devices as email-capable mobile phones rather than as 2-way pagers.", image, "jan2011", 100, 100,Color.LIGHTGREEN);
cu_field[i].setChangeListener(this);
vmanager.add(new SeparatorField());
vmanager.add(cu_field[i]);
}
add(vmanager);
}
public void fieldChanged(Field field, int context) {
// TODO Auto-generated method stub
for(int i=0;i<size;i++){
if(field==cu_field[i]){
final int k=i;
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
Dialog.alert("You click on Item No "+k);
}
});
}
}
}
}
class CustomListField extends HorizontalFieldManager{
private Bitmap scale_image;
private int width=0;
private int height=0;
private int background_color=0;
private BitmapField bitmap_field;
private boolean flag=false;
Bitmap logingBg;
public CustomListField(String title, Bitmap image, String date,int image_width,int image_height,int background_color){
super(NO_HORIZONTAL_SCROLL|USE_ALL_WIDTH);
this.background_color=background_color;
width=image_width;
height=image_width;
if(image!=null){
scale_image=new Bitmap(image_width, image_height);
image.scaleInto(scale_image, Bitmap.FILTER_LANCZOS);
bitmap_field=new BitmapField(scale_image);
flag=false;
bitmap_field.setMargin(5, 5, 5, 5);
add(bitmap_field);
}
VerticalFieldManager vmanager=new VerticalFieldManager(USE_ALL_WIDTH|Field.FIELD_VCENTER){
protected void sublayout(int maxWidth, int maxHeight) {
super.sublayout(Display.getWidth(), height);
setExtent(Display.getWidth(), height);
}
};
///////////////////////////////////////////
/* Bitmap logingBg = Bitmap.getBitmapResource("myorderdatebackground.png");
HorizontalFieldManager hfm = new HorizontalFieldManager(USE_ALL_WIDTH);
HorizontalFieldManager hfm2 = new HorizontalFieldManager(Field.FIELD_HCENTER);
ButtonField btnext = new ButtonField("btn");
CustomButtonField btnSignIn = new CustomButtonField(0, "", logingBg,
logingBg, Field.FOCUSABLE, Color.WHITE);
hfm2.add(btnext);
hfm.add(btnSignIn);
hfm.add(hfm2);
vmanager.add(hfm);*/
logingBg = Bitmap.getBitmapResource("myorderdatebackground.png");
HorizontalFieldManager HFM = new HorizontalFieldManager(USE_ALL_WIDTH) {
public void paint(Graphics g) {
//g.setBackgroundColor(Color.BLUE);
g.clear();
g.drawBitmap(0, 0, Display.getWidth(),
logingBg.getHeight(), logingBg, 0, 0);
super.paint(g);
}
};
HorizontalFieldManager lableRegistHFM = new HorizontalFieldManager(
FIELD_VCENTER);
LabelField RegistrationLbl = new LabelField("My Orders",Field.FIELD_HCENTER);
FontFamily fontFamily[] = FontFamily.getFontFamilies();
Font font11 = fontFamily[1].getFont(FontFamily.CBTF_FONT, 12);
font11 = fontFamily[1].getFont(Font.BOLD, 18);
RegistrationLbl.setFont(font11);
RegistrationLbl.setMargin(0, 0, 0, (Display.getWidth() / 3));
lableRegistHFM.add(RegistrationLbl);
HFM.add(lableRegistHFM);
vmanager.add(HFM);
/**************************************************************/
LabelField title_lbl=new LabelField("Title: "+title,Field.NON_FOCUSABLE|DrawStyle.ELLIPSIS);
vmanager.add(title_lbl);
LabelField date_lbl=new LabelField("Date: "+date,Field.NON_FOCUSABLE);
vmanager.add(date_lbl);
LabelField date_lbl3=new LabelField("Date2: "+date,Field.NON_FOCUSABLE);
vmanager.add(date_lbl3);
LabelField date_lbl4=new LabelField("Date2: "+date,Field.NON_FOCUSABLE);
vmanager.add(date_lbl4);
Font fon=title_lbl.getFont();
int title_height=fon.getHeight();
Font font=date_lbl.getFont();
int date_height=font.getHeight();
int pad=title_height+date_height;
title_lbl.setPadding((height-pad)/2, 0, 0, 0);
add(vmanager);
add(new NullField(FOCUSABLE));
}
protected void sublayout(int maxWidth, int maxHeight) {
super.sublayout(Display.getWidth(), height);
setExtent(Display.getWidth(), height);
}
protected void paint(Graphics graphics) {
if(flag)
graphics.setBackgroundColor(background_color);
graphics.clear();
super.paint(graphics);
}
protected void onFocus(int direction) {
super.onFocus(direction);
flag=true;
invalidate();
}
protected void onUnfocus() {
invalidate();
flag=false;
}
protected boolean navigationClick(int status, int time) {
if(Touchscreen.isSupported()){
return false;
}else{
fieldChangeNotify(1);
return true;
}
}
protected boolean touchEvent(TouchEvent message)
{
if (TouchEvent.CLICK == message.getEvent())
{
FieldChangeListener listener = getChangeListener();
if (null != listener)
this.setFocus();
listener.fieldChanged(this, 1);
}
return super.touchEvent(message);
}
}
The issue is here:
protected void sublayout(int maxWidth, int maxHeight) {
super.sublayout(Display.getWidth(), height);
setExtent(Display.getWidth(), height);
}
You're giving manager height less that it needs for content (only image_width).
Also here is possible error:
width=image_width;
height=image_width;
You should use image_height for the height.
And sure you could use scrolling for VerticalManager - add style VERTICAL_SCROLL to constructor. But as user I will find the User Experience (UX) strange in this case.

Custom PopUp to work like tooltip without time out

I need to create a Customised PopUp to work like a ToolTip How can i achieve it
i have worked on a code please check out and tell me where i need improvement
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.PopupScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
public class ToolTip extends PopupScreen{
private static VerticalFieldManager vfm;
private LabelField lbl;
private int xPosition;
private int yPosition;
private String message;
public ToolTip(String message,int xPos,int yPos){
super(vfm);
this.xPosition=xPos;
this.yPosition=yPos;
this.message=message;
vfm=new VerticalFieldManager(){
protected void paint(Graphics graphics) {
graphics.setColor(0x00FFFFFF);
graphics.fillRect(0,0,getWidth(),getHeight());
graphics.setColor(0x00000000);
graphics.drawRect(0,0,getWidth(),getHeight());
super.paint(graphics);
}
};
lbl=new LabelField(message);
vfm.add(lbl);
}
protected void sublayout(int width, int height) {
super.sublayout(width, height);
setExtent(lbl.getPreferredWidth(), lbl.getPreferredHeight());
setPosition(xPosition, yPosition);
}
}
i get an error of NullPointer Exception at super(vfm) because vfm is null when i use it as follows . How can i optimise my code .
ButtonField bf1=new ButtonField("Login"){
protected boolean navigationClick(int status, int time) {
UiApplication.getUiApplication().pushScreen(new ToolTip("Hello ", 50, 50));
return super.navigationClick(status, time);
}
};
add(bf1);
Pass an initialized instance of VerticalFieldManager to super(..) of PopupScreen. Check following code.
class ToolTip extends PopupScreen {
private static VerticalFieldManager vfm = new VerticalFieldManager() {
protected void paint(Graphics graphics) {
graphics.setColor(0x00FFFFFF);
graphics.fillRect(0, 0, getWidth(), getHeight());
graphics.setColor(0x00000000);
graphics.drawRect(0, 0, getWidth(), getHeight());
super.paint(graphics);
}
};
// other codes
public ToolTip(String message, int xPos, int yPos) {
super(vfm);
// other codes
}
protected void sublayout(int width, int height) {
// other codes
}
}
Updated code for custom background support
class ToolTip extends PopupScreen {
private int xPosition;
private int yPosition;
public ToolTip(String message, int xPos, int yPos) {
super(new VerticalFieldManager());
// Define and set background here
// Use - BackgroundFactory.createBitmapBackground(bitmap) for bitmap background
getDelegate().setBackground(BackgroundFactory.createSolidBackground(Color.RED));
// Add other UI Field here
this.xPosition=xPos;
this.yPosition=yPos;
add(new LabelField(message));
// other codes
}
// Empty implementation of this will disable default rounded border.
protected void applyTheme() {
}
protected void sublayout(int width, int height) {
super.sublayout(width, height);
setPosition(xPosition, yPosition);
}
}
You need to use getDelegate().setBackground(..) to set the background. Check the class BackgroundFactory for creating custom background.

How i can set Focus in Custom Horizontal Field Manager and Click event in Blackberry?

Hi, I had Generate custom layouts in my screen, i want change color or image of horizontal Field manager when it focus and also perform Click event of selected horizontal Field manager. For
More reference see screen shot.
this is my code which i had try..
HorizontalFieldManager hfmrow=new HorizontalFieldManager(USE_ALL_WIDTH|HorizontalFieldManager.FOCUSABLE | Field.FOCUSABLE)
{
public void onFocus(int direction)
{
isFocus = true;
super.onFocus(direction);
invalidate();
}
public void onUnfocus()
{
invalidate();
isFocus = false;
super.onUnfocus();
}
public void paint(Graphics g)
{
if(isFocus)
{
hfmrow.setBackground(BackgroundFactory.createSolidBackground(Color.RED));
invalidate();
super.paint(g);
}
else
{
hfmrow.setBackground(BackgroundFactory.createSolidBackground(Color.GREEN));
invalidate();
super.paint(g);
}
}
protected boolean navigationClick(int status, int time)
{
Dialog.alert("You are in hfm"+id);
return true;
}
};
hfmrow.setMargin(0, 10, 0, 10);
hfmrow.add(new NullField(Field.FOCUSABLE));
Any help is appreciate..
Thanx in advance
Here how can you do it this code is for changing color when you are selecting hfm
import net.rim.device.api.system.Display;
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.Manager;
import net.rim.device.api.ui.TouchEvent;
import net.rim.device.api.ui.Touchscreen;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.NullField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;
public class sample extends MainScreen implements FieldChangeListener{
HorizontalFieldManager logInDetailManager=null;
int background_color=0;
public sample() {
logInDetailManager = new HorizontalFieldManager(Manager.USE_ALL_WIDTH|Field.FOCUSABLE){
protected void sublayout(int maxWidth, int maxHeight) {
int height=40;
super.sublayout(Display.getWidth(), height);
setExtent(Display.getWidth(), height);
}
protected void paint(Graphics graphics) {
graphics.setBackgroundColor(background_color);
graphics.clear();
invalidate();
super.paint(graphics);
}
protected void onFocus(int direction) {
super.onFocus(direction);
background_color=Color.RED;
invalidate();
}
protected void onUnfocus() {
invalidate();
background_color=Color.GREEN;
}
protected boolean navigationClick(int status, int time) {
if(Touchscreen.isSupported()){
return false;
}else{
fieldChangeNotify(1);
return true;
}
}
protected boolean touchEvent(TouchEvent message)
{
if (TouchEvent.CLICK == message.getEvent())
{
FieldChangeListener listener = getChangeListener();
if (null != listener)
this.setFocus();
listener.fieldChanged(this, 1);
}
return super.touchEvent(message);
}
};
logInDetailManager.setChangeListener(this);
logInDetailManager.add(new LabelField("hello"));
logInDetailManager.add(new NullField(Field.FOCUSABLE));
add(logInDetailManager);
add(new LabelField("good",Field.FOCUSABLE));
}
public void fieldChanged(Field field, int context) {
if(field==logInDetailManager){
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
Dialog.inform("Hi how are you?");
}
});
}
}
}
Use onFocus() and onUnfocus to maintain the focus to your HorizontalFieldManager. and use navigationClick() to click on your hfm . but make sure that you havo to set Field.FOCUSABLE in your HorizontalFieldManager.
take one boolean to check the focus status and do the code in paint method. check below code ..
`
boolean _isFocus = false;
HorizontalFieldManager hfm = new HorizontalFieldManager(HorizontalFieldManager.FOCUSABLE | Field.FOCUSABLE)
{
public void onFocus(int direction)
{
_isFocus = true;
super.onFocus(direction);
}
public void onUnfocus()
{
_isFocus = false;
super.onUnfocus();
}
public void paint(Graphics g)
{
if(_isFocus)
{
// replace your bitmap image or bgcolor here
hfm.invalidate();
super.paint(g);
}
else
{
// replace your bitmap image or bgcolor here
hfm.invalidate();
super.paint(g);
}
}
protected boolean navigationClick(int status, int time)
{
Dialog.alert("You are in hfm");
return true;
}
}`
hope it works !!!

How to get the field on focus in blackberry

Hello Sir I m making an app in which i got stuck in a problem please help me out ...
in one page i m getting the data from server and displaying that data on the page now i m use Focusable for highlighting them ...now my requirement is that when focus come on any data the it should change the field ie exmple: printing the name on which focus is present ..
following is the code which is showing the data on the page now it is working on field change where i need to click the label field ....
package com.Tally.app;
import com.Tally.screenParams.BankList;
import com.Tally.screenParams.CrDrList;
import com.Tally.screenParams.SearchList;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.DrawStyle;
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.Manager;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.BackgroundFactory;
public class LowerCreditors extends VerticalFieldManager implements
FieldChangeListener {
// class Lower extends
static LabelField[] CrDrLabels;
public LowerCreditors() throws Exception {
super(VerticalFieldManager.VERTICAL_SCROLL);
CrDrLabels = new LabelField[CrDrList.VendorNameArr.length];
for (int i = 0; i < CrDrLabels.length; i++) {
final int t = i;
VerticalFieldManager hfm = new VerticalFieldManager(USE_ALL_WIDTH) {
protected void sublayout(int Width, int Height) {
// TODO Auto-generated method stub
super.sublayout(Width, Height);
setPositionChild(getField(0), 15, 0);
setPositionChild(getField(1), Display.getWidth()
- getFont().getAdvance(CrDrList.VendorValArr[t])
- 10, 0);
}
};
CrDrLabels[i] = new LabelField(CrDrList.VendorNameArr[i],
LabelField.FOCUSABLE ) {
public int getPreferredWidth()
{
return 210; // fix width
}
protected boolean navigationClick(int status, int time) {
fieldChanged(this, 0);
return true;
};
};
LabelField Value = new LabelField(CrDrList.VendorValArr[i]);
hfm.add(CrDrLabels[i]);
hfm.add(Value);
add(hfm);
}
}
protected void paintBackground(Graphics g) {
// TODO Auto-generated method stub
g.setColor(0xB8E0B8);
g.fillRect(0, 0, net.rim.device.api.system.Display.getWidth(),
net.rim.device.api.system.Display.getHeight() + 2000);
g.setColor(Color.BLUE);
super.paintBackground(g);
}
protected void paint(Graphics graphics) {
graphics.setColor(0xB8E0B8);
graphics.fillRect(0, 0, Display.getWidth(), 20);
graphics.setColor(Color.BLACK);
super.paint(graphics);
}
protected void sublayout(int maxWidth, int maxHeight) {
// TODO Auto-generated method stub
setExtent(net.rim.device.api.system.Display.getWidth(),
net.rim.device.api.system.Display.getHeight());
super.sublayout(net.rim.device.api.system.Display.getWidth(),
net.rim.device.api.system.Display.getHeight());
}
public void fieldChanged1(Field field, int context) {
// TODO Auto-generated method stub
}
protected boolean abc(int status, int time) {
navigationClick(1, 1);
Dialog.alert("Called......");
return false;
}
public void fieldChanged(Field field, int context) {
// TODO Auto-generated method stub
for (int i = 0; i < CrDrList.VendorNameArr.length; i++) {
if (field == CrDrLabels[i]) {
String LN = CrDrLabels[i].getText();
// SearchList.cname=cn;
CrDrList.cname1 = LN;
LabelField cname1 = CrDrLabels[i];
System.out.println("cnnnnnnnnnnnnnn : "+ cname1);
CrDrList.selLad = CrDrLabels[i];
System.out.println("Creddddddddddddddrrrrrrr"+CrDrList.selLad);
abc(i, i);
}
}
}
}
waitng for reply ..
Couple things you could do:
You can set a FocusChangeListener on each Field and do whatever needs done in the focusChanged() method.
Use getFieldWithFocus() or getLeafWithFocus()
Override each LabelField's onFocus() method
Edit for comment
Overriding onFocus() on your LabelField. Just add this to the other methods you're overriding:
protected void onFocus(int direction) {
//do things that need done when the label gets focus
}
And if it needs to do something when it loses focus
protected void onUnfocus() {
//do things that need done when the label loses focus
}
on navigation movment try this.
protected boolean navigationMovement(int dx, int dy, int status, int time){
System.out.println("field name with focus is...."+getLeafFieldWithFocus());
return super.navigationMovement(dx,dy,status,time);
}

BlackBerry customized HorizontalFieldManager

In Blackberry I want to create a class that extends HorizontalFieldManager so that I can display a label and an image on the same line with the label to the left and the image to the right.
I want the user to be able to interact with the HorizontalFieldManager as if it were a single field. I also want each HorizontalFieldManager to be focusable when added to a VeriticalFieldManager. I also want the clcik action events.
Sounds like you'll be wanting to start writing your own Field classes, here's an example to get you started:
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Graphics;
public class SimpleField extends Field {
private String label;
private Bitmap image;
private int fieldWidth;
private int fieldHeight;
private boolean hover = false;
private int focusColor = 0xcccccc;
public SimpleField(String label, Bitmap image) {
super(Field.FOCUSABLE);
this.label = label;
this.image = image;
fieldWidth = Display.getWidth();
fieldHeight = image.getHeight();
}
protected void onFocus(int direction) {
hover = true;
invalidate();
super.onFocus(direction);
}
protected void onUnfocus() {
hover = false;
invalidate();
super.onUnfocus();
}
public int getPreferredWidth() {
return fieldWidth;
}
public int getPreferredHeight() {
return fieldHeight;
}
protected void layout(int width, int height) {
setExtent(fieldWidth, fieldHeight);
}
protected void paint(Graphics graphics) {
if(hover){
graphics.setColor(focusColor);
graphics.fillRect(0, 0, fieldWidth, fieldHeight);
}
graphics.drawText(label, 0, (fieldHeight - graphics.getFont().getHeight()) / 2);
graphics.drawBitmap(graphics.getFont().getAdvance(label), 0, image.getWidth(), image.getHeight(), image, 0, 0);
}
}

Resources