Welcome!

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

Reply
Developer
Kiran_Snist
Posts: 174
Registered: ‎01-25-2011
My Device: Not Specified

Screen width interms of string length

Hi,

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

Please use plain text.
Developer
madiha
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) {
		super.paintBackground(graphics);
	}

	protected void paint(Graphics graphics) {
		graphics.setColor(Color.BLACK);
		super.paint(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);
    }

}

 




Please use plain text.
New Developer
shivam284
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
Please use plain text.