06-10-2009 12:37 PM
How do I stop an edit field from wrapping text?
I put an EditField inside a Horizontal Field Manager(with an image background). To look kind of like a google search Box.
My problem is that when the user enters more text that is larger than the size of the manager, the edit field starts wrapping into a new line. I put Manger.HorizontalScroll on the HFM but that didnt do it, instead it wraps unto the next line.
Any ideas?
06-10-2009 01:21 PM
I think in such type situation you have to customize EditField extending Field.
Here is one of my CustomTextField that meets your requirement. This might help you.
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.system.Display; import net.rim.device.api.system.Characters; import net.rim.device.api.ui.Graphics; import net.rim.device.api.ui.Font; public class CustomTextBox extends Manager { 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; 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; 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 = Display.getWidth(); private int height = minHeight; private EditField editField; public CustomTextBox() { super(0); editField = new EditField(); 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); 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); } }
Regards
Bikas
09-18-2009 01:37 AM
09-18-2009 07:44 AM
Hi Bikas, thanks for posting and helping others with your customised code.
Hi, kemdada,Try something like this...
EditField ef = new EditField(Field.NO_VERTICAL_SCROLL | Field.HORIZONTAL_SCROLL) ;
Note: the above code is not compiled.
Thanks and Regards,
PraveenGoparaju
11-12-2009 01:54 AM
HI bikas,
i have used ur code. but i need only enter number in textbox and its scroll ,but when delete key press ,it not delete the number .how this possible,i want only enter number and it scroll.
thanks
11-12-2009 03:12 AM
Hi lsonu123,
Try with the following code. It should accept only numeric input and allow scroll.
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.system.Display;
import net.rim.device.api.system.Characters;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Font;
public class NumericTextField extends Manager
{
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;
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;
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 = Display.getWidth();
private int height = minHeight;
private EditField editField;
public NumericTextField()
{
super(0);
editField = new EditField(EditField.FILTER_NUMERIC);
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);
boolean longText = false;
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)
{
longText = true;
break;
}
}
}
if (longText == true)
{
ef.setText(textToDraw);
super.paint(graphics);
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);
}
}Please let us know if it works for you.
Regards
Bikas
11-12-2009 03:28 AM
HI bikas,
It work but i want that when delete key press,it not delete the number that enter before means delete key not works.we can only enter number in TextBox not delete that number that would be entered earlier.
thanks
11-12-2009 05:37 AM - edited 11-12-2009 05:38 AM
Hi lsonu123,
Try with replacing the keychar(...) method of the above code with the following:
protected boolean keyChar(char ch, int status, int time)
{
//eliminate ENTER key
if (ch == Characters.ENTER)
{
return true;
}
//eliminate delete key
else if (ch == '\b')
{
return true;
}
else
{
return super.keyChar(ch, status, time);
}
}
@Edit: typo
Regards
Bikas
07-08-2010 06:19 AM
Hi Experts,
I am using this same code for wrapping the text. But I an getting IllegalStackSizeException when the field is filled with text and I am trying to change the trackball.
Definetly some problem with onFocus() and onUnfocus().....
I dont know why.. I just want to set focus on this field first..
Thanks in advance...
Dee
07-09-2010 08:45 AM
hello,
need a reply on this..
Thanks,
Dee