Hi i want to create a multiline Custom Label in Blackberry I have created a Custom Dialog Box which has either one or two buttons and to show the message i have added a LabelField But, the problem is if the message text gets too long than it goes out of the custom dialog boundary i need some customised label which puts the message in next line for the width i might define for label.
Asked
Active
Viewed 609 times
1 Answers
3
You should add yourLabelField
to a VerticalFieldManager
. This answer is based on my answer here. You can change the width of the label field by modifying the maxWidth
variable in the manager's sublayout()
method.
public final class CustomScreen extends MainScreen {
public CustomScreen() {
String longString = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean vel lorem eget tortor hendrerit adipiscing. Curabitur mollis pellentesque est ac.";
VerticalFieldManager vfm = new VerticalFieldManager(Manager.USE_ALL_WIDTH | Manager.NO_HORIZONTAL_SCROLL) {
protected void sublayout(int maxWidth, int maxHeight) {
super.sublayout(maxWidth, maxHeight);
};
};
vfm.add(new LabelField(longString));
add(vfm);
}
}
This code snippet produces
Modifying maxWidth
to Display.getWidth()/2
(or maxWidth/2
) in the manager's sublayout()
method produces
UPDATE (as Yatin suggested): One can achieve the same functionality without using VerticalFieldManager
but than vertical scrolling will not be available in case the text doesn't fit vertically.
public final class CustomScreen extends MainScreen {
public CustomScreen() {
String longString = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean vel lorem eget tortor hendrerit adipiscing. Curabitur mollis pellentesque est ac.";
add(new LabelField(longString) {
protected void layout(int width, int height) {
super.layout(width/2, height);
}
});
}
}

Community
- 1
- 1

tonymontana
- 5,728
- 4
- 34
- 53
-
I find this code curious. In the snippet you show, you're overriding `sublayout()`, but calling `super.sublayout()` with the same parameters passed to you. In which case, what's the point of the custom `VerticalFieldManager`? I guess maybe you were setting it up to show how the **bottom** screenshot could be achieved by using maxWidth/2? But, that still seems strange. If you only want a half-width `LabelField`, why would you nest it in a full-width `Manager`? It seems to me that the `Manager` should be half-width itself if its content is going to be constrained in that way. – Nate Jun 24 '12 at 09:27
-
1@Nate I've set it up for the second example (I emphasized that *"You can change the width of the label field by modifying the maxWidth variable in the manager's sublayout() method"*). Otherwise, there is no use for that. – tonymontana Jun 24 '12 at 10:02
-
i have a curiosity whether this can be achieved just by overriding the sublayout or layout method (which ever is applicable )of LabelField and not Vertical field if one has to add other elements in VerticalFieldManager – Yatin Jun 24 '12 at 11:41
-
1@YatinK Actually, you can. You can override the `layout()` method of the `LabelField` in the same manner as I did for the `VerticalFieldManager` but, you will loose the vertical scrolling functionality (in case the text is very long). – tonymontana Jun 24 '12 at 12:11
-
@YatinK I've updated my answer with an example that doesn't use `VerticalFieldManager`. – tonymontana Jun 24 '12 at 12:17