how to add checkbox, labelField to HorizontalFieldManager - blackberry

I wrote a simple program that adds two edit fields to the field manager:
HorizontalFieldManager hrfm = new HorizontalFieldManager(Manager.HORIZONTAL_SCROLL);
EditField editField1 = new EditField();
editField1.setText("User Name:");
EditField editField2 = new EditField();
editField2.setText("Hello");
hrfm.add(editField1);
hrfm.add(editField2);
add(hrfm);
But when i run the emulator it is displaying only UserName field only. I am unable to find the other edit field. Why is this problem occuring. I also faced the similar problem while adding checkbox, labelField. Please help me on using this FieldManager. Thank you

Check How to - Implement advanced buttons, fields, and managers.
There is JustifiedHorizontalFieldManager - it should solve your need.

Hope this will helps you.
EditField editField1 = new EditField();
editField1.setText("User Name:");
EditField editField2 = new EditField();
editField2.setText("Hello");
int Width = editField1.getPrefferedWidth()+editField2.getPrefferedWidth();
int Height = editField1.getPrefferedHeight()+editField2.getPrefferedHeight();
HorizontalFieldManager hrfm = new HorizontalFieldManager(Manager.HORIZONTAL_SCROLL)
{
protected void sublayout(int maxWidth, int maxHeight) {
super.sublayout(Width, Height);
super.setExtent(Width, Height)
}
}
hrfm.add(editField1);
hrfm.add(editField2);
add(hrfm);

Related

Set the text in the center of the screen in Button Field in Blackberry?

I am an an Android Developer, developing an Blackberry application.
I have created a button of full width of screen. Getting problem in shifting the text to the center of the button area.
Used below code :
ButtonField _contactButton = new ButtonField(Constants.contactButtonTitle,Field.FIELD_HCENTER|Field.USE_ALL_WIDTH |
Field.ACTION_INVOKE | Field.FOCUSABLE | ButtonField.CONSUME_CLICK){
protected void layout(int width, int height) {
super.layout(width, height);
HARD_CODED_HEIGHT = this.getHeight()/2 + 6;
this.setExtent(contactButtonWidth, HARD_CODED_HEIGHT);
}
public int getPreferredWidth() {
return contactButtonWidth;
}
};
Now using the below code :
ButtonField _contactButton = new ButtonField(Constants.contactButtonTitle,Field.FIELD_VCENTER|Field.USE_ALL_WIDTH |
Field.ACTION_INVOKE | Field.FOCUSABLE | ButtonField.CONSUME_CLICK){
protected void layout(int width, int height) {
super.layout(getPreferredWidth(), height);
}
public int getPreferredWidth() {
return (Display.getWidth()-60);
}
};
Still getting the issue .. text of my button align to right corner. Please suggest
ButtonField appears to be a little 'broken'. But it also appears to be consistently broken in all the OS Levels that I have tested (OS 5.0 to OS 7.1), so I think we can achieve what you want by working round the broken bits and be confident the workaround will work in all levels you want.
As has been noted, ButtonField ignores USE_ALL_WIDTH, but does respect preferredWidth. So if you want to set the width of your ButtonField, then just override getPreferredWidth(). You should not do anything with width in layout.
Now you are using the styles for ButtonField already. Given that we have discarded USE_ALL_WIDTH as a useful style, I note that you also use FIELD_HCENTER. You should be aware that this is actually a directive to the Manager that is positioning this Field - telling the Manager to position the Field in the centre of the width the Manager has available. This style does not relate to how the contents of the ButtonField are drawn.
For that, you can look to use DrawStyle. By default, ButtonField uses DrawStyle.RIGHT. And it respects DrawStyle.Left - the text will be drawn on the left. It does not however, respect DrawStyle.HCENTER. So to get centred text, we need to paint the text ourselves.
There is one more complication. ButtonField passes a Context area into its paint() method, not the full Field canvas - presumably it does not pass in the edges because these are painted by a border. So to centre the text appropriately, we have to use the clipping region that has been passed in.
Here is the final, hopefully working, ButtonField. I appreciate you will have to spend some time creating a class for this, I'm sorry, I've been lazy and done in it 'in-line'. Please publish your CenteredTextButtonField class if you create one....
final String buttonText = "Test";
ButtonField _contactButton = new ButtonField(" ",
Field.ACTION_INVOKE | Field.FOCUSABLE | ButtonField.CONSUME_CLICK){
public int getPreferredWidth() {
return contactButtonWidth;
}
protected void paint(Graphics graphics) {
super.paint(graphics);
XYRect clippingRect = graphics.getClippingRect();
int centreY = (clippingRect.height - graphics.getFont().getHeight()) / 2;
graphics.drawText(buttonText, 0, centreY, DrawStyle.HCENTER, clippingRect.width);
}
};
USE_ALL_WIDTH is our instruction to the field. Surprisingly, ButtonField ignores such instructions. Even more surprisingly, it honors its own getPreferredWidth (as illogical as it sounds).
So drop that USE_ALL_WIDTH and define your ButtonField like this:
ButtonField testBtn = new ButtonField("Button") {
public int getPreferredWidth() {
return Display.getWidth();
}
};

BlackBerry: setting the height of a field manager without manually building the layout

I want to set the height of a manager so that my tab bar sits perfectly beneath the manager at the bottom of the screen.
I'd add my vertical field manager which holds all the content, then I add(tabbar).
The problem is that when I use the following, all the fields disappear. However, the height is set the way I want it.
bottom_vfm = new VerticalFieldManager() {
protected void sublayout(int maxWidth, int maxHeight) {
setExtent(maxWidth, 200);
}
};
Do I have to manually setChildPosition and layoutChild for every field? Is there any way around it?
have u tried like this?
bottom_vfm = new VerticalFieldManager(){
protected void sublayout(int maxWidth, int maxHeight){
super.sublayout(maxWidth,200);
setExtent(maxWidth,200);
}
};

not display cursor in custom BasicEditField bb

please check it
BasicEditField demo = ew BasicEditField("", number, 15,
BasicEditField.FILTER_NUMERIC
| BasicEditField.FIELD_LEFT) {
public int getPreferredWidth() {
int Width = Graphics.getScreenWidth() - 180;
return Width;
}
public int getPreferredHeight() {
return 30;
}
public void paint(Graphics g) {
g.setColor(Color.LINEN);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
g.setColor(Color.BLUE);
g.drawText(getText(), 0, 0);
super.paint(g);
}
protected void layout(int arg0, int arg1) {
super.layout(getPreferredWidth(), getPreferredHeight());
super.setExtent(getPreferredWidth(), getPreferredHeight());
}
};
this is my code help me out?
Kalpana, I checked your code. Yes, It is not showing cursor. I suggest you to use EditField instead of BasicEditField. You can override these methods for Editfield also. I tried it and it is showing cursor.
I think I may have solved this by adding another field to the manager before this custom BasicEditField. Add a field that doesn't do anything. Something like this:
BitmapField bugFix = new BitmapField(Bitmap.getBitmapResource("empty_image.png"));
myFieldManager.add(bugFix);
myFieldManager.add(demo);
However, what I found is that the size of the dummy field (BitmapField in this case) matters. If your "empty_image.png" image is only 2px high, only the top 2px of the cursor will display. So, if you can deal with the extra padding, add a field that has at least 20px or so and the entire cursor should display. I should also add that this bug does not seem to show up on any subsequent custom BasicEditFields that you add to the manager... only the first one. Hmmm...

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);

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