How can I vertically center a title on Blackberry? - blackberry

I'm trying to make a design like at the picture:
as you can see, at the top bar of the screen there is a title at the center. I can set it to horizontal center, but how to set it to vertical center?
I allready tryed:
override RichTextField to make it custom sized. it works for widht, but not height.
override horizontal/vertical field manager to make it with custon height with vertical centrizing, but it didn't work:
VerticalFieldManager vfm = new VerticalFieldManager();
HorizontalFieldManager hfm = new HorizontalFieldManager(Field.FIELD_VCENTER){
protected void sublayout( int maxWidth, int maxHeight )
{
super.sublayout( Display.getWidth(), 50);
}
};
LabelWithCustomSize title = new LabelWithCustomSize("ENTERTAINMENT",Field.NON_FOCUSABLE,Display.getWidth(),50,30,2);
hfm.add(title);
vfm.add(hfm);
add an empty RichTextField with small text size, but fiels allways has same height.
How to set text or any other field at vertical center?

The issue that you will need to solve is "How does a Manager centre vertically and Horizontally any field that is added to it". Trying to solve this problem for N number of fields added to Manager might be a bit involved but just for one field, it must be quite easy. I suggest you try this :
VerticalFieldManager vfm = new VerticalFieldManager(){
public void sublayout( int maxWidth, int maxHeight )
{
int margin = 0;
int lheight = 0;
Field f = getField( 0 );
margin = f.getMarginBottom()+f.getMarginTop();
layoutChild(f, maxWidth-margin, maxHeight);
int w = f.getWidth();
int h = f.getHeight();
lheight = h+margin;
int x = (maxWidth - w)/2;
int y = (maxHeight -h)/2;
setPositionChild( f, x, y);
setExtent( maxWidth, lheight);
}
};
vfm.add( field);

Related

Strange things happen when set margin to VerticalFieldManager

I'd like to generate a VerticalFieldManager that has a none-zero margin, so I create a VerticalFieldManager, and then use vfm.setMargin(10, 10, 10, 10); after that, put some fields(buttonField, ObjectChoiceField) in it.
It seems too simple, right? but strange thing happens. When I focus to the last ObjectChoiceField and press space to toggle choice of it, the ObjectChoiceField disappeared.
How can that be? here is the demo code, anyone kindly find the bug?
final class HelloWorldScreen extends MainScreen{
HelloWorldScreen() {
// just a demo to show the strange thing
String [] arr = {"a","b"};
for(int i = 0; i < 10; i++){
VerticalFieldManager vfm = new VerticalFieldManager(); // Field.USE_ALL_WIDTH
vfm.setMargin(10, 10, 10, 10);
ButtonField btn = new ButtonField(String.valueOf(i));
ObjectChoiceField ch = new ObjectChoiceField(String.valueOf(i), arr);
vfm.add(btn);
vfm.add(ch);
add(vfm);
}
}
}
Edit: screenshot of desired UI margins:
That is strange.
For the benefit of those who don't run your code, what's happening is that when you click on the object choice fields, the whole screen is scrolling a little bit vertically. After each vertical scroll, the screen loses the ability to scroll all the way down to the bottom. After many such operations, eventually much of the lower part of this screen is no longer accessible.
I don't know why this is happening. (looks like a BlackBerry bug to me)
What I observed is that if you take out the call to
vfm.setMargin(10, 10, 10, 10);
the problem goes away.
May I suggest a workaround where you use
vfm.setPadding(10, 10, 10, 10);
instead?
I know that margin and padding are not the same thing. However, depending on what your full UI design is (which I cannot see), in this case, setting a 10 pixel padding may be sufficient to do what you were trying to do.
Update: based on the screenshot of your desired UI, I was able to produce that with this workaround. Again, it shouldn't be this much work, but this additional code worked for me:
public class BugScreen extends MainScreen {
private static final int BG_COLOR = Color.LIGHTGRAY;
private static final int FG_COLOR = Color.WHITE;
private static final int BORDER_LINE_COLOR = Color.GRAY;
private static final int PAD = 10;
public BugScreen() {
super(MainScreen.VERTICAL_SCROLL | MainScreen.VERTICAL_SCROLLBAR);
getMainManager().setBackground(BackgroundFactory.createSolidBackground(BG_COLOR));
// this additional call ensures that when scrolling bounces, the area "behind"
// the normal visible area is ALSO of BG_COLOR
setBackground(BackgroundFactory.createSolidBackground(BG_COLOR));
// this will establish a thin gray padding on the screen's left/right sides
// NOTE: I seem to get drawing artifacts if I try to use this for ALL sides!
getMainManager().setPadding(0, PAD, 0, PAD);
String [] arr = {"a","b"};
for(int i = 0; i < 10; i++){
VerticalFieldManager vfm = new VerticalFieldManager(Field.USE_ALL_WIDTH) {
public int getPreferredWidth() {
return Display.getWidth() - 2 * PAD;
}
public void paint(Graphics graphics) {
int oldColor = graphics.getColor();
super.paint(graphics);
graphics.setColor(BORDER_LINE_COLOR);
// draw the (1-pixel wide) border size you would like.
graphics.drawRect(0, 0, getPreferredWidth(), getHeight());
graphics.setColor(oldColor);
}
};
vfm.setBackground(BackgroundFactory.createSolidBackground(FG_COLOR));
ButtonField btn = new ButtonField(String.valueOf(i));
ObjectChoiceField ch = new ObjectChoiceField(String.valueOf(i), arr);
vfm.add(btn);
vfm.add(ch);
// add a separator field to get thin gray margin between (vfm) fields
add(new MarginField());
add(vfm);
}
// add one last margin field at the bottom
add(new MarginField());
}
private class MarginField extends Field {
public MarginField() {
super(Field.USE_ALL_WIDTH);
}
public int getPreferredHeight() { return PAD; }
protected void paint(Graphics g) {
int oldColor = g.getColor();
g.setColor(BG_COLOR);
g.fillRect(0, 0, getWidth(), getPreferredHeight());
g.setColor(oldColor);
}
protected void layout(int width, int height) {
setExtent(width, getPreferredHeight());
}
}
}

Set the height and width of EditField on BlackBerry

I want to set the height and width of an EditField in my BlackBerry app.
You need to override the field's layout method to set the size for you:
protected void layout(int width, int height)
{
super.layout(customWidth, customHeight);
setExtent(customWidth, customHeight);
}
where customWidth and customHeight have been set by you elsewhere.
super.layout(customWidth, customHeight) lays the field out to use your special width & height.
setExtent(customWidth, customHeight) sets the size of the field on the screen.
You can do this when you declare your EditField using code similar to the below code:
final int customWidth = Display.getWidth();
final int customHeight = 30;
Field myEditField = new EditField()
{
protected void layout(int width, int height)
{
super.layout(customWidth, customHeight);
setExtent(customWidth, customHeight);
}
};
You can also pace the edit field inside a horizontalFieldmanager and override the sublayout method to set the height and width of horizontalfieldmanager for setting the height & width of editfield
Note 1 : You can't set the width and height for editField
Note 2 : You use just the sublayout method in manager
Note 3 : If you add the editField to the screen it will fill all the available width from the beginning to the end of screen
You can use this idea by putting some fields in the left side and putting your editField in the last.
Or, you can use the below code.
You will set the width and height of both the manager and the edit field and try to put them on the screen:
HorizontalFieldManager containerManager = new HorizontalFieldManager() {
public int getPreferredHeight() {
return super.getPreferredHeight();
}
public int getPreferredWidth() {
return super.getPreferredWidth();
}
protected void sublayout(int maxWidth, int maxHeight) {
setExtent(getPreferredWidth(), getPreferredHeight());
super.sublayout(getPreferredWidth(), getPreferredHeight());
}
};
EditField editField = new EditField(Field.FIELD_VCENTER) {
public int getPreferredHeight() {
// TODO Auto-generated method stub
return super.getPreferredHeight();
}
public int getPreferredWidth() {
// TODO Auto-generated method stub
return super.getPreferredWidth();
}
};
Another way to modify the height of an EditField is by fiddling with the padding around the borders like this:
EditField emailField = new EditField("", "optional initial text");
XYEdges xyEdge = new XYEdges(2, 2, 2, 2);
XYEdges xyEdgeColors = new XYEdges(0x00dddddd, 0x00dddddd, 0x00dddddd, 0x00dddddd);
Border aBorder = BorderFactory.createSimpleBorder(xyEdge, xyEdgeColors, Border.STYLE_SOLID);
emailField.setBorder(aBorder);
emailField.setPadding(10, 5, 5, 10);

BlackBerry HorizontalFieldManager alignment

Horizontally, I want to display two bitmaps, and between them display a label field.
The code seems straightforward, but all the fields are added on the left side of the screen.
HorizontalFieldManager hfm = new HorizontalFieldManager();
callbmp = new BitmapField(ei.getBitmap(),Field.FOCUSABLE |BitmapField.FIELD_LEFT);
LabelField NAME = new LabelField("mylable", LabelField.FIELD_HCENTER);
mailbmp = new BitmapField(mail.getBitmap(),Field.FOCUSABLE|BitmapField.FIELD_RIGHT);
hfm.add(callbmp);
hfm.add(NAME);
hfm.add(mailbmp);
add(hfm);
Manager customManager = new Manager(0)
{
protected void sublayout(int width, int height) {
setPositionChild(
getField(0),
0,
0);
layoutChild(
getField(0),
getField(0).getPreferredWidth(),
getField(0).getPreferredHeight());
setPositionChild(
getField(1),
Graphics.getScreenWidth()/2 - getField(1).getPreferredWidth()/2,
0);
layoutChild(
getField(1),
getField(1).getPreferredWidth(),
getField(1).getPreferredHeight());
setPositionChild(
getField(2),
Graphics.getScreenWidth() - getField(2).getPreferredWidth(),
0);
layoutChild(
getField(2),
getField(2).getPreferredWidth(),
getField(2).getPreferredHeight());
setExtent(width, height);
}
};
customManager.add(new BitmapField(Bitmap.getBitmapResource("image1.png")));
customManager.add(new LabelField("Hello Alignment"));
customManager.add(new BitmapField(Bitmap.getBitmapResource("image2.png")));
HorizontalFieldManager lays out the fields left-to-right in the order in which they are added. The style bits for horizontal layout are ignored.
If you want left, right and center on a horizontal line, you'll need a custom manager.
This should be your requirement:
It can be done simply by subtracting the widths of the items you are adding on your horizontal field manager. By default the leftButton or the first item you add on the HFM will be added on left. Then you can add your label(userName) and the rightButton in the following way:
LabelField userName = new LabelField("MaheshBabu");
HorizontalFieldManager horizontalBar = new HorizontalFieldManager(USE_ALL_WIDTH|Manager.NO_HORIZONTAL_SCROLL|Manager.NO_HORIZONTAL_SCROLLBAR);
horizontalBar.setBackground(BackgroundFactory.createSolidBackground(Color.BLACK));
rightButton.setMargin(0, 0, 0, Display.getWidth()-rightButton.getPreferredWidth()-leftButton.getPreferredWidth()-userName.getPreferredWidth());
horizontalBar.add(leftButton);
horizontalBar.add(userName);
horizontalBar.add(rightButton);

Can't align fields horizontally on custom pop up screen in Blackberry

What I want to achieve is to have a custom popup screen with specified width and height.
On that screen I add two buttons which stay in one row and align center horizontally.
public class CustomPopupScreen extends PopupScreen {
ButtonField minimizeBf;
ButtonField cancelBf;
HorizontalFieldManager hfm;
public CustomPopupScreen() {
super(new VerticalFieldManager());
minimizeBf = new ButtonField("Minimize", FIELD_HCENTER|Field.USE_ALL_WIDTH);
cancelBf = new ButtonField("Cancel", FIELD_HCENTER|Field.USE_ALL_WIDTH);
hfm = new HorizontalFieldManager(FIELD_HCENTER|Field.USE_ALL_WIDTH);
hfm.add(minimizeBf);
hfm.add(cancelBf);
add(hfm);
//add(minimizeBf);
//add(cancelBf);
}
protected void sublayout(int width, int height) {
// TODO Auto-generated method stub
int xPos = 0;
int yPos = 0;
int sideMargin = 30;
int screenWidth = width - (2 * sideMargin);
int screenHeight = 100;
layoutDelegate(screenWidth, screenHeight);
//setPositionDelegate(0, 0);
super.sublayout(screenWidth, screenHeight);
setExtent(screenWidth, screenHeight);
xPos = (width - screenWidth) / 2;
yPos = (height - screenHeight) / 2;
setPosition(xPos, yPos);
// layout(screenWidth, screenHeight);
}
}
If I add those buttons to the screen, then it will align center horizontally, but the buttons appear on different row, while I want it to appear in the same row.
Can somebody tell me where have I code wrong ?
Try removing the USE_ALL_WIDTH flag from your HorizontalFieldManager allocation. That should do the trick.
Try putting a couple of vertical field managers, each taking half the available width, inside the horizontal manager. then add one button to each of the vertical managers.
I was just wondering the purpose of having a custom size. There is often a correct way to do something without having to modify the layout manually. If you share your end goal, we may be able to help better. Since it almost appears that you want to simply make two buttons be side to side. To add width and height you can add spacers (add(new SeparatorField())) to the field managers and specify their width/height as well as where in the manager you want them. This would allow all the fields to use their default layout patterns.
just do one
hfm = new HorizontalFieldManager(USE_ALL_WIDTH);
i think it might help you just give it a try.

Placement of ButtonField

i want to place the Buttons as displayed in the image:
alt text http://www.freeimagehosting.net/uploads/413b8990b3.png
for this i am using the following code but it is not working for me can any one please help me out
topbarManager = new HorizontalFieldManager();
topbarLeftManager = new HorizontalFieldManager(Field.FIELD_LEFT);
topbarRightManager = new HorizontalFieldManager(Field.FIELD_RIGHT);
topbarCenterManager = new HorizontalFieldManager(Field.FIELD_HCENTER);
topbarLeftManager.add(new ButtonField("first"));
topbarRightManager.add(new ButtonField("second"));
topbarCenterManager.add(new ButtonField("third"));
topbarManager.add(topbarLeftManager);
topbarManager.add(topbarCenterManager);
topbarManager.add(topbarRightManager);
i am not getting the result as i required.
can any one please Help me out.
Thanks a lot
according to docs.
HorizontalFieldManager layout manager arranges UI components in a single horizontal row starting at the left side of the screen and ending at the right side of the screen. Because this layout manager arranges UI components horizontally, you cannot apply horizontal style bits to UI components (for example, Field.FIELD_LEFT, Field.FIELD_HCENTER, or Field.FIELD_RIGHT). You can apply vertical style bits (for example, Field.FIELD_TOP, Field.FIELD_BOTTOM, or Field.FIELD_VCENTER).
so if you want to see Field.FIELD_LEFT effect use
VerticalFieldManager hfm = new VerticalFieldManager(Manager.USE_ALL_WIDTH);
instead of HorizontalFieldManager, you will see the stair case like effect.
To layout UI best thing is use custom layout
HorizontalFieldManager hfm = new HorizontalFieldManager(Manager.USE_ALL_WIDTH){
protected void sublayout(int maxWidth, int maxHeight) {
Field f;
int w = Display.getWidth();
int x=0;
for (int i = 0; i < getFieldCount(); i++) {
f = getField(i);
layoutChild(f, f.getPreferredWidth(), f.getPreferredHeight());
setPositionChild(f, x, 0);
x+=(w-f.getWidth())/2;
}
setExtent(maxWidth, maxHeight);
}
};

Resources