Not read all entered words in customEditField in Blackberry - blackberry

I am using custom EditField which given in Rim samples.
With this editfield, i can input maximum characters in one line.. But the problem is that left most hidden characters are not getting when i am getting from edittext.
Following is my CustomEditField class
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Characters;
import net.rim.device.api.ui.Field;
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.EditField;
public class CustomEditfield extends Manager
{
private final static int DEFAULT_LEFT_MARGIN = 0;
private final static int DEFAULT_RIGHT_MARGIN = 5;
private final static int DEFAULT_TOP_MARGIN = 0;
private final static int DEFAULT_BOTTOM_MARGIN = 0;
private final static int DEFAULT_LEFT_PADDING = 5;
private final static int DEFAULT_RIGHT_PADDING = 5;
private final static int DEFAULT_TOP_PADDING = 0;
private final static int DEFAULT_BOTTOM_PADDING = 0;
private int topMargin = DEFAULT_TOP_MARGIN;
private int bottomMargin = DEFAULT_BOTTOM_MARGIN;
private int leftMargin = DEFAULT_LEFT_MARGIN;
private int rightMargin = DEFAULT_RIGHT_MARGIN;
private int topPadding = DEFAULT_TOP_PADDING;
private int bottomPadding = DEFAULT_BOTTOM_PADDING;
private int leftPadding = DEFAULT_LEFT_PADDING;
private int rightPadding = DEFAULT_RIGHT_PADDING;
private int totalHorizontalEmptySpace = leftMargin + leftPadding + rightPadding + rightMargin;
private int totalVerticalEmptySpace = topMargin + topPadding + bottomPadding + bottomMargin;
private int minHeight = getFont().getHeight() + totalVerticalEmptySpace;
private int width ;
private int height ;
private EditField editField;
private Bitmap _currentPicture;
public CustomEditfield(String label,String initialValue,int maxChars,long style,Bitmap image)
{
super(0);
this._currentPicture=image;
this.width=_currentPicture.getWidth();
this.height=_currentPicture.getHeight()+20;
editField = new EditField(label, initialValue, maxChars,style);
// editField.setPadding(_currentPicture.getHeight()/7, 0, 0, _currentPicture.getWidth()/40);
add(editField);
}
protected void sublayout(int width, int height)
{
Field field = getField(0);
layoutChild(field, this.width - totalHorizontalEmptySpace, this.height - totalVerticalEmptySpace);
setPositionChild(field, leftMargin+leftPadding, topMargin+topPadding);
setExtent(this.width, this.height);
}
public void setTopMargin(int topMargin)
{
this.topMargin = topMargin;
}
public void setBottomMargin(int bottomMargin)
{
this.bottomMargin = bottomMargin;
}
protected void paint(Graphics graphics)
{
// graphics.drawRoundRect(leftMargin, topMargin, width - (leftMargin+rightMargin), height - (topMargin+bottomMargin), 5, 5);
graphics.drawBitmap(leftMargin, topMargin, width - (leftMargin+rightMargin), height - (topMargin+bottomMargin), _currentPicture, 5, 0);
EditField ef = (EditField)getField(0);
String entireText = ef.getText();
String textToDraw = "";
Font font = getFont();
int availableWidth = width - totalHorizontalEmptySpace;
if (font.getAdvance(entireText) <= availableWidth)
{
textToDraw = entireText;
}
else
{
int endIndex = entireText.length();
for (int beginIndex = 1; beginIndex < endIndex; beginIndex++)
{
textToDraw = entireText.substring(beginIndex);
if (font.getAdvance(textToDraw) <= availableWidth)
{
break;
}
}
}
ef.setText(textToDraw);
super.paint(graphics);
}
public int getPreferredWidth()
{
return width;
}
public int getPreferredHeight()
{
return height;
}
protected boolean keyChar(char ch, int status, int time)
{
if (ch == Characters.ENTER)
{
return true;
}
else
{
return super.keyChar(ch, status, time);
}
}
public String getText()
{
return ((EditField)getField(0)).getText();
}
public void setText(final String text)
{
((EditField)getField(0)).setText(text);
}
}
------ In my screen class---
et_fname = new CustomEditfield("", "", 50, EditField.NO_NEWLINE
| EditField.FOCUSABLE, txt_bg);
So when i called
et_fname.getText().toString().trim()
It will return only visible characters.
So how can i get all entered characters.
If any one has idea,please help..
Help will be appreciated.

Look at your paint method. In there you truncate the EditField so that it only contains the text that will fit. So what did you expect to happen?
You might think you can save the full text, then call super.paint() and the restore the full text, but that is actually gong to cause you other problems. A setText() on an EditField will tell the field that it has been updated and so it will try to repaint itself. So you will truncate, which will flag up to the EditField it needs to be painted, call super.paint, replace, which will flag up a paint required again, and then the paints for the EditField will kick in and that will try to paint themselves, I suspect on the way invoking the paint() for the Manager which will get recursive and messy.
This approach to painting only the visible are in the Manager is flawed and will never work.
What you seem to be trying to do is a single line EditField that scrolls left and right, The more normal approach to doing this is to have a full width HorizontalFieldManager add the EdtiField to that and have the HFM scroll the EditField left and right. I have not looked for sample code that does this, but I suspect you will find some if you look round. So have a go at that option and let us know how you get on.

According to Peter Strange suggestion,
I found new solution for Single line editfield which have solved my actual problem.
Following is my CustomEditField class for Single line editfield with horizontal scrolling.
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Field;
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.container.HorizontalFieldManager;
public class CustomEditfield extends HorizontalFieldManager
{
private int managerWidth;
private int managerHeight;
BasicEditField searchEdit;
private Bitmap _currentPicture;
private int width ;
private int height ;
private final static int DEFAULT_LEFT_MARGIN = 0;
private final static int DEFAULT_RIGHT_MARGIN = 5;
private final static int DEFAULT_TOP_MARGIN = 0;
private final static int DEFAULT_BOTTOM_MARGIN = 0;
private final static int DEFAULT_LEFT_PADDING = 5;
private final static int DEFAULT_RIGHT_PADDING = 5;
private final static int DEFAULT_TOP_PADDING = 0;
private final static int DEFAULT_BOTTOM_PADDING = 0;
private int topMargin = DEFAULT_TOP_MARGIN;
private int bottomMargin = DEFAULT_BOTTOM_MARGIN;
private int leftMargin = DEFAULT_LEFT_MARGIN;
private int rightMargin = DEFAULT_RIGHT_MARGIN;
private int topPadding = DEFAULT_TOP_PADDING;
private int bottomPadding = DEFAULT_BOTTOM_PADDING;
private int leftPadding = DEFAULT_LEFT_PADDING;
private int rightPadding = DEFAULT_RIGHT_PADDING;
private int totalHorizontalEmptySpace = leftMargin + leftPadding + rightPadding + rightMargin;
private int totalVerticalEmptySpace = topMargin + topPadding + bottomPadding + bottomMargin;
public CustomEditfield(String label,String initialValue,int maxChars,long style,Bitmap image)
{
super(Manager.NO_HORIZONTAL_SCROLL);
this._currentPicture=image;
this.width=_currentPicture.getWidth();
this.height=_currentPicture.getHeight()+20;
searchEdit = new BasicEditField(label, initialValue, maxChars,style){
public int getPreferredHeight()
{
return _currentPicture.getHeight();
}
public int getPreferredWidth()
{
return _currentPicture.getWidth();
}
// public void paint(Graphics g)
// {
// getManager().invalidate();
// super.paint(g);
// }
};
searchEdit.setPadding(_currentPicture.getHeight()/7, 0, 0, 0);
HorizontalFieldManager hfm = new HorizontalFieldManager(Manager.HORIZONTAL_SCROLL)
{
public void sublayout(int width, int height)
{
if (managerWidth == 0) {
managerWidth = searchEdit.getPreferredWidth();
}
if (managerHeight == 0) {
managerHeight = searchEdit.getPreferredHeight();
}
super.sublayout(managerWidth, managerHeight);
setExtent(managerWidth,managerHeight);
}
public void paint(Graphics g) {
super.paint(g);
}
};
// searchEdit.setMaxSize(70);
hfm.add(searchEdit);
add(hfm);
}
public int getPreferredHeight()
{
return _currentPicture.getHeight();
}
public String getText()
{
return searchEdit.getText();
}
public void setText(final String text)
{
searchEdit.setText(text);
}
protected void sublayout(int maxWidth, int maxHeight)
{
Field currField;
currField = this.getField(0);
this.setPositionChild(currField,leftMargin+leftPadding, topMargin+topPadding);
this.layoutChild(currField, this.width - totalHorizontalEmptySpace, this.height - totalVerticalEmptySpace);
setExtent(this.width, this.height);
}
protected void paint(Graphics graphics)
{
graphics.drawBitmap(0, 0, getWidth(), getHeight(), _currentPicture, 0, 0);
// graphics.drawBitmap(leftMargin, topMargin, width - (leftMargin+rightMargin), height - (topMargin+bottomMargin), _currentPicture, 5, 0);
super.paint(graphics);
// graphics.drawRect(0, 0, this.getPreferredWidth(), this.getPreferredHeight());
}
}
I have found this solution from Blackberry - Custom EditField Cursor
I am giving this so it can help others.

the problem is when you typed character once width of field occupied with characters. it will take new character and drop old characters in field. so when you get data inside field you only get characters available on the fields.
you can reproduce this by following steps:
1. type atleast 8 characters.
2. prees back key.
3. then come to 1st character it is not that character that you entered.

Related

Vertical Scroll TextField in Blackberry

I am new to Blackberry developement.I want do the custom textfield in my application.
I write a code for that But I want to scroll the text in that text field (if me text in textfield extends above 4 line.)
I use manager class to add vertical scroll but cant get it.
TextField textfield1=new TextField(Manager.VERTICAL_SCROLL);
Anybody know how to give vertical scroll to the text field.
Try the following code:
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.component.TextField;
import net.rim.device.api.ui.container.VerticalFieldManager;
public class MyTextField extends VerticalFieldManager {
private int fieldWidth;
private int fieldHeight;
private TextField textField;
public MyTextField(int width, int height) {
super(Manager.NO_VERTICAL_SCROLL);
fieldWidth = width;
fieldHeight = height;
VerticalFieldManager vfm = new VerticalFieldManager(Manager.VERTICAL_SCROLL|Manager.VERTICAL_SCROLLBAR);
textField = new TextField() {
public void paint(Graphics g) {
getManager().invalidate();
super.paint(g);
}
};
vfm.add(textField);
add(vfm);
}
public void paint(Graphics g) {
// draw the border of the text area;
int color = g.getColor();
g.setColor(0x00FFCC);
g.drawRect(0, 0, fieldWidth, fieldHeight);
g.setColor(color);
super.paint(g);
}
public void sublayout(int width, int height) {
if (fieldWidth == 0) {
fieldWidth = width;
}
if (fieldHeight == 0) {
fieldHeight = height;
}
super.sublayout(fieldWidth, fieldHeight);
setExtent(fieldWidth,fieldHeight);
}
public String getText() {
return textField.getText();
}
public void setText(String text) {
textField.setText(text);
}
}
add it to your Screen or Manager by specifying the size like this:
add(new MyTextField(200, 200));
Create
HorizontalFieldManager HorizontalFieldManager=new HorizontalFieldManager(Manager.VERTICAL_SCROLL);
Then
TextField textfield1=new TextField();
HorizontalFieldManager.add(textfield1);
add(HorizontalFieldManager);
you can use custom textbox as bellow.
public class CustomTextBox extends Manager
{
/**
* Default margins
*/
private final static int DEFAULT_LEFT_MARGIN = 10;
private final static int DEFAULT_RIGHT_MARGIN = 10;
private final static int DEFAULT_TOP_MARGIN = 5;
private final static int DEFAULT_BOTTOM_MARGIN = 5;
/**
* Default paddings
*/
private final static int DEFAULT_LEFT_PADDING = 10;
private final static int DEFAULT_RIGHT_PADDING = 10;
private final static int DEFAULT_TOP_PADDING = 5;
private final static int DEFAULT_BOTTOM_PADDING = 5;
/**
* Margins around the text box
*/
private int topMargin = DEFAULT_TOP_MARGIN;
private int bottomMargin = DEFAULT_BOTTOM_MARGIN;
private int leftMargin = DEFAULT_LEFT_MARGIN;
private int rightMargin = DEFAULT_RIGHT_MARGIN;
/**
* Padding around the text box
*/
private int topPadding = DEFAULT_TOP_PADDING;
private int bottomPadding = DEFAULT_BOTTOM_PADDING;
private int leftPadding = DEFAULT_LEFT_PADDING;
private int rightPadding = DEFAULT_RIGHT_PADDING;
/**
* Amount of empty space horizontally around the text box
*/
private int totalHorizontalEmptySpace = leftMargin + leftPadding
+ rightPadding + rightMargin;
/**
* Amount of empty space vertically around the text box
*/
private int totalVerticalEmptySpace = topMargin + topPadding
+ bottomPadding + bottomMargin;
/**
* Minimum height of the text box required to display the text entered
*/
private int minHeight = getFont().getHeight() + totalVerticalEmptySpace;
/**
* Width of the text box
*/
private int width = Display.getWidth();
/**
* Height of the text box
*/
private int height = minHeight;
/**
* Background image for the text box
*/
private EncodedImage backgroundImage;
/**
* Bitmap version of the backgroundImage.
* Needed to reduce the calculation overhead incurred by
* scaling of the given image
* and derivation of Bitmap from the
* EncodedImage every time it is needed.
*/
private Bitmap bitmapBackgroundImage;
/**
* The core element of this text box
*/
private EditField editField;
//private BasicEditField editField;
//private String entireText;
public CustomTextBox()
{
// Let the super class initialize the core
super(0);
// An edit field is the sole field of this manager.
editField = new EditField();
//editField = new CustomEditField();
add(editField);
}
public CustomTextBox(EncodedImage backgroundImage)
{
this();
setBackgroundImage(backgroundImage);
}
public void setSize(int width, int height)
{
boolean isChanged = false;
if (width > 0 // Ignore invalid width
&& this.width != width)
{
this.width = width;
isChanged = true;
}
// Ignore the specified height if it is less
// than the minimum height required to display the text.
if (height > minHeight && height != this.height)
{
this.height = height;
isChanged = true;
}
// If width/height has been changed and background image
// is available, adapt it to the new dimension
if (isChanged == true && backgroundImage != null)
{
bitmapBackgroundImage = getScaledBitmapImage(backgroundImage,
this.width - (leftMargin+rightMargin),
this.height - (topMargin+bottomMargin));
}
}
public void setWidth(int width)
{
if (width > 0 && width != this.width)
{
this.width = width;
// If background image is available, adapt it to the new width
if (backgroundImage != null)
{
bitmapBackgroundImage = getScaledBitmapImage(backgroundImage,
this.width - (leftMargin+rightMargin),
this.height - (topMargin+bottomMargin));
}
}
}
public void setHeight(int height)
{
// Ignore the specified height if it is
// less than the minimum height required to display the text.
if (height > minHeight)
{
this.height = height;
// If background image is available, adapt it to the new width
if (backgroundImage != null)
{
bitmapBackgroundImage = getScaledBitmapImage(backgroundImage,
this.width - (leftMargin+rightMargin),
this.height - (topMargin+bottomMargin));
}
}
}
public void setBackgroundImage(EncodedImage backgroundImage)
{
this.backgroundImage = backgroundImage;
// Consider the height of background image in
// calculating the height of the text box.
// setHeight() does not ensure that specified
// height will be in effect, of course, for valid reasons.
// So derivation of Bitmap image in the setHeight() method is not sure.
setHeight(backgroundImage.getHeight() + topMargin + bottomMargin);
if (bitmapBackgroundImage == null)
{
bitmapBackgroundImage = getScaledBitmapImage(backgroundImage,
this.width - (leftMargin+rightMargin),
this.height - (topMargin+bottomMargin));
}
}
/**
* Generate and return a Bitmap Image scaled according
* to the specified width and height.
*
* #param image EncodedImage object
* #param width Intended width of the returned Bitmap object
* #param height Intended height of the returned Bitmap object
* #return Bitmap object
*/
private Bitmap getScaledBitmapImage(EncodedImage image, int width, int height)
{
// Handle null image
if (image == null)
{
return null;
}
int currentWidthFixed32 = Fixed32.toFP(image.getWidth());
int currentHeightFixed32 = Fixed32.toFP(image.getHeight());
int requiredWidthFixed32 = Fixed32.toFP(width);
int requiredHeightFixed32 = Fixed32.toFP(height);
int scaleXFixed32 = Fixed32.div(currentWidthFixed32, requiredWidthFixed32);
int scaleYFixed32 = Fixed32.div(currentHeightFixed32, requiredHeightFixed32);
image = image.scaleImage32(scaleXFixed32, scaleYFixed32);
return image.getBitmap();
}
protected void sublayout(int width, int height)
{
// We have one and only child - the edit field.
// Place it at the appropriate place.
Field field = getField(0);
layoutChild(field, this.width - totalHorizontalEmptySpace,
this.height - totalVerticalEmptySpace);
setPositionChild(field, leftMargin+leftPadding, topMargin+topPadding);
setExtent(this.width, this.height);
}
public void setTopMargin(int topMargin)
{
this.topMargin = topMargin;
}
public void setBottomMargin(int bottomMargin)
{
this.bottomMargin = bottomMargin;
}
protected void paint(Graphics graphics)
{
// Draw background image if available, otherwise draw a rectangle
if (bitmapBackgroundImage == null)
{
graphics.drawRect(leftMargin, topMargin,
// width - (leftMargin+rightMargin),
// height - (topMargin+bottomMargin));
graphics.drawRoundRect(leftMargin, topMargin,
width - (leftMargin+rightMargin),
height - (topMargin+bottomMargin), 5, 5);
}
else
{
graphics.drawBitmap(leftMargin, topMargin,
width - (leftMargin+rightMargin),
height - (topMargin+bottomMargin),
bitmapBackgroundImage, 0, 0);
}
// Determine the rightward text that can fit into the visible edit field
EditField ef = (EditField)getField(0);
String entireText = ef.getText();
boolean longText = false;
String textToDraw = "";
Font font = getFont();
int availableWidth = width - totalHorizontalEmptySpace;
if (font.getAdvance(entireText) <= availableWidth)
{
textToDraw = entireText;
}
else
{
int endIndex = entireText.length();
for (int beginIndex = 1; beginIndex < endIndex; beginIndex++)
{
textToDraw = entireText.substring(beginIndex);
if (font.getAdvance(textToDraw) <= availableWidth)
{
longText = true;
break;
}
}
}
if (longText == true)
{
// Force the edit field display only the truncated text
ef.setText(textToDraw);
// Now let the components draw themselves
super.paint(graphics);
// Return the text field its original text
ef.setText(entireText);
}
else
{
super.paint(graphics);
}
}
public int getPreferredWidth()
{
return width;
}
public int getPreferredHeight()
{
return height;
}
protected boolean keyChar(char ch, int status, int time)
{
if (ch == Characters.ENTER)
{
return true;
}
else
{
return super.keyChar(ch, status, time);
}
}
public String getText()
{
return ((EditField)getField(0)).getText();
}
public void setText(final String text)
{
((EditField)getField(0)).setText(text);
}
}

How i create new TextArea in Blackberry

i am new in blackberry i want to create a custom textArea. After entering 40 letter's it should change automatically the line. After more lines a vertical scroll option is shown.
please any on send me the code or tell me procedure
package max;
import net.rim.device.api.math.Fixed32;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Characters;
import net.rim.device.api.system.Display;
import net.rim.device.api.system.EncodedImage;
import net.rim.device.api.ui.component.EditField;
import net.rim.device.api.ui.component.PasswordEditField;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.*;
public class TextArea extends VerticalFieldManager implements FocusChangeListener, FieldChangeListener {
/**
* Default margins
*/
private final static int DEFAULT_LEFT_MARGIN = 1;
private final static int DEFAULT_RIGHT_MARGIN =5;
private final static int DEFAULT_TOP_MARGIN = 0;
private final static int DEFAULT_BOTTOM_MARGIN = 0;
/**
* Default paddings
*/
private final static int DEFAULT_LEFT_PADDING = 0;
private final static int DEFAULT_RIGHT_PADDING =0;
private final static int DEFAULT_TOP_PADDING =0;
private final static int DEFAULT_BOTTOM_PADDING=0;
//private static final int TYPE_PASSWORD = 0;
/**
* Margins around the text box
*/
private int topMargin = DEFAULT_TOP_MARGIN;
private int bottomMargin = DEFAULT_BOTTOM_MARGIN;
private int leftMargin = DEFAULT_LEFT_MARGIN;
private int rightMargin = DEFAULT_RIGHT_MARGIN;
/**
* Padding around the text box
*/
private int topPadding = DEFAULT_TOP_PADDING;
private int bottomPadding = DEFAULT_BOTTOM_PADDING;
private int leftPadding = DEFAULT_LEFT_PADDING;
private int rightPadding = DEFAULT_RIGHT_PADDING;
private boolean ignoreChange = false;
private int totalHorizontalEmptySpace = leftMargin + leftPadding
+ rightPadding + rightMargin;
/**
* Amount of empty space vertically around the text box
*/
private int totalVerticalEmptySpace = topMargin + topPadding
+ bottomPadding + bottomMargin;
/**
* Minimum height of the text box required to display the text entered
*/
private int minHeight = getFont().getHeight() + totalVerticalEmptySpace;
/**
* Width of the text box
*/
private int width =Display.getWidth();
/**
* Height of the text box
*/
private int height = minHeight;
/**
* Background image for the text box
*/
private EncodedImage backgroundImage;
private EncodedImage focusedBackgroundImage;
/**
* Bitmap version of the backgroundImage.
* Needed to reduce the calculation overhead incurred by
* scaling of the given image
* and derivation of Bitmap from the
* EncodedImage every time it is needed.
*/
//private int focus_color = AppColors.FOCUS_COLOR;
private int focus_color = AppColors.FOCUS_COLOR;
private Bitmap bitmapBackgroundImage;
private Bitmap focusedBitmapBackgroundImage;
//private EditField editField;
/**
* The core element of this text box
*/
// public static final int TYPE_PASSWORD=2;
// public static final int TYPE_PLAIN=1;
// private BasicEditField editField;
// public TextArea(int type)
// {
// // Let the super class initialize the core
// super(0);
//// this.type = type;
// VerticalFieldManager vfm=new VerticalFieldManager(Manager.VERTICAL_SCROLL);
// if (type!=TYPE_PASSWORD) {
// // An edit field is the sole field of this manager.
// editField = new EditField(FIELD_VCENTER);
// }else{
// editField = new PasswordEditField();
// editField.setMaxSize(12);
// }
// //editField = new CustomEditField();
// vfm.add(editField);
// add(vfm);
// vfm.setFocusListener(this);
//
// }
public EditField objEditField;
public TextArea(int w,int h, EncodedImage backgroundImage, EncodedImage focusedState)
{
this.width=w;
this. height=h;
VerticalFieldManager vfm=new VerticalFieldManager(Manager.VERTICAL_SCROLL);
setFocusedBackgroundImage(backgroundImage);
if(focusedState==null){
focusedState = backgroundImage;
}
setFocusedBackgroundImage(focusedState);
vfm.add(objEditField);
add(vfm);
vfm.setFocusListener(this);
//
objEditField.setFocusListener(this);
}
// private void setFocusedBackgroundImage(EncodedImage focusedState) {
// // TODO Auto-generated method stub
//
// }
public TextArea(int w,int h) {
// TODO Auto-generated constructor stub
super(Manager.NO_HORIZONTAL_SCROLL);
this.width=w;
this.height=h;
VerticalFieldManager vfm=new VerticalFieldManager(Manager.VERTICAL_SCROLL);
objEditField=new EditField()
{
// public void paint(Graphics g)
// {
// getManager().invalidate();
// super.paint(g);
//
// }
};
vfm.add(objEditField);
add(vfm);
}
public void setSize(int width, int height)
{
boolean isChanged = false;
if (width > 0 // Ignore invalid width
&& this.width != width)
{
this.width = width;
isChanged = true;
}
// Ignore the specified height if it is less
// than the minimum height required to display the text.
if (height > minHeight && height != this.height)
{
this.height = height;
isChanged = true;
}
// If width/height has been changed and background image
// is available, adapt it to the new dimension
if (isChanged == true && backgroundImage != null)
{
bitmapBackgroundImage = getScaledBitmapImage(backgroundImage,
this.width - (leftMargin+rightMargin),
this.height - (topMargin+bottomMargin));
focusedBitmapBackgroundImage = getScaledBitmapImage(focusedBackgroundImage,
this.width - (leftMargin+rightMargin),
this.height - (topMargin+bottomMargin));
}
// int actualHeight = getFont().getHeight();
// if(height>actualHeight){
// yTranslate = (height - actualHeight)/2;
// }
}
public void setWidth(int width)
{
if (width > 0 && width != this.width)
{
this.width = width;
// If background image is available, adapt it to the new width
if (backgroundImage != null)
{
bitmapBackgroundImage = getScaledBitmapImage(backgroundImage,
this.width - (leftMargin+rightMargin),
this.height - (topMargin+bottomMargin));
focusedBitmapBackgroundImage = getScaledBitmapImage(focusedBackgroundImage,
this.width - (leftMargin+rightMargin),
this.height - (topMargin+bottomMargin));
}
}
}
public void setHeight(int height)
{
// Ignore the specified height if it is
// less than the minimum height required to display the text.
if (height > minHeight)
{
this.height = height;
// If background image is available, adapt it to the new width
if (backgroundImage != null)
{
bitmapBackgroundImage = getScaledBitmapImage(backgroundImage,
this.width - (leftMargin+rightMargin),
this.height - (topMargin+bottomMargin));
focusedBitmapBackgroundImage = getScaledBitmapImage(focusedBackgroundImage,
this.width - (leftMargin+rightMargin),
this.height - (topMargin+bottomMargin));
}
}
}
public void setBackgroundImage(EncodedImage backgroundImage)
{
this.backgroundImage = backgroundImage;
// Consider the height of background image in
// calculating the height of the text box.
// setHeight() does not ensure that specified
// height will be in effect, of course, for valid reasons.
// So derivation of Bitmap image in the setHeight() method is not sure.
setHeight(backgroundImage.getHeight() + topMargin + bottomMargin);
if (bitmapBackgroundImage == null)
{
bitmapBackgroundImage = getScaledBitmapImage(backgroundImage,
this.width - (leftMargin+rightMargin),
this.height - (topMargin+bottomMargin));
}
}
public void setFocusedBackgroundImage(EncodedImage backgroundImage)
{
this.focusedBackgroundImage = backgroundImage;
// Consider the height of background image in
// calculating the height of the text box.
// setHeight() does not ensure that specified
// height will be in effect, of course, for valid reasons.
// So derivation of Bitmap image in the setHeight() method is not sure.
setHeight(backgroundImage.getHeight() + topMargin + bottomMargin);
if (focusedBitmapBackgroundImage == null)
{
focusedBitmapBackgroundImage = getScaledBitmapImage(backgroundImage,
this.width - (leftMargin+rightMargin),
this.height - (topMargin+bottomMargin));
}
}
private Bitmap getScaledBitmapImage(EncodedImage image, int width, int height)
{
// Handle null image
if (image == null)
{
return null;
}
int currentWidthFixed32 = Fixed32.toFP(image.getWidth());
int currentHeightFixed32 = Fixed32.toFP(image.getHeight());
int requiredWidthFixed32 = Fixed32.toFP(width);
int requiredHeightFixed32 = Fixed32.toFP(height);
int scaleXFixed32 = Fixed32.div(currentWidthFixed32, requiredWidthFixed32);
int scaleYFixed32 = Fixed32.div(currentHeightFixed32, requiredHeightFixed32);
image = image.scaleImage32(scaleXFixed32, scaleYFixed32);
return image.getBitmap();
}
// protected void sublayout(int width, int height)
// {
// // We have one and only child - the edit field.
// // Place it at the appropriate place.
// Field field = getField(0);
// layoutChild(field, this.width - totalHorizontalEmptySpace,
// this.height - totalVerticalEmptySpace);
// setPositionChild(field, leftMargin+leftPadding, topMargin+topPadding);
//
// setExtent(this.width, this.height);
// }
public void paint(Graphics g)
{
// super.paint(g);
// if(isFocus()){
// g.setColor(Color.AQUA);
// }else{
// g.setColor(Color.BLACK);
// }
// //g.drawRect(0, 0, getWidth(), getHeight());
// g.drawRoundRect(leftMargin, topMargin,
// width - (leftMargin+rightMargin),
// height - (topMargin+bottomMargin), 5, 5);
int prevColor = g.getColor();
if (bitmapBackgroundImage == null)
{
//graphics.drawRect(leftMargin, topMargin, width - (leftMargin+rightMargin), height - (topMargin+bottomMargin));
if(isFocus()){
g.setColor(focus_color);
}
else{
g.setColor(Color.BLACK);
}
g.drawRoundRect(leftMargin, topMargin,
this.width - (leftMargin+rightMargin),
this.height - (topMargin+bottomMargin), 5, 5);
g.setColor(Color.BLACK);
invalidate();
}
else
{
if(isFocus()){
g.setColor(Color.AQUA);
// g.setColor(Color.WHITE);
g.drawBitmap(leftMargin, topMargin,
this.width - (leftMargin+rightMargin),
this.height - (topMargin+bottomMargin),
focusedBitmapBackgroundImage, 0, 0);
}else{
g.setColor(Color.BLACK);
g.drawBitmap(leftMargin, topMargin,
this.width- (leftMargin+rightMargin),
this.height - (topMargin+bottomMargin),
bitmapBackgroundImage, 0, 0);
}
}
EditField objEditField=new EditField();
String entireText = objEditField.getText();
boolean longText = false;
String textToDraw = "";
Font font = getFont();
int availableWidth = width - totalHorizontalEmptySpace;
if (font.getAdvance(entireText) <= availableWidth)
{
textToDraw = entireText;
}
else
{
int endIndex = entireText.length();
for (int beginIndex = 1; beginIndex < endIndex; beginIndex++)
{
textToDraw = entireText.substring(beginIndex);
if (font.getAdvance(textToDraw) <= availableWidth)
{
longText = true;
break;
}
}
}
if (longText == true)
{
// Force the edit field display only the truncated text
objEditField.setText(textToDraw);
// Now let the components draw themselves
// graphics.translate(0, yTranslate);
super.paint(g);
// graphics.translate(0, -yTranslate);
// Return the text field its original text
objEditField.setText(entireText);
}
else
{
// graphics.translate(0, yTranslate);
super.paint(g);
// graphics.translate(0, -yTranslate);
}
//g.setColor(Color.WHITE);
g.setColor(prevColor);
}
protected void sublayout(int width1, int height1)
{
// We have one and only child - the edit field.
// Place it at the appropriate place.
if(width==0)
{
width=width1;
}
if(height==0)
{
height=height1;
}
// super.sublayout(this.width - totalHorizontalEmptySpace,this.height - totalVerticalEmptySpace);
// setExtent(this.width, this.height);
Field field = getField(0);
layoutChild(field, this.width - totalHorizontalEmptySpace,
this.height - totalVerticalEmptySpace);
setPositionChild(field, leftMargin+leftPadding, topMargin+topPadding);
setExtent(this.width, this.height);
}
public int getPreferredWidth()
{
return width;
}
public int getPreferredHeight()
{
return height;
}
public void setTopMargin(int topMargin)
{
this.topMargin = topMargin;
}
public void setBottomMargin(int bottomMargin)
{
this.bottomMargin = bottomMargin;
}
// protected boolean keyChar(char ch, int status, int time)
// {
// if (ch == Characters.ENTER)
// {
// return true;
// }
// else
// {
// return super.keyChar(ch, status, time);
// }
// }
public String getText()
{
return objEditField.getText();
}
public void setText(String str)
{
objEditField.setText(str);
}
public void setChangeListner(){
objEditField.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
}
});
}
public void fieldChanged(Field field, int context) {
if (!ignoreChange) {
EditField textField = (EditField) field;
textChanged(textField.getText());
}
}
public void textChanged(String text){
}
public void startListning(){
ignoreChange = false;
}
public void focusChanged(Field field, int eventType) {
invalidate();
}
public void addTextChangeListner(){
objEditField.setChangeListener(this);
}
public void removeTextChangeListner() {
objEditField.setChangeListener(null);
}
}
I thinks this code will help you.

customize Edit field on blackberry

I want to customize Edit Field like in this link http://www.hostingpics.net/viewer.php?id=44669634im.png.
I find this code
------------------------------------------------CustomTextBox---------------------
package mypackage;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.component.EditField;
import net.rim.device.api.ui.component.BasicEditField;
import net.rim.device.api.system.EncodedImage;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.system.Characters;
import net.rim.device.api.math.Fixed32;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Font;
public class CustomTextBox extends Manager
{
/**
* Default margins
*/
private final static int DEFAULT_LEFT_MARGIN = 10;
private final static int DEFAULT_RIGHT_MARGIN = 10;
private final static int DEFAULT_TOP_MARGIN = 5;
private final static int DEFAULT_BOTTOM_MARGIN = 5;
/**
* Default paddings
*/
private final static int DEFAULT_LEFT_PADDING = 10;
private final static int DEFAULT_RIGHT_PADDING = 10;
private final static int DEFAULT_TOP_PADDING = 5;
private final static int DEFAULT_BOTTOM_PADDING = 5;
/**
* Margins around the text box
*/
private int topMargin = DEFAULT_TOP_MARGIN;
private int bottomMargin = DEFAULT_BOTTOM_MARGIN;
private int leftMargin = DEFAULT_LEFT_MARGIN;
private int rightMargin = DEFAULT_RIGHT_MARGIN;
/**
* Padding around the text box
*/
private int topPadding = DEFAULT_TOP_PADDING;
private int bottomPadding = DEFAULT_BOTTOM_PADDING;
private int leftPadding = DEFAULT_LEFT_PADDING;
private int rightPadding = DEFAULT_RIGHT_PADDING;
/**
* Amount of empty space horizontally around the text box
*/
private int totalHorizontalEmptySpace = leftMargin + leftPadding
+ rightPadding + rightMargin;
/**
* Amount of empty space vertically around the text box
*/
private int totalVerticalEmptySpace = topMargin + topPadding
+ bottomPadding + bottomMargin;
/**
* Minimum height of the text box required to display the text entered
*/
private int minHeight = getFont().getHeight() + totalVerticalEmptySpace;
/**
* Width of the text box
*/
private int width = Display.getWidth();
/**
* Height of the text box
*/
private int height = minHeight;
/**
* Background image for the text box
*/
private EncodedImage backgroundImage;
/**
* Bitmap version of the backgroundImage.
* Needed to reduce the calculation overhead incurred by
* scaling of the given image
* and derivation of Bitmap from the
* EncodedImage every time it is needed.
*/
private Bitmap bitmapBackgroundImage;
/**
* The core element of this text box
*/
private EditField editField;
//private BasicEditField editField;
//private String entireText;
public CustomTextBox()
{
// Let the super class initialize the core
super(0);
// An edit field is the sole field of this manager.
editField = new EditField();
//editField = new CustomEditField();
add(editField);
}
public CustomTextBox(EncodedImage backgroundImage)
{
this();
setBackgroundImage(backgroundImage);
}
public void setSize(int width, int height)
{
boolean isChanged = false;
if (width > 0 // Ignore invalid width
&& this.width != width)
{
this.width = width;
isChanged = true;
}
// Ignore the specified height if it is less
// than the minimum height required to display the text.
if (height > minHeight && height != this.height)
{
this.height = height;
isChanged = true;
}
// If width/height has been changed and background image
// is available, adapt it to the new dimension
if (isChanged == true && backgroundImage != null)
{
bitmapBackgroundImage = getScaledBitmapImage(backgroundImage,
this.width - (leftMargin+rightMargin),
this.height - (topMargin+bottomMargin));
}
}
public void setWidth(int width)
{
if (width > 0 && width != this.width)
{
this.width = width;
// If background image is available, adapt it to the new width
if (backgroundImage != null)
{
bitmapBackgroundImage = getScaledBitmapImage(backgroundImage,
this.width - (leftMargin+rightMargin),
this.height - (topMargin+bottomMargin));
}
}
}
public void setHeight(int height)
{
// Ignore the specified height if it is
// less than the minimum height required to display the text.
if (height > minHeight)
{
this.height = height;
// If background image is available, adapt it to the new width
if (backgroundImage != null)
{
bitmapBackgroundImage = getScaledBitmapImage(backgroundImage,
this.width - (leftMargin+rightMargin),
this.height - (topMargin+bottomMargin));
}
}
}
public void setBackgroundImage(EncodedImage backgroundImage)
{
this.backgroundImage = backgroundImage;
// Consider the height of background image in
// calculating the height of the text box.
// setHeight() does not ensure that specified
// height will be in effect, of course, for valid reasons.
// So derivation of Bitmap image in the setHeight() method is not sure.
setHeight(backgroundImage.getHeight() + topMargin + bottomMargin);
if (bitmapBackgroundImage == null)
{
bitmapBackgroundImage = getScaledBitmapImage(backgroundImage,
this.width - (leftMargin+rightMargin),
this.height - (topMargin+bottomMargin));
}
}
/**
* Generate and return a Bitmap Image scaled according
* to the specified width and height.
*
* #param image EncodedImage object
* #param width Intended width of the returned Bitmap object
* #param height Intended height of the returned Bitmap object
* #return Bitmap object
*/
private Bitmap getScaledBitmapImage(EncodedImage image, int width, int height)
{
// Handle null image
if (image == null)
{
return null;
}
int currentWidthFixed32 = Fixed32.toFP(image.getWidth());
int currentHeightFixed32 = Fixed32.toFP(image.getHeight());
int requiredWidthFixed32 = Fixed32.toFP(width);
int requiredHeightFixed32 = Fixed32.toFP(height);
int scaleXFixed32 = Fixed32.div(currentWidthFixed32, requiredWidthFixed32);
int scaleYFixed32 = Fixed32.div(currentHeightFixed32, requiredHeightFixed32);
image = image.scaleImage32(scaleXFixed32, scaleYFixed32);
return image.getBitmap();
}
protected void sublayout(int width, int height)
{
// We have one and only child - the edit field.
// Place it at the appropriate place.
Field field = getField(0);
layoutChild(field, this.width - totalHorizontalEmptySpace,
this.height - totalVerticalEmptySpace);
setPositionChild(field, leftMargin+leftPadding, topMargin+topPadding);
setExtent(this.width, this.height);
}
public void setTopMargin(int topMargin)
{
this.topMargin = topMargin;
}
public void setBottomMargin(int bottomMargin)
{
this.bottomMargin = bottomMargin;
}
protected void paint(Graphics graphics)
{
// Draw background image if available, otherwise draw a rectangle
if (bitmapBackgroundImage == null)
{
graphics.drawRect(leftMargin, topMargin,
width - (leftMargin+rightMargin), height - (topMargin+bottomMargin));
graphics.drawRoundRect(leftMargin, topMargin,
width - (leftMargin+rightMargin),
height - (topMargin+bottomMargin), 5, 5);
}
else
{
graphics.drawBitmap(leftMargin, topMargin,
width - (leftMargin+rightMargin),
height - (topMargin+bottomMargin),
bitmapBackgroundImage, 0, 0);
}
// Determine the rightward text that can fit into the visible edit field
EditField ef = (EditField)getField(0);
String entireText = ef.getText();
boolean longText = false;
String textToDraw = "";
Font font = getFont();
int availableWidth = width - totalHorizontalEmptySpace;
if (font.getAdvance(entireText) <= availableWidth)
{
textToDraw = entireText;
}
else
{
int endIndex = entireText.length();
for (int beginIndex = 1; beginIndex < endIndex; beginIndex++)
{
textToDraw = entireText.substring(beginIndex);
if (font.getAdvance(textToDraw) <= availableWidth)
{
longText = true;
break;
}
}
}
if (longText == true)
{
// Force the edit field display only the truncated text
ef.setText(textToDraw);
// Now let the components draw themselves
super.paint(graphics);
// Return the text field its original text
ef.setText(entireText);
}
else
{
super.paint(graphics);
}
}
public int getPreferredWidth()
{
return width;
}
public int getPreferredHeight()
{
return height;
}
protected boolean keyChar(char ch, int status, int time)
{
if (ch == Characters.ENTER)
{
return true;
}
else
{
return super.keyChar(ch, status, time);
}
}
public String getText()
{
return ((EditField)getField(0)).getText();
}
public void setText(final String text)
{
((EditField)getField(0)).setText(text);
}
}
--------------------------------------------MyScreen------------------------
package mypackage;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;
/**
* A class extending the MainScreen class, which provides default standard
* behavior for BlackBerry GUI applications.
*/
public final class MyScreen extends MainScreen
{
public MyScreen()
{
new CustomTextBox();
}
}
--------------------------------------------MyApp------------------------
package mypackage;
import net.rim.device.api.ui.UiApplication;
/**
* This class extends the UiApplication class, providing a
* graphical user interface.
*/
public class MyApp extends UiApplication
{
public static void main(String[] args)
{
// Create a new instance of the application and make the currently
// running thread the application's event dispatch thread.
MyApp theApp = new MyApp();
theApp.enterEventDispatcher();
}
public MyApp()
{
// Push a screen onto the UI stack for rendering.
pushScreen(new MyScreen());
}
}
But I obtain white Screen what should I change in this code to ibtain the custon edit field
hi you just create object but you forget to add that object to mainscreen
package mypackage;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;
/**
* A class extending the MainScreen class, which provides default standard
* behavior for BlackBerry GUI applications.
*/
public final class MyScreen extends MainScreen
{
public MyScreen()
{
add(new CustomTextBox());//in your code it is like new CustomTextBox();
}
}
if you want to add any image as background to your editbox then you can yous following way
package mypackage;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;
/**
* A class extending the MainScreen class, which provides default standard
* behavior for BlackBerry GUI applications.
*/
public final class MyScreen extends MainScreen
{
public MyScreen()
{
EncodedImage enc_img=EncodedImage.getEncodedImageResource("input-box.png");//image name is 'input-box.png'
CustomTextBox edi_box=new CustomTextBox(enc_img);
add(edi_box);
}
}

Custom multi line textbox in Blackberry

Can any one tell me how to create multi line text box with increasing height with background color.
Thank You
public class CustomTextBox extends Manager
{
/**
* Default margins
*/
private final static int DEFAULT_LEFT_MARGIN = 10;
private final static int DEFAULT_RIGHT_MARGIN = 10;
private final static int DEFAULT_TOP_MARGIN = 5;
private final static int DEFAULT_BOTTOM_MARGIN = 5;
/**
* Default paddings
*/
private final static int DEFAULT_LEFT_PADDING = 10;
private final static int DEFAULT_RIGHT_PADDING = 10;
private final static int DEFAULT_TOP_PADDING = 5;
private final static int DEFAULT_BOTTOM_PADDING = 5;
/**
* Margins around the text box
*/
private int topMargin = DEFAULT_TOP_MARGIN;
private int bottomMargin = DEFAULT_BOTTOM_MARGIN;
private int leftMargin = DEFAULT_LEFT_MARGIN;
private int rightMargin = DEFAULT_RIGHT_MARGIN;
/**
* Padding around the text box
*/
private int topPadding = DEFAULT_TOP_PADDING;
private int bottomPadding = DEFAULT_BOTTOM_PADDING;
private int leftPadding = DEFAULT_LEFT_PADDING;
private int rightPadding = DEFAULT_RIGHT_PADDING;
/**
* Amount of empty space horizontally around the text box
*/
private int totalHorizontalEmptySpace = leftMargin + leftPadding
+ rightPadding + rightMargin;
/**
* Amount of empty space vertically around the text box
*/
private int totalVerticalEmptySpace = topMargin + topPadding
+ bottomPadding + bottomMargin;
/**
* Minimum height of the text box required to display the text entered
*/
private int minHeight = getFont().getHeight() + totalVerticalEmptySpace;
/**
* Width of the text box
*/
private int width = Display.getWidth();
/**
* Height of the text box
*/
private int height = minHeight;
/**
* Background image for the text box
*/
private EncodedImage backgroundImage;
/**
* Bitmap version of the backgroundImage.
* Needed to reduce the calculation overhead incurred by
* scaling of the given image
* and derivation of Bitmap from the
* EncodedImage every time it is needed.
*/
private Bitmap bitmapBackgroundImage;
/**
* The core element of this text box
*/
private EditField editField;
//private BasicEditField editField;
//private String entireText;
public CustomTextBox()
{
// Let the super class initialize the core
super(0);
// An edit field is the sole field of this manager.
editField = new EditField();
//editField = new CustomEditField();
add(editField);
}
public CustomTextBox(EncodedImage backgroundImage)
{
this();
setBackgroundImage(backgroundImage);
}
public void setSize(int width, int height)
{
boolean isChanged = false;
if (width > 0 // Ignore invalid width
&& this.width != width)
{
this.width = width;
isChanged = true;
}
// Ignore the specified height if it is less
// than the minimum height required to display the text.
if (height > minHeight && height != this.height)
{
this.height = height;
isChanged = true;
}
// If width/height has been changed and background image
// is available, adapt it to the new dimension
if (isChanged == true && backgroundImage != null)
{
bitmapBackgroundImage = getScaledBitmapImage(backgroundImage,
this.width - (leftMargin+rightMargin),
this.height - (topMargin+bottomMargin));
}
}
public void setWidth(int width)
{
if (width > 0 && width != this.width)
{
this.width = width;
// If background image is available, adapt it to the new width
if (backgroundImage != null)
{
bitmapBackgroundImage = getScaledBitmapImage(backgroundImage,
this.width - (leftMargin+rightMargin),
this.height - (topMargin+bottomMargin));
}
}
}
public void setHeight(int height)
{
// Ignore the specified height if it is
// less than the minimum height required to display the text.
if (height > minHeight)
{
this.height = height;
// If background image is available, adapt it to the new width
if (backgroundImage != null)
{
bitmapBackgroundImage = getScaledBitmapImage(backgroundImage,
this.width - (leftMargin+rightMargin),
this.height - (topMargin+bottomMargin));
}
}
}
public void setBackgroundImage(EncodedImage backgroundImage)
{
this.backgroundImage = backgroundImage;
// Consider the height of background image in
// calculating the height of the text box.
// setHeight() does not ensure that specified
// height will be in effect, of course, for valid reasons.
// So derivation of Bitmap image in the setHeight() method is not sure.
setHeight(backgroundImage.getHeight() + topMargin + bottomMargin);
if (bitmapBackgroundImage == null)
{
bitmapBackgroundImage = getScaledBitmapImage(backgroundImage,
this.width - (leftMargin+rightMargin),
this.height - (topMargin+bottomMargin));
}
}
/**
* Generate and return a Bitmap Image scaled according
* to the specified width and height.
*
* #param image EncodedImage object
* #param width Intended width of the returned Bitmap object
* #param height Intended height of the returned Bitmap object
* #return Bitmap object
*/
private Bitmap getScaledBitmapImage(EncodedImage image, int width, int height)
{
// Handle null image
if (image == null)
{
return null;
}
int currentWidthFixed32 = Fixed32.toFP(image.getWidth());
int currentHeightFixed32 = Fixed32.toFP(image.getHeight());
int requiredWidthFixed32 = Fixed32.toFP(width);
int requiredHeightFixed32 = Fixed32.toFP(height);
int scaleXFixed32 = Fixed32.div(currentWidthFixed32, requiredWidthFixed32);
int scaleYFixed32 = Fixed32.div(currentHeightFixed32, requiredHeightFixed32);
image = image.scaleImage32(scaleXFixed32, scaleYFixed32);
return image.getBitmap();
}
protected void sublayout(int width, int height)
{
// We have one and only child - the edit field.
// Place it at the appropriate place.
Field field = getField(0);
layoutChild(field, this.width - totalHorizontalEmptySpace,
this.height - totalVerticalEmptySpace);
setPositionChild(field, leftMargin+leftPadding, topMargin+topPadding);
setExtent(this.width, this.height);
}
public void setTopMargin(int topMargin)
{
this.topMargin = topMargin;
}
public void setBottomMargin(int bottomMargin)
{
this.bottomMargin = bottomMargin;
}
protected void paint(Graphics graphics)
{
// Draw background image if available, otherwise draw a rectangle
if (bitmapBackgroundImage == null)
{
//graphics.drawRect(leftMargin, topMargin,
// width - (leftMargin+rightMargin),
//height - (topMargin+bottomMargin));
graphics.drawRoundRect(leftMargin, topMargin,
width - (leftMargin+rightMargin),
height - (topMargin+bottomMargin), 5, 5);
}
else
{
graphics.drawBitmap(leftMargin, topMargin,
width - (leftMargin+rightMargin),
height - (topMargin+bottomMargin),
bitmapBackgroundImage, 0, 0);
}
// Determine the rightward text that can fit into the visible edit field
EditField ef = (EditField)getField(0);
String entireText = ef.getText();
boolean longText = false;
String textToDraw = "";
Font font = getFont();
int availableWidth = width - totalHorizontalEmptySpace;
if (font.getAdvance(entireText) <= availableWidth)
{
textToDraw = entireText;
}
else
{
int endIndex = entireText.length();
for (int beginIndex = 1; beginIndex < endIndex; beginIndex++)
{
textToDraw = entireText.substring(beginIndex);
if (font.getAdvance(textToDraw) <= availableWidth)
{
longText = true;
break;
}
}
}
if (longText == true)
{
// Force the edit field display only the truncated text
ef.setText(textToDraw);
// Now let the components draw themselves
super.paint(graphics);
// Return the text field its original text
ef.setText(entireText);
}
else
{
super.paint(graphics);
}
}
public int getPreferredWidth()
{
return width;
}
public int getPreferredHeight()
{
return height;
}
protected boolean keyChar(char ch, int status, int time)
{
if (ch == Characters.ENTER)
{
return true;
}
else
{
return super.keyChar(ch, status, time);
}
}
public String getText()
{
return ((EditField)getField(0)).getText();
}
public void setText(final String text)
{
((EditField)getField(0)).setText(text);
}
}
you can use EditField in APIs and assign a background to it by:
yourEditField.setBackground(yourBackground);

Custom editfield not displaying all typed text

Below class is a textbox field. Can this be modified so that when the textbox is filled with text and user keeps type the text then scrolls ? Whats happening now is that once the textbox is filled with text any subsequent text that is typed is not being displayed.
Thanks
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.component.EditField;
public class CustomEditField extends EditField {
// private members of the CustomEditField class
private Font defaultFont;
// used to get the default font
private String text;
// used to specify the default width of the table cells
// constructor calls the super class constructor
public CustomEditField(String label, String initialValue, int maxNumChars,
long style) {
super(label, initialValue, maxNumChars, style);
}
// overrides the default getPreferredWidth functionality to return a fixed
// width
public int getPreferredWidth() {
defaultFont = Font.getDefault();
text = "0000000000";
return defaultFont.getAdvance(text);
}
// overrides the default layout functionality to set the width of the table
// cell
protected void layout(int width, int height) {
width = getPreferredWidth();
height = super.getPreferredHeight();
super.layout(width, height);
// uses the super class' layout functionality
// after the width and the height are set
super.setExtent(width, height);
// uses the super class' setExtent functionality
// after the width and the height are set
}
public void paint(Graphics graphics){
graphics.setBackgroundColor(Color.LIGHTBLUE);
super.paint(graphics);
}
}
This will help you to get started. It is a simplified version of the ScrollableEditField that I am using. I coded it before touch BlackBerry devices became available, therefore some additional work is required here to support TouchEvents.
class ScrollableEditField extends Manager {
private final static int DEFAULT_TOP_PADDING = 1;
private final static int DEFAULT_BOTTOM_PADDING = 1;
private final static int DEFAULT_LEFT_PADDING = 1;
private final static int DEFAULT_RIGHT_PADDING = 1;
private int TOTAL_VERTICAL_PADDING = DEFAULT_TOP_PADDING + DEFAULT_BOTTOM_PADDING;
private int TOTAL_HORIZONTAL_PADDDING = DEFAULT_LEFT_PADDING + DEFAULT_RIGHT_PADDING;
private int width = -1;
private int height = -1;
private HorizontalFieldManager hfm = new HorizontalFieldManager(HORIZONTAL_SCROLL);
private EditField ef;
public ScrollableEditField(String label, String initialValue, int maxNumChars, long innerEditFieldStyle) {
super(NO_HORIZONTAL_SCROLL);
ef = new EditField(label, initialValue, maxNumChars, innerEditFieldStyle);
hfm.add(ef);
add(hfm);
}
protected void sublayout(int width, int height) {
if (this.width != -1) {
width = this.width;
}
if (this.height != -1) {
height = this.height;
} else {
height = ef.getFont().getHeight();
}
layoutChild(hfm, width-TOTAL_HORIZONTAL_PADDDING, height-TOTAL_VERTICAL_PADDING);
setPositionChild(hfm, DEFAULT_LEFT_PADDING, DEFAULT_TOP_PADDING);
setExtent(width, height);
}
public EditField getEditField() {
return ef;
}
public void setWidth(int width) {
this.width = width;
}
protected void onFocus(int direction) {
super.onFocus(direction);
ef.setCursorPosition(0);
}
protected void onUnfocus() {
hfm.setHorizontalScroll(0);
super.onUnfocus();
}
};
public class ScrollableEditFieldScreen extends MainScreen {
public ScrollableEditFieldScreen() {
super(NO_VERTICAL_SCROLL);
setTitle("ScrollableEditField");
// hfm1 and hfm2 are here just to position the ScrollableEditField in the center of the screen
HorizontalFieldManager hfm1 = new HorizontalFieldManager(USE_ALL_HEIGHT | FIELD_HCENTER);
HorizontalFieldManager hfm2 = new HorizontalFieldManager(FIELD_VCENTER);
// instantiating the scrollable edit field and adding border
ScrollableEditField sef = new ScrollableEditField("", "", 50, 0);
sef.setBorder(BorderFactory.createRoundedBorder(new XYEdges(5,5,5,5)));
sef.setWidth(sef.getFont().getAdvance('0')*10);
hfm2.add(sef);
hfm1.add(hfm2);
add(hfm1);
}
}

Resources