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
Contributor
pk25
Posts: 17
Registered: ‎06-07-2013
My Device: Blackberry Curve - 9300
Accepted Solution

Aligning labelfield in the centre of a Horizontal Field Manager

I am trying to align a labelfield in the centre of a horizontal field manager but instead the label field is being aligned to the right most corner of the horizontal field manager. 

The following is my code snippet:

 

HorizontalFieldManager itemFieldManager =  new HorizontalFieldManager(HorizontalFieldManager.USE_ALL_WIDTH|HorizontalFieldManager.FIELD_HCENTER|
HorizontalFieldManager.FIELD_VCENTER){ public void paint(Graphics graphics) { graphics.setBackgroundColor(Color.DODGERBLUE); graphics.clear(); super.paint(graphics); }}; LabelField itemNumber = new LabelField("1/20",DrawStyle.HCENTER|LabelField.FIELD_HCENTER); itemFieldManager.add(itemNumber);

 

 

Need your valuable inputs.

Developer
developer_aman_arora
Posts: 354
Registered: ‎06-05-2013
My Device: Blackberry Z10

Re: Aligning labelfield in the centre of a Horizontal Field Manager

Use this to make labelfield in centre of screen

HorizontalFieldManager itemFieldManager = new HorizontalFieldManager(USE_ALL_WIDTH | USE_ALL_HEIGHT)
{
protected void sublayout(int maxWidth,int maxHeight)
{
super.sublayout(maxWidth, maxHeight);
setPositionChild(this.getField(0), (Display.getWidth()-itemNumber.getWidth())/2,(Display.getHeight()-itemNumber.getHeight())/2);
}
public void paint(Graphics graphics)
{
graphics.setBackgroundColor(Color.DODGERBLUE);
super.paint(graphics);
}
};
itemNumber = new LabelField("1/20");
itemFieldManager.add(itemNumber);
add(itemFieldManager);
Contributor
pk25
Posts: 17
Registered: ‎06-07-2013
My Device: Blackberry Curve - 9300

Re: Aligning labelfield in the centre of a Horizontal Field Manager

It's not working :smileysad: 

Developer
developer_aman_arora
Posts: 354
Registered: ‎06-05-2013
My Device: Blackberry Z10

Re: Aligning labelfield in the centre of a Horizontal Field Manager

what u have done now? please provide me the code
Contributor
pk25
Posts: 17
Registered: ‎06-07-2013
My Device: Blackberry Curve - 9300

Re: Aligning labelfield in the centre of a Horizontal Field Manager

[ Edited ]

I have done the same as you suggested except I didnt put USE_ALL_HEIGHT in the horizontal field manager since I already have a vertical field manager to which I have added this horizontal field manager.

 

Code snippet:

 

VerticalFieldManager verticalFieldManager = new VerticalFieldManager();

final LabelField itemNumber = new LabelField("1/20");

HorizontalFieldManager itemFieldManager = new HorizontalFieldManager(HorizontalFieldManager.USE_ALL_WIDTH)
{
protected void sublayout(int maxWidth,int maxHeight)
{
super.sublayout(maxWidth, maxHeight);
setPositionChild(this.getField(0), (Display.getWidth()-itemNumber.getWidth())/2,(Display.getHeight()-itemNumber.getHeight())/2);
}

public void paint(Graphics graphics)
{
graphics.setBackgroundColor(Color.DODGERBLUE);//blue
graphics.clear(); super.paint(graphics);
}
};

itemFieldManager.add(itemNumber);
verticalFieldManager.add(itemFieldManager);
add(verticalFieldManager);

 

P.S: I have used a vertical field manager since I had 2 more fields which I have added to it.

Also, I want to add a button on the right msot corner of the horizontal field manager  after aligning the labelfield in the centre.

Developer
developer_aman_arora
Posts: 354
Registered: ‎06-05-2013
My Device: Blackberry Z10

Re: Aligning labelfield in the centre of a Horizontal Field Manager

u r not adding verticalFieldManager
Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: Aligning labelfield in the centre of a Horizontal Field Manager

@develope4_aman_arora - It is always better to test your sample code before posting it.  Here is a version that has been hacked round to 'sort of' work.  I will get round to what I mean by 'sort of' in a minute.

 

        final LabelField itemNumber = new LabelField("1/20", LabelField.FOCUSABLE) {
            public void paint(Graphics graphics)
            {
            graphics.setBackgroundColor(Color.RED);
            graphics.clear();
            super.paint(graphics);
            }
        };
        HorizontalFieldManager itemFieldManager = new HorizontalFieldManager(USE_ALL_WIDTH | USE_ALL_HEIGHT)
        {
        protected void sublayout(int maxWidth,int maxHeight)
        {
        super.sublayout(maxWidth, maxHeight);
        setPositionChild(this.getField(0), (Display.getWidth()-itemNumber.getWidth())/2,(Display.getHeight()-itemNumber.getHeight())/2);
        }
        public void paint(Graphics graphics)
        {
        graphics.setBackgroundColor(Color.DODGERBLUE);
        graphics.clear();
        super.paint(graphics);
        }
        };

Developer
developer_aman_arora
Posts: 354
Registered: ‎06-05-2013
My Device: Blackberry Z10

Re: Aligning labelfield in the centre of a Horizontal Field Manager

add(verticalFieldManager); missing at the end

VerticalFieldManager verticalFieldManager = new VerticalFieldManager();

final LabelField itemNumber = new LabelField("1/20");

HorizontalFieldManager itemFieldManager = new HorizontalFieldManager(HorizontalFieldManager.USE_ALL_WIDTH)
{
protected void sublayout(int maxWidth,int maxHeight)
{
super.sublayout(maxWidth, maxHeight);
setPositionChild(this.getField(0), (Display.getWidth()-itemNumber.getWidth())/2,(Display.getHeight()-itemNumber.getHeight())/2);
}

public void paint(Graphics graphics)
{
graphics.setBackgroundColor(Color.DODGERBLUE);//blue
graphics.clear(); super.paint(graphics);
}
};

itemFieldManager.add(itemNumber);
verticalFieldManager.add(itemFieldManager);
add(verticalFieldManager);


Developer
developer_aman_arora
Posts: 354
Registered: ‎06-05-2013
My Device: Blackberry Z10

Re: Aligning labelfield in the centre of a Horizontal Field Manager

i m testing it its working fine with me..
Contributor
pk25
Posts: 17
Registered: ‎06-07-2013
My Device: Blackberry Curve - 9300

Re: Aligning labelfield in the centre of a Horizontal Field Manager

[ Edited ]

I have used 

add(verticalFieldManager); in the end. I am sorry I didn't post it in my code snippet since I thought it was obvious.

I have edited the code now. Thank you for bringing it to my notice.