11-23-2010 03:07 AM
Hi,
I need a textbox with rounded corners and customized width and height. Am able to do it by extending Edit Field. But the cursor is always at the top of the field. I want it to be vertically centered.. can someone please suggest me a solution?
Please find the code below for reference
public class TextBoxBorder extends EditField {
String mDefaultText;
int BorderColor, FillColor = Color.WHITE, FontColor = Color.BLACK, Width, Height, W = 0, H = 0;
boolean _inFocus = false;
public TextBoxBorder(int BorderColor,int FontColor, int Width, int Height,int W,int H,long _st) {
super(EditField.NO_NEWLINE|EditField.FOCUSABLE|Edi tField.FIELD_VCENTER|_st);
this.BorderColor = BorderColor;
this.FontColor = FontColor;
this.Width = Width;
this.Height = Height;
this.W = W;
this.H = H;
}
public void setDefaultText(String DefaultText) {
mDefaultText = DefaultText;
}
public int getPreferredWidth() {
return this.Width;
}
public int getPreferredHeight() {
return this.Height;
}
public void paint(Graphics g) {
g.setColor(BorderColor);
g.drawRoundRect(0, 0, getPreferredWidth() , getPreferredHeight(),10,10);
g.setColor(FontColor);
Font font= g.getFont();
g.drawText(getText(), 2, (getPreferredHeight()-font.getHeight())/2);
if ( _inFocus ){
_inFocus = false;
}
}
public String getDefaultText() {
return mDefaultText;
}
protected boolean keyChar(char key, int status, int time) {
if (null != mDefaultText)
if (getText().equalsIgnoreCase(mDefaultText)) {
setText(String.valueOf(key));
return true;
}
return super.keyChar(key, status, time);
}
public void layout(int width, int height) {
width = Math.min( width, getPreferredWidth() );
height = Math.min(height,getPreferredHeight());
super.layout(width, height);
setExtent(width, height);
}
protected void onFocus(int direction){
_inFocus = true;
}
protected void onUnfocus(){
_inFocus = false;
}
protected boolean navigationClick(int status, int time) {
fieldChangeNotify(1);
return true;
}
}
Solved! Go to Solution.
11-23-2010 03:31 AM
welcome to the support forums.
i would suggest that you use a manager for your decoration and add the editfield in the center of said manager.
11-23-2010 04:50 AM
Thanks Simon... Works like a charm!