Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Posts: 271
Registered: ‎12-24-2010
My Device: BlackBerry 10
My Carrier: Airtel

How to change the Edit Field to be work on a Single line



In my application, i am using a custom edit field. In this edit field, i am using to different focus and unfocus image and using like Android Edit Field. Till this it is working properly. But when i am entering the text in the edit field, after touching the border of the image, it is again coming to the new line. But I don't want new line. It should go as much as possible in a single line and user should be capable of entering the characters as much as possible as like Android Edit Field.


Here i wrote the code for this:


public class EditFieldCustom extends EditField {
	Bitmap bkgnd;
	Bitmap bmpFocussedImage, bmpUnFocussedImage;
	String strHint = null;
	private boolean _drawFocus = false;

	public EditFieldCustom(Bitmap unFocussedImage, Bitmap focussedImage, int length, long style, String strHintText) {
		super("", "", length, style | EditField.NO_NEWLINE | EditField.FIELD_LEADING | EditField.NON_SPELLCHECKABLE | EditField.FOCUSABLE);
		this.bmpFocussedImage = focussedImage;
		this.bmpUnFocussedImage = unFocussedImage;
		this.strHint = strHintText;
		bkgnd = unFocussedImage;
	protected void layout(int maxWidth, int maxHeight) {
		super.layout(bkgnd.getWidth(), bkgnd.getHeight());
		setExtent(bkgnd.getWidth(), bkgnd.getHeight());
		int top = bkgnd.getHeight() / 2 - NetworkActivity.PFONT_7.getHeight() / 2;
		int left = 15;
		this.setPadding(new XYEdges(top, 0, 0, left));

	public int getPreferredWidth() {
		return bkgnd.getWidth();

	public int getPreferredHeight() {
		return bkgnd.getHeight();

	protected void paint(Graphics graphics) {
		if (getTextLength() == 0) {
            graphics.drawText(strHint, 0, 0);
	} // end paint()

	public void setBkgnd(Bitmap bkgnd) {
		this.bkgnd = bkgnd;
		setBackground(BackgroundFactory.createBitmapBackground(bkgnd, 0, 0, Background.REPEAT_NONE));
	protected void onUnfocus() {
		bkgnd = bmpUnFocussedImage;
	protected void onFocus(int direction) {
		bkgnd = bmpFocussedImage;
	public void setFocussedImage(Bitmap focusImage) {
		bmpFocussedImage = focusImage;


 It will be a great help if any one give me the solution..



Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to change the Edit Field to be work on a Single line

Sorry no idea what you mean by Android Edit Field, but if you search the forum you will find that there have been quite a number of posts related to SingleLineEditField.  From memory this is typically achieved by placing an EditField inside a scrolling HorizontalFieldManager. 


Regarding the current code, there are number of issues. that I see:


a) Your layout should not invoke anything that could cause an recursive layout.  The idea of layout is to define the space requirement for the Field.  So you should not do anything that will change this.  But in your layout you have

this.setPadding(new XYEdges(top, 0, 0, left));

and setPadding will impact on the size requirement.  So calculate that once, set it as part of the Field constructions and don't do it in layout.


b) As you indicate by your parameters, layout is passed the maximum width and height.  You should make sure that this you don't enlarge these.  You code assumes that bkgnd.getWidth() is less than maxWidth for example.


c) If you call super.layout to layout the Field than you should respect its decision, especially if you then expect it to paint itself.  If you want the Field to use all the width and height you give it, then specify USE-ALL_HEIGHT and USE_ALL_WIDTH. 


d) I recommend that you look at the standard setBackground method and see how you can set different backgrounds automatically depending on the visual state.  Doing that is a much better approach that trying to do it in onUnfocus and onFocus as you currently do.