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
Berrysoft
Posts: 143
Registered: ‎07-14-2008
My Device: Not Specified
Accepted Solution

Text alignment frustration

[ Edited ]

It seems impossible to easily right align the text of a LabelField.

 

I add two fields to a simple HFM, and the left one is left-aligned just as I want, but I want the 2nd field right-aligned.

 

So I could better see what was going on, I put some color in a paint(), and this is the inner class of the right field:

 

 

private static class MovesField extends LabelField { MovesField( String n ) { super( n, LabelField.FIELD_RIGHT ); } public int getPreferredWidth() { return 60; } public void paint( Graphics g ) { g.setColor( Color.DARKGOLDENROD ); g.fillRect( 0, 0, this.getWidth( ), this.getHeight( ) ); g.setColor( Color.BLACK ); super.paint( g ); } }

 

Test of Dialog

 

 

The RED is the first LabelField, and the GOLDENROD, the second which I wish to be right aligned.  The text for the field is only 2-3 charaters long, so 60 pixels works fine, but the text is left aligned!

 

This is really annoying.  The API states that this is a valid style, so why is my text not aligned????

 

Message Edited by Berrysoft on 12-28-2008 04:37 AM
Please use plain text.
New Developer
dacut
Posts: 4
Registered: ‎12-05-2008
My Device: Not Specified

Re: Text alignment frustration

The FIELD_RIGHT style applies to the widget, not the text.

 

Maybe a RichTextField with the RichTextField.TEXT_ALIGN_RIGHT | Field.FIELD_RIGHT style bits would work better?

Please use plain text.
Developer
Berrysoft
Posts: 143
Registered: ‎07-14-2008
My Device: Not Specified

Re: Text alignment frustration

So, is it impossible (without manually overriding layout() ) to right-align text in a Label or Text Field???

Seems like this is so basic a thing that it should indeed be part of the API, does it not?

 

A RichTextField is overkill when all you need is simple text, so I hate to create all those objects to instantiate one just to do something this ridiculously simple.

 

Is there not another way???

 

 

 

Please use plain text.
New Developer
dacut
Posts: 4
Registered: ‎12-05-2008
My Device: Not Specified

Re: Text alignment frustration

If there is a way to do it with LabelField, I've not discovered it.  In my application, I've had to subclass LabelField to do this and a few other kinds of alignment tricks which I would consider standard from other toolkits.
Please use plain text.
Developer
marchywka
Posts: 1,415
Registered: ‎07-30-2008
My Device: Not Specified

Re: Text alignment frustration

I'm not sure I ever looked as I needed to layout stuff manually anyway and just used

the font advance for my own calculations.

Please use plain text.
Developer
Berrysoft
Posts: 143
Registered: ‎07-14-2008
My Device: Not Specified

Re: Text alignment frustration

I finally figured out an easy solution.

 

Basically, I override getPreferredWidth() in the HFM, fixing it at a size which is large enough for both fields.  Then, I override this same method in the first field setting it's max width leaving enough space left in the HFM to allow for the second field to be right-aligned.  Then, rather than override that method in the second field, I just allow it to be large enough to display the text it contains which I know to be smaller than the space I've allowed for it.  This, it is then right aligned as a whole in the HFM, rather than within the LabelField itself.

 

This allows me to avoid having to override layout() and extend Field in a more complex way.

 

 

Please use plain text.
Developer
Berrysoft
Posts: 143
Registered: ‎07-14-2008
My Device: Not Specified

Re: Text alignment frustration

Awh, I spoke too soon.

 

Turns out that although this should work in theory, it doesn't.  Field.FIELD_RIGHT just does not right align the LabelField within the HFM - period.  It should according to the docs, but it does not.

 

The first field is left aligned (by default), but although the sum of the lengths of the two Label's is less than the width of the HFM, the second is left aligned within the HFM, leaving all the extra room in the HFM to the right of both fields.

 

So, in short FIELD_RIGHT does not work to either align the widget OR the text in this case.

 

Please use plain text.
Developer
RexDoug
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: Text alignment frustration

You should try overriding paint() and drawing the text with DrawStyle.RIGHT

 

Please use plain text.
Developer
marchywka
Posts: 1,415
Registered: ‎07-30-2008
My Device: Not Specified

Re: Text alignment frustration

While I confess to making paint() computationally intensive, you could apply your chosen method

to your own off-screen Graphics and copy from this in paint(). This does use up memory for

your offscreen bitmap and I'm not sure that double buffering here helps anything.

 

 

Please use plain text.
Developer
mreed
Posts: 1,041
Registered: ‎07-16-2008
My Device: ಠ_ಠ

Re: Text alignment frustration

You can override sublayout in the HFM and right align the field manually. The HFM width just needs to be larger than then sum of the two fields to be worth it.

 

 

protected void sublayout( int width, int height ) { super.sublayout( width, height ); width = getWidth(); height = getHeight(); Field field = getSomeField(); if (field != null && equals(field.getManager())) { int x = width - field.getWidth(); int y = (int)(height * 0.50); setPositionChild( field, x, y ); } }

 

 

 

Please use plain text.