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: 174
Registered: ‎01-25-2011
My Device: Not Specified

Screen width interms of string length


I got a basic(may be wierd) doubt on how to set the max no. of a characters to a edit field with respect to the display width of the device... So, is there a way to convert display width(in pixels) to string length(int integers)???


Or there is any other way to identify the no. of characters a device  can take before gng to another line??

Posts: 107
Registered: ‎05-26-2011
My Device: Torch
My Carrier: Mobilink

Re: Screen width interms of string length

may this help uu

Create a custome edit field extend it from EditField...

override the keyChar() method like this


public class CustomEditField extends EditField {

	private int _width = 0;

public CustomEditField(long style, int width) {
		super(style |EditField.NON_SPELLCHECKABLE
				| EditField.CONSUME_INPUT );
		_width = width;

public int getPreferredWidth() {
		// TODO Auto-generated method stub
		return _width;

	protected void layout(int width, int height) {
		super.layout(_width, height);

	protected void paintBackground(Graphics graphics) {

	protected void paint(Graphics graphics) {

protected boolean keyChar(char key, int status, int time) {
     if (key == Characters.ENTER) 
	return false;
      else if (getFont().getAdvance(getText()) > Dispkay.getWidth()
                && key != Characters.BACKSPACE) {
            Dialog.alert("Field full !");
            return false;

        return super.keyChar(key, status, time);



New Developer
Posts: 108
Registered: ‎01-23-2012
My Device: torch,bold,curve,strom
My Carrier: Blackberry

Re: Screen width interms of string length

Hi ,

I think if you override the keychar();


you will get what ever is your requirment.



Hope it will work!

Thanks & Regards,
Kumar Shivam