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
Posts: 206
Registered: ‎02-22-2011
My Device: BB TORCH & 9800
My Carrier: idea

CustomEditField Content Alignment issue

Hi All

 

I m using a class which extends from BasicEditfield and overrided layout(), getPreferredWidth() and getPreferredHeight() methods.

 

Im adding this customized editfield to a scrollable HorizontalFeildManager.

 

For "left" alignment:

 

          I able to display text properly,

 

          In customized manager of  setPositionChild(field,0,0) I given--------------------->A

 

 

For "right" alignment

 

          I able to display text properly only when the entered text width is within the Screen width

 

          In customized manager of  setPositionChild(field,(screen_width-text_width)>0((screen_width-text_width)):0,0) I given--------------------->B

 

 

so my problem is with "right" alignment,

 

with "right alignment" when im keep on trying text in editfield its returning(by getPreferredWidth()) some value and using line B i able to get result even if the text width is more. when textwidth is more the X-coordinate of line B is 0 and cursor is showing at right end only.

 

but I move from that field to another then I came back to the field which is right aligned the cursor is placed at 0th position , Im thinking its due to line B. so how to overcome this even if i go out of that field and came to that field the cursor focus should be at right end how to achieve this.

 

Thanks in advance

Anil.

 

 

 

 

 

 

 

 

 

          

 

 

*****************************************************************************
* Give kudos when you like it
* Mark it as a solution if it fixes your issue
Developer
Posts: 2,268
Registered: ‎07-08-2009
My Device: various
My Carrier: various

Re: CustomEditField Content Alignment issue

Can you explain what you are trying to achieve? It's not clear enough from your explanation.

 

There is one remark I can give right now, though: you should not use getPreferredWidth as the basis for your width calculations. You should check the overall text length (using getFont().getAdvance(getText()), for example) and see if it's more than the screen width (in which case the field will word wrap and take multiple lines).

 

In general, explain what you want to see and give both your Field's relevant code (layout() is extremely relevant here; if you are trying to utilize getPreferredWidth, show its code as well) and your Manager's (sublayout() of your Manager is the key part and there are many possibilities for small and huge mistakes there).

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!
Developer
Posts: 206
Registered: ‎02-22-2011
My Device: BB TORCH & 9800
My Carrier: idea

Re: CustomEditField Content Alignment issue

Hi arkadyz, For left & right content alignments im doing and achieving like below

 

In case of left:

left

 

So for left initially cursor position is at left end and keep on typing cursor is placed at right end (like above image) and characters are entering properly.

 

 

 

In case of right:

 

right1.PNG     cursor placed at right end intially

 

 

right2.PNG  

 

keep on typing the characters are entered like above image

 

 

 

right3.PNG

 

while entering more characters its like above here the problem comes,

actually for right alignment im doing setPositionChild(field, (screen_width-text_width),height) , when text_width is more the X-coordinate of this method becomes -ve value.at this case what I have to take.

 

how to get the right alignment properly, I hope now I explained properly about my requirement.

 

Coming to coding part:

I have taken customized editfield and added to Scrollable(style) HorizontalFieldManager.

 

In my customized editfield I overrided getPreferredWidth() and In sublayout of my manager Im using layoutchild(fld,screen_width,height), setPositionChild(fld, (screen_width-fld.getPreferredWidth),height).

 

here when fld.getPreferredWidth is more than screen_width due to -ve , X-value im not able to get the RIGHT alignment properly.

 

 

 

Thanks in advance

Anil 

 

*****************************************************************************
* Give kudos when you like it
* Mark it as a solution if it fixes your issue
Developer
Posts: 2,268
Registered: ‎07-08-2009
My Device: various
My Carrier: various

Re: CustomEditField Content Alignment issue

So you re-layout the manager on each key press in the field? OK.

 

First of all, please show your code of getPreferredWidth. I guess you might have some small mistake there.

Second, if you add your field to a scrolling manager, the manager will scroll properly with the cursor going right because edit field reports the cursor position as its focus rectangle (as you've discovered with your left-aligned field). Therefore, you don't have to give negative x position to the field. Why don't you calculate x as Math.max(0, <your calculated position>)? It will become zero once your field is too wide and will behave exactly like the left-aligned field which is fine at this point.

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!
Developer
Posts: 206
Registered: ‎02-22-2011
My Device: BB TORCH & 9800
My Carrier: idea

Re: CustomEditField Content Alignment issue

Hi arkadyz,

In customized editfield, in getPreferredWidth() im using like this,

public int getPreferredWidth ()
{
if(this.getText() != null && this.getText().length() > 0)
return font.getAdvance(this.getText()) + 10;/* 10 pixels for the cursor when the focus is on the last char*/
else
return (2 * font.getAdvance("9"));
}

Its giving the entered text width.
*****************************************************************************
* Give kudos when you like it
* Mark it as a solution if it fixes your issue
Developer
Posts: 2,268
Registered: ‎07-08-2009
My Device: various
My Carrier: various

Re: CustomEditField Content Alignment issue

OK, I guessed something like that. I would suggest using font.getAdvance("W") (or another wide character) instead of the hard-coded 10 pixels.

 

Have you tried my suggestion of setting position to no less than zero?

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!
Developer
Posts: 206
Registered: ‎02-22-2011
My Device: BB TORCH & 9800
My Carrier: idea

Re: CustomEditField Content Alignment issue

Ok, arkadyz im trying with your inputs,

 

and as you said to use Math.max(0, <your calculated position>) like that I used 0 already when the difference  is become -ve in setPositionChild() method, here when keep on typing it is behaving correctly but at this moment if I go from that field to another and came back to that field(move focus out and when that field retain focus) then my problem is

 

A) for RIGHT alignment initially when the field getting focus its behaving like below, its correct (the cursor position)

 

right1.PNG

 

B) for RIGHT alignment while keep on typing ,then also cursor position is there itself which is correct.

right2.PNG

 

 

C)If I move focus out its behaving like this, its normal

right3.PNG

 

D) when my required getting focus(RIGHT alignmanted field) its getting like this , its correct (here the entered text_width is less thatn screen_width ), this what we expected

 

right4.png

 

E) whenever the entered text_width is more than screen_width, its behaving like below, which we dont want.

 

I think it is because of we are giving X- coordinate is 0 when entered text_width is more than screen_width, i.e.,Math.max(0,"my calculated width") for X-coordinate.

 

right5.png

 

 

So at this case how to set thecursor position (for right alignment), I have tried setCursorposition() but of no use.

 

 

any ideas

 

 

Thanks in advance

Anil

*****************************************************************************
* Give kudos when you like it
* Mark it as a solution if it fixes your issue
Developer
Posts: 2,268
Registered: ‎07-08-2009
My Device: various
My Carrier: various

Re: CustomEditField Content Alignment issue

Looks weird indeed.

Can you also show the code of layout() of your BasicEditField?

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!
Developer
Posts: 206
Registered: ‎02-22-2011
My Device: BB TORCH & 9800
My Carrier: idea

Re: CustomEditField Content Alignment issue

In layout I'm doing this

 

setExtent(width,height)

Super.layout(width,height)

 

I have checked that width I'm getting 350( I.e, width=350)

 

In sub layout of my extended manager

 

setpositionchild(fld,mycalculated_width,hgt)

Layoutchild(fld,text_width,hgt), // here text_width is giving entered text width 

 

I'm not using setExtent() in sublayout, 

 

please assist

 

Thanks in advance

Anil

 

*****************************************************************************
* Give kudos when you like it
* Mark it as a solution if it fixes your issue
Developer
Posts: 19,623
Registered: ‎07-14-2008
My Device: Not Specified

Re: CustomEditField Content Alignment issue

[ Edited ]

Removed - made redundant by arkadyz's next post.