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
Trusted Contributor
Posts: 242
Registered: ‎03-16-2010
My Device: 9530
Accepted Solution

Solution for EditField !

 

Hi,

 

    I am sure many to all must have faced this problem and their must be some way to get thru it.

EditField, it takes all the space that it has and importantly doesn't show the right side border. I added in FlowField and HorizontalField manager, in both it behaves the same. I want it to have till the end or some size, but need not set maxsize to show it in the screen.

I mean, if I want to let the user enter 200 chars. But on screen I want to show only till 20 chars. Rest all should be scrollable like out normal text fields.

 

   What can be the technique to solve this ?

 

Any guide, help is highly appreciated.

 

Thanks


--------------------------------------------------------------------------------
For any helpful answers give kudo's (star) to the helper.
Developer
Posts: 216
Registered: ‎02-18-2010
My Device: 9350,8520
My Carrier: no

Re: Solution for EditField !

why you are not override layout method like

 

 

protected void layout(int width, int height) 
{
	super.layout(100, 30);
	setExtent(100, 30);
}

 

 

 

Developer
Posts: 2,268
Registered: ‎07-08-2009
My Device: various
My Carrier: various

Re: Solution for EditField !

EditField cannot handle scrolling on its own.  They try to resize and wrap to the next line if there is not enough horizontal space.  Besides, if you type ENTER you will wrap it to the next line as well.

 

It seems like you want your EditField to stay on one line.  If it is true, then the following article is for you:

 

Scrollable one-line text input field

 

There are also comments to that article which suggest ways of painting the border around the text entry area.

 

If you want to further limit the width of your edit field, you will need either some sublayout() code and an additional parameter in the constructor, or a custom manager playing with layoutChild(...) and setPositionChild(...) in its own sublayout() around this field.

 

For example, if you can calculate the total width of your field (label + text entry area) in pixels and you put it in, say, myMaxWidth member variable, then your sublayout() will look like this:

 

protected void sublayout(int width, int height) {
  super.sublayout(Math.min(myMaxWidth, width), height);
}

 

 

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!
Trusted Contributor
Posts: 242
Registered: ‎03-16-2010
My Device: 9530

Re: Solution for EditField !

 

Thanks arkadzy,

 

     Yes, I wanted my EditField to be in 1 line only.

 

     All is working properly as I want. Just wondering hoe can I convert length into pixels, reg sublayout myMaxWidth.

 

    In my class constructor I added a parameter textEntryArea. I add label.length() + textEntryArea and save to maxWidth : maxWidth = label.length() + 100; //textEntryArea;

 

   While calling, I pass value 20 for textEntryarea. so, my label length is 6 + 20 = 27 is the maxWidth that is used in sublayout. This provides very small area, Label is also not shown in same line (it comes in 2 lines like :

Lab

el

 

Are you aware How to calcualte/convert the length to pixel ? Alternative I choosed is to pass bigger value to textEntryArea so eg.  pass 100 insted of 20. If conversion to pixel can be done easily, then I can just pass the text area actual width to be displayed based on my screen layout and space avaialable.

 

Was also wondering of one more thing : once the focus is lost from the edit field, it should go to the start of the text like it goes on receiving focus. Wondering for this what to write in onUnFocus() of editfield ?

 

Thanks

 

 


--------------------------------------------------------------------------------
For any helpful answers give kudo's (star) to the helper.
New Developer
Posts: 45
Registered: ‎05-25-2010
My Device: 8300
My Carrier: developer

Re: Solution for EditField !

Font class contains method:

 

getAdvance(String text)
          Determines advance of a text string.
Returns:
    Pixels advanced by specified text string. If text parameter is null, this method returns 0.

Developer
Posts: 2,268
Registered: ‎07-08-2009
My Device: various
My Carrier: various

Re: Solution for EditField !

As for characters-to-pixels conversion: you've already got an advice on using font.getAdvance().  Be advised, though, that different characters have different widths - go with a really wide character like 'W' and multiply its advance by the number of characters.

 

If you use the code from the third comment in the article and want to limit the width, you'll need to add your stuff to the sublayout there.  Something like that:

 

protected void sublayout(int w, int h) {
	int height;
	int width;
	Font inputFont = _editField.getFont();
	Font labelFont = _label.getFont();
	int labelSize = labelFont.getAdvance(_label.getText());
	int inputMaxSize = numChars * inputFont.getAdvance("W");
	int myMaxWidth = labelSize + inputMaxSize + 2 * (_border + _padding);
	w = Math.min(myMaxWidth, w);

	layoutChild(_label, w, h);
	...

 I marked the additional code in red. numChars is the number of characters you want to see on the screen (might be more than that if the characters are narrow enough, like 'i' or 'l').

 

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!
Highlighted
Trusted Contributor
Posts: 242
Registered: ‎03-16-2010
My Device: 9530

Re: Solution for EditField !

 

Thanks arkadyz,

 

    it works perfectly well.

 

Thanks


--------------------------------------------------------------------------------
For any helpful answers give kudo's (star) to the helper.