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.
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.