I'm trying to center vertically and horizontally a label in a vertical field manager. I have succeeded to center it horizontally but not vertically. The problem is that none of the style constants I've tried (Field.FIELD_VCENTER, DrawStyle.VCENTER etc.). I get the debugger for BlackBerry JRE 6 and I develop for Bold 9900 device. Can anyone help?
VerticalFieldManager vfm_listeAeroport=new VerticalFieldManager (USE_ALL_WIDTH){
protected void sublayout(int maxWidth, int maxHeight) {
super.sublayout(2*Display.getWidth()/3,70);
setExtent(2*Display.getWidth()/3,70);
}
protected void paint(Graphics g){
super.paint(g);
setBackground(BackgroundFactory.createSolidBackground(0xefc94c));
}
};
String mText = "Liste des AĆ©roports";
LabelField Label = new LabelField(mText,Field.FIELD_HCENTER) {
public void paint(Graphics graphics){
graphics.setColor(0xffffff);
super.paint(graphics);
}
};
vfm_listeAeroport.add(Label);
add(vfm_listeAeroport);
Thanks!
DrawStyle.VCENTER will center the text within the label, not within it's manager. Eg, the labelfield may have a height of 50px and the manager 100px, DrawStyle will center in the 50px.
I think FIELD_VCENTER is not working because it's within a VerticalFieldManager.
In this case I would just set a margin for the label.
int margin_top = (Display.getHeight() - label.getPreferredHeight()) / 2;
int margin_left = (Display.getWidth() - label.getPreferredWidth()) / 2;
label.setMargin(margin_top,0,0,margin_left);
Just commenting on Kevin's suggested solution that positions the label by using margins and sets the margins based on the Screen height and width.
Kevin is 100% correct. VerticalFieldManager only takes up as much vertical space as is needed, not the whole screen. Use setBackground to set a different background colour to see this in operation.
Kevin's solution makes the size of the contained label bigger, so now the VFM will take up more space.
You do not need to use Kevin's solution to set the left and right Margin - as you have already seen, VFM will centre things horizontally using Field_HCENTER.
The problem with Kevins' solution, and in fact with any solution that attempts to guess how much space is required, is that it will fail when there are other things on the screen, for example a Title area or Banner. The issue is the use of Display.getHeight(), which gets the actual height required, rather than the 'free' space on the screen.
The only time when you know the free space, is when the Field is being laid out. Managers do this in their sublayout() method. So to create a generic 'centring' manager, you need to understand how to create a Manager. You will also need to know a little more about MainScreen and its internal processing. To this end, I recommend the following KB articles from the official BB site:
Custom Manager
Extend Manager
MainScreen explained
Related
I'm working with a 2x2 GridLayout in Vaadin.
gridLayout = new GridLayout(2, 2);
gridLayout.setWidth(100, Unit.PERCENTAGE);
gridLayout.setMargin(true);
gridLayout.setSpacing(true);
The cell in the upper-left corner contains a simple label aligned to the right. Upper-right cell contains a textfield aligned to the left. The second row simply contains a label below the textfield.
gridLayout.addComponent(captionLabel, 0, 0);
gridLayout.addComponent(inputField, 1, 0);
gridLayout.setComponentAlignment(captionLabel, Alignment.MIDDLE_RIGHT);
gridLayout.setComponentAlignment(inputField, Alignment.MIDDLE_LEFT);
Now I want to have both columns within the grid to have size set to 50% to have the whole layout aligned in the middle of my page - now it is slightly shifted to the left side and I can not figure out why...
Vaadin's wiki-page shows a related article, but I can not figure out how to work with it. Seems to be deprecated, because I can not access #getColumn(); - method?! https://vaadin.com/wiki/-/wiki/10674/Configuring+Grid+column+widths
For further info: The GridLayout is added as a separate component to a VerticalLayout.
You can influence the column width's with the grid.setColumnExpandRatio(1, 1); method.
If you wish to have both use 50% of the total width, just set the expand ration to the same value on both columns.
Please also note this:
A layout that contains components with percentual size must have a defined size!
If a layout has undefined size and a contained component has, say, 100% size, the component would fill the space given by the layout, while the layout would shrink to fit the space taken by the component, which is a paradox. This requirement holds for height and width separately.
Book of Vaadin
Solved it by adding a HorizontalLayout-wrapper for the left column containing the label. The right colum contains a VerticalLayout with all other components.
Label captionLabel = new Label(localized);
captionLabel.setSizeUndefined();
HorizontalLayout wrapper = new HorizontalLayout();
wrapper.setSizeFull();
wrapper.addComponent(captionLabel);
wrapper.setComponentAlignment(captionLabel, Alignment.TOP_RIGHT);
[...]
gridLayout.addComponent(wrapper, 0, 0);
gridLayout.addComponent(inputLayout, 1, 0);
gridLayout.setColumnExpandRatio(0, (float)0.5);
gridLayout.setColumnExpandRatio(1, (float)0.5);
I am working on a java web app with SmartGWT for the UI, in it I am using a wide-logo image in the following manner--
Img logo = new Img("hlogo.png", 607, 113 );
Label name = new Label();
HLayout westLayout = new HLayout();
westLayout.setHeight(HEADER_AREA_HEIGHT);
westLayout.setWidth("70%");
westLayout.addMember(logo);
westLayout.addMember(name);
How do I set the width so that it automatically scales to 100% of the width of the 'westLayout' (of type HLayout)
logo.setWidth100();
will extend the logo to 100% of layout size. However, your logo might not keep its size and height scaled. If you want to set height, width to 607,113 as you're doing it, then you're doing it the right way !!
I've been trying to layout fields in blackberry and when I think I have it working I try another simulator and my field's are laid out differently.
If I call setPositionChild(field, 0, 100); I would expect on all phone resolutions that
the fields would be positioned at position x=0, y=100. This is not the case.
Reading the RIM doc for setPositionChild, the y parameter is - "y - Offset of the top of the field in its manager." The y offset returned by field.getTop is consistently 0, so position should be consistent across screens? Maybe a good explanation of how setpositionChild works would suffice.
Thank you.
I suspect your variable results are from extending MainScreen. That class contains separators etc that will be offsetting your 0,100 to something else.
Either try using FullScreen which is a single VerticalFieldManager
Or override sublayout on your MainScreen and set its width and height to the full size of the display.
How to create a custom Label field that works similar to default LabelField
issues : it should be able to take large texts and should be able to display them in different lines if the length exceeds the screen width.
The existing LabelField class already displays text on a different line if the length exceeds the screen width. To increase the font size do something like this:
label.setFont(Font.getDefault().derive(Font.PLAIN, 18, Ui.UNITS_pt));
I am currently working on my own implementation of a tab bar for a BlackBerry app, where each tab bar has a title that is right aligned (i.e. the last character in each should be the same distance from the right hand side of the screen). To work out where to draw the text I am using the following calculation:
screen width - advance of title - indent.
The font I am using is 'BBAlpha Sans' (height 28). Using BlackBerry OS 4.6 everything seems to be calculated properly and the text is aligned when I move between tabs, however I am finding that when I use OS 5.0 it doesn't calculate the advance properly and as a result the alignment is off by maybe 5 pixels or so. With the default font (also BBAlpha Sans, but height 24 - for OS 5.0 at least) it works fine in both versions.. but I don't necessarily always want to use the default font/size, so any ideas what could be going wrong? Is this a bug in the 5.0 API?
Thanks.
Code:
public class TitleBarBackground extends Background {
..
public void draw(Graphics graphics, XYRect rect) {
graphics.pushRegion(rect);
..
Font titleBarFont = FontFamily.forName("BBAlpha Sans").getFont(Font.PLAIN, 28);
...
int textWidth = titleBarFont.getAdvance(title);
graphics.drawText(title, rect.width - textWidth - TITLE_OFFSET, textYOffset);
graphics.popContext();
}
..
}
Are you calling graphics.setFont(titleBarFont) before graphics.drawText()?