Scrollable Edit Box for Blackberry

Getting a scrollable edit field is a tough tusk in blackberry. But if you use the below code, you can get it easily.

public class TextBoxWithScrollField extends Manager implements ScrollChangeListener
{
private int nManagerWidth = 0;
private int nManagerHeight = 0;
private String strTxtContent = null;
int nOrientaion = Display.getOrientation();
private VerticalFieldManager vfm = new VerticalFieldManager(VERTICAL_SCROLL | USE_ALL_WIDTH | USE_ALL_HEIGHT);//
private EditField editField;</code>

public TextBoxWithScrollField(String content, int width, int height, int maxChars, long style)
{
super(style | NO_VERTICAL_SCROLL | NO_HORIZONTAL_SCROLL);
editField = new EditField("", "", maxChars, FOCUSABLE);
nManagerWidth = width;
nManagerHeight = height;
strTxtContent = content;
editField.setText(strTxtContent);
editField.setEditable(true);
editField.setPadding(new XYEdges(10, 10, 10, 10));
vfm.add(editField);
add(vfm);
vfm.setScrollListener(this);
}

public TextBoxWithScrollField(String content, int width, int height, int maxChars)
{
this(content, width, height, maxChars, 0L);

}

public void setBgImgWidth(int width)
{
nManagerWidth = width;
}

public void setBgImgHeight(int height)
{
nManagerHeight = height;
}

public String getText()
{
return editField.getText();
}

public void setText(String text)
{
editField.setText(text);
}

public int getPreferredWidth()
{
return nManagerWidth;
}

public int getPreferredHeight()
{
return nManagerHeight;
}

protected void sublayout(int w, int h)
{
if (nManagerWidth == 0)
{
nManagerWidth = w;
}

if (nManagerHeight == 0)
{
nManagerHeight = h;
}

int nActWidth = Math.min(nManagerWidth, w);
int nActHeight = Math.min(nManagerHeight, h);
layoutChild(vfm, nActWidth - 2, nActHeight - 2); // Leave room for
// border
setPositionChild(vfm, 1, 1); // again, careful not to stomp over the
// border
setExtent(nActWidth, nActHeight);
}

public void scrollChanged(Manager m, int x, int y)
{
}
}

Now how you will use this in your code or in your screen?? It is pretty simple..

TextBoxWithScrollField tbsfMsg = new TextBoxWithScrollField("",Bitmap.getBitmapResource("sample.png").getWidth(), Bitmap.getBitmapResource("sample.png").getHeight(),1000);
tbsfMsg.setBackground(BackgroundFactory.createBitmapBackground(Bitmap.getBitmapResource("sample.png"), 0, 0, 1));
tbsfMsg.setMargin(new XYEdges(10, 0, 0, 10));
tbsfMsg.setEditable(true);
Manager.add(tbsfMsg);

Thats it. Try it. You will get a proper scroll edit box if your edit box background image will be good..

Advertisements

One thought on “Scrollable Edit Box for Blackberry

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s