Setting padding in a blackberry app - blackberry

I'm trying to set the padding between various fields in a blackberry app. I can use setpadding method but this seems like overkill for what im trying to achieve. I just want to set a consistent spacing around all fields. Is this possbible without using setpadding? My fields are part of a verticalfield managers if that helps.
Thanks

If these are Fields you will be using frequently with the same amount of spacing the easiest solution would probably to extend the Field and in the constructor call a setMargin() on it.
public class SpacedLabelField extends LabelField {
public SpacedLabelField(String text, long style) {
super(text, style);
setMargin(10, 10, 10, 10);
}
}
I would recommend setMargin() because the Managers will take that information and give you an even spacing. For example, you have Field A with a bottom margin of 10 and Field B with a top margin of 10. If you stack A on top of B you will end up with a spacing between them of 10 rather than 20 (at least this has been my observed behavior in 5.0).
At any rate, now rather than having to create your Field and then call setMargin() on each, you only have to call your class and it will already be formatted for you.
Alternatively you could create your own Manager and in the sublayout() call implement a spacing between the Field when you are laying out and positioning them.

Related

Field having multiple attributes

How to create fields having multiple attributes(each Field containing multiline texts,image icon,date etc) in an Application.??
What exactly they are called as???
In the BlackBerry Java UI framework, the fundamental class is a Field. A Field might be a TextField or BitmapField or ButtonField.
Managers
If you want to create a field that is a group of fields, you would normally use or create a class that extends Manager. A Manager is a container of field objects, that is responsible for properly laying out (sizing and positioning) the fields it contains.
Also, a Manager is itself a Field in that it extends Field. So, you can have a heirarchy of field objects, and managers. A Manager can contain child Manager objects, which themselves contain low-level fields: buttons, labels, sliders, etc.
You can extend the Manager class yourself, and implement the methods to properly size and layout its children,
protected abstract void sublayout(int width, int height);
public int getPreferredWidth();
public int getPreferredHeight();
or you can use one of the pre-written RIM managers, like VerticalFieldManager, or HoriztonalFieldManager.
VerticalFieldManager, for example, lays out all Field objects that you add() to it, in order, vertically from top to bottom. You can adjust spacing on the child fields by calling setMargin() on each field.
Some other built-in Manager subclasses:
HorizontalFieldManager - lays out child fields horizontally, in the order added, from left to right
FlowFieldManager - lays out child fields horizontally, until horizontal space is full, and then vertically (like the words on a page flow)
AbsoluteFieldManager - lays out child fields at fixed (x,y) screen coordinates
GridFieldManager - lays out child fields in a grid of rows and columns ... like a table

setMargin and setPadding blackberry API

I have been trying some UI stuff on BB devices and I'm a bit confused between setMargin and setPadding functions of the Field class. I know their definitions but I dont get when I'm supposed to use either of one for any field.
Any help, examples would be highly appreciated!
Thanks!!
Difference between setMargin() and setPadding() will be clear by the fig. below
setMargin() sets the margin of one field from the other field as shown in fig.1
and setPadding() increasse the area of the field on which you are setting the padding. As shown in fig.2 the extent of Button2 become increased, while in setMargin(), the extent of field remains same and the spacing between the field become changed.

BlackBerry API 6.0. How to make a LabelField wider (to make it stop enforcing new lines)?

I have a class which extends LabelField to set font and color.
I have 2 such custom LabelFields and they display some strings of text. Those Strings are rather short, about a dozen symbols long.
The problem is that these custom LabelFields enforce new lines. They break my strings and display them in 2 lines instead of only 1 (no matter if a String is 6 or 12 characters long).
How to make these LabelFields stop enforcing new lines?
I need to have one line stay one line.
Update:
I have found the reason of that problem.
The problem was on another level, - in the process of calculation of width and height for my popup Screen which contained those LabelFields.
you can use the style LabelField.ELLIPSIS to force the single line.
like
LabelField l = new LabelField("your text",LabelField.ELLIPSIS);

Blackberry - Smileys in text field

I'm looking for a way to insert emoticons/smileys/any bitmap in a text field (LabelField, EditField, whatever). From what I've researched so far, there is no direct way to do this, so I'm stuck with trying to simulate this behavior.
I was thinking of using a normal text field and instead of smileys to insert blank spaces and then place some BitmapFields over the text field where the smileys should be. The problem is that I have no reliable/quick way of finding a character's position on screen (x,y).
Can you give me some ideas?
Can you think of other approaches to this problem?
I'm sure a lot of people encountered this kind of problem at one time or another. I hope there is someone who managed to find a solution. I'm desperate enough to accept any solution, no matter how crazy/complex/difficult.
Also, I would like to use this in SDK 5.0 and up, but I will settle for only 6.0.
What you might be able to do is create a custom Manager that is a container for EditFields and BitmapFields. When the user clicks in the initial EditField that is inside of your Manager and begins typing, you let them type until they want an emoticon. When they select it you create a BitmapField right after the EditField and then place a new EditField to the right of the BitmapField. You'll have to keep track of things like when the user hits backspace and empties an EditField, it should be removed, and then they are selecting the BitmapField, and a subsequent backspace would delete the BitmapField and put focus on the previous EditField.
You will have to also create your own EditField that you can control the size of, and BitmapField that allows for backspace to delete it.
As far as I can think ahead, you shouldn't have any problem using this for 5.0
Edit for comments:
You'll have to be implementing your own sublayout() of your Manager in any approach you take, so you know positions because you're the one who put them there. If you want to want to do multi-line (my approach was for single-line), you can do one of three things:
1.) Just have new EditFields for each line and do the linking the same way I talked about where backspace drops you to the previous Field, in this case the last line. You have to keep measurements of how big the field is based off of the text, and see if that will move it to the next line. If it does just adjust where the EditFields are positioned.
2.) You can do your image placement in the multi-line field by implementing your own EditField that expands height, in which case you'll know where the text is and what lines you are on. Drawing the Bitmap would be a matter of calculating (what line number you are) * (font height) and getAdvance(text_up_to_bitmap).
3.) Implement your own EditField where you have a focusable Manager that you manually do all of the drawing. You can parse a String that you save from capturing key strokes and maybe have a flag where an emoticon should be. Draw the text before it, draw the emoticon, then continue drawing the text. The difficulty with this is you will have to do things like determining where the cursor should be, bringing up the virtual keyboard, and handling some of the other tasks that the EditField does for you.

Add gui components from bottom up instead of top down

Is it possible to add gui components to blackberry screen beginning from the bottom instead of the top ?
Thanks
A quick response would be no but let me explain why and suggest afew work arounds;
Screens don't actually handle the laying out of fields onto themselves, to do this they delcare a delegate manager which can be any type of manager, vertical, horizontal etc. The problem is all managers begin painting themselves from the top left. For a manager to paint fields starting from the bottom it would have to know exaclty where the bottom is located and add components up rather than down which goes against all the low level code inside the manager class. You can read more on managers in the BlackBerry API documentation.
You could still achieve an effect similar to this though by tweaking how you add fields and playing with field styles. For example consider this code:
add(new LabelField("field 1"));
add(new LabelField("field 2"));
This would give us the results;
field 1
field 2
because field 1 is drawn then field 2 below it. However if we were always to insert fields at the begining of our manager e.g. position 0 like so:
insert(new LabelField("field 1", FIELD_BOTTOM), 0);
insert(new LabelField("field 2", FIELD_BOTTOM), 0);
We would get the results;
field 2
field 1
Which is the results you'd expect from a screen described in your question.
I'm not really sure how you'd get the fields to paint to the bottom of a screen though, you could try researching the "position relative bottom" styles but I'm honestly unsure.
You are probably using a VerticalFieldManager, and the documentation on that says:
A vertical field manager lays out
fields top to bottom in a single
column.
So if you
manager.add(field1);
manager.add(field2);
manager.add(field3);
The order of the fields on the screen will be just that.
But you could do something like this:
Vector v = new Vector();
v.add(field1);
v.add(field2);
v.add(field3);
for(int i=v.size()-1;i>=0;i--) {
manager.add((Field)v.elementAt(i));
}
Sort of. You can use the Manager#insert(Field, int) method and always insert at the zero index. If you do this with a VerticalFieldManager, it would simulate a bottom-up adding of Fields to the Manager.
Some of the answers so far are to use Manager.insert(Field, int), and keep inserting at position 0. This will work, but the running time of the insert is linear in the number of elements already added to the manager. Meaning this solution will have an overall quadratic running time. Not a big deal if you're adding under 10 fields, but if you're planning on adding more than that, the insert overhead will be substantial.
If you can do the inserts top to bottom, by reordering the fields as Muger's solution suggests, the running time will be much improved.
Finally, you can write your own BottomUpVerticalFieldManager that does the layout the way you want. When you write your own manager, you can layout the fields in whatever way pleases you. In this case, it would be bottom to top. Writing your own manager may seem daunting, but it will give you considerable freedom in the future when trying to solve layout issues.

Resources