how to set width of DropDown window in object choice field? customized - blackberry

i am using an ObjectChoiceField in blackberry now. I have items in String array like "a", "abc", "abcdefg" etc. The application automatically sets the width based on the width of the choice items. I want the dropdown window in ChoiceField having width static which can hold the largest entry in the string array.
My question is, Can i set the dropdown window size? if yes, How could i set the width of the DropDown window (which displays all choice items of your choice field)?

Your question is not clear. In the current implementation, the drop down size is the size of the largest elements.
My guess is you want the size of the "highlighted" element after selection to be of the size of largest element?
And here it is smaller
Clarify what exactly you intend to do?

i had created a custom choice field..
import net.rim.device.api.ui.component.ObjectChoiceField;
/*
* The MyChoiceField is actually an extended class of ObjectChoiceField.
* In this, the design of an ObjectChoiceField is overloaded with providing some additional
* tasks is done. It includes a constructor and a Layout methods.
* In that Layout method the design for the ObjectChoiceField is overrided...
*/
public class MyChoiceField extends ObjectChoiceField
{
public MyChoiceField(String label ,Object[] choices)
{
super(label, choices);
}
protected void layout(int width, int height)
{
setMinimalWidth(width/2-62);
super.layout(width, height);
}
}
Now i got it...
thanks....

Related

customized editable text field with predefined width and height

i am developing a mobile app in blackberry 7,i need to create a editable text field as shown in below figure with save and clear button.initially it has to show customized edittext field with predefined width(fixed as it should not exceed the defined layout) and height,and automatically get appended by new line if user requires to enter more characters after reaching predefined space as user keeps filling the field.
i googled, but i did not get any source which is similar to this.please help me by providing any suggestion or with samples
Blackberry fields decide their size in their layout field. I'm not entirely sure what EditField does in its layout, but I was able to get the behaviour you want by setting the extent. Every time the edit field text will wrap, layout will be triggered so that it can grow.
EditField editField = new EditField()
{
private final int MIN_HEIGHT = 200;
protected void layout(int width, int height)
{
super.layout(width, height);
if (getHeight() < MIN_HEIGHT)
{
setExtent(getWidth(), MIN_HEIGHT);
}
}
};
editField.setBorder(BorderFactory.createSimpleBorder(new XYEdges(1, 1, 1, 1)));
add(editField);

Unable to change width of TextField in Blackberry

I am a newbie to Blackberry, I was just trying out some sample apps in Blackberry. I tried to create a login page. In that, when I tried to change the width of the text field, the text field became invisible.
The below is part of the code to create the TextField.
super(Field.FIELD_VCENTER);
......
t_username = new TextField()
{
public void layout(int width, int height)
{
super.layout(500, 30);
setExtent(500, 30);
}
};
t_username.setMaxSize(10);
t_username.setBorder(BorderFactory.createSimpleBorder(new XYEdges(1,1,1,1),Border.STYLE_SOLID));
I tried to create a border to check where it is coming or not, but couldn't find it.
PFB, the snapshot:
Overriding the TextField#layout() method
public void layout(int width, int height)
{
super.layout(500, 30);
setExtent(500, 30);
}
is one way that you can set a text field's width. So, I think there's something else going wrong here.
1) Possibly, when you changed your code, your mistakenly removed the call to
add(t_username);
you don't show us where you actually add that field, so if you're not calling add(t_username) somewhere else, it's not going to be visible. Fields must be added to a Manager or Screen to be visible.
2) Perhaps some other code you've written (but not shown) is attempting to do something with a Graphics object. For example, if you're overriding a paint(Graphics) method in another field, you may be changing a color (e.g. graphics.setColor(Color.WHITE)) and not remembering to reset the original color. Possibly, your text field is there, but it's the same color as its background. If this was happening, though, you could still see the text field cursor when you give that field focus. I simply can't tell from your screenshot.
A Better Way
Normally (but not always), it should be the responsibility of the Manager/Screen that contains the field to determine its size. I think it's poor encapsulation to have most fields set their own width (although there are exceptions to this). I would recommend using setMargin() and the USE_ALL_WIDTH flag to set a reasonable width for this text field:
public class TextFieldScreen extends MainScreen {
private TextField t_username;
public TextFieldScreen() {
super(Field.FIELD_VCENTER);
HorizontalFieldManager row = new HorizontalFieldManager();
LabelField label = new LabelField("Username");
label.setMargin(new XYEdges(2, 0, 2, 10));
row.add(label);
t_username = new TextField(TextField.USE_ALL_WIDTH);
t_username.setMaxSize(10);
t_username.setBorder(BorderFactory.createSimpleBorder(new XYEdges(1,1,1,1), Border.STYLE_SOLID));
t_username.setMargin(new XYEdges(2, 10, 2, 10));
row.add(t_username);
add(row);
}
}
One benefit of this solution, compared to hard-coding a width of 500, is that if your app supports portrait/landscape rotation, the code above will correctly adjust the field width as the screen width changes. If you hard-code the field width, the field will be too wide when the device rotates to portrait.

How to Increase the row height of the selected item of listfield

I am using a ListField for my app to show the data in a list. Now my requirement is that i want to increase the row height of the selected item of the list.
For this, i created a GridFieldManager and populated my list using this manager. I then override the onFocus() method of this manager. But, this method is never invoked. As a result i am unable to increase the height of the selected row item.
Please help.
ListField rows are all designed to be uniformly sized, so unfortunately you won't be able to directly change the size of one row. It's possible you can emulate it by setting it so that the row above and below draw a little bit of the focus color at the bottom and top respectively. Alternatively, you could have one field that just stays centered above whatever row is focused. Then redraw this "floating Field" with the information from the selected row.
I got it working. I faked the ListField implementation. I removed ListField and replaced it with VerticalFieldManager. I adjusted its size during onfocus() and onUnfocus() and used sublayout() to change the height of this manager. Its working exactly the way i want.
you can increase the row height/font text/...
as ultimately, all of the api's call canvas's paint methods
how to do:
//try playing with the font_type,font height
Font myFont = Font.getDefault().derive(Font.PLAIN, 14, Ui.UNITS_px);
private class ListCallback implements ListFieldCallback {
public void drawListRow(ListField list, Graphics g, int index, int y, int w) {
g.setFont(myFont);
String text = (String)listElements.elementAt(index);
g.drawText(text, 0, y, 0, w);
//you can increase the height of a particular row in the height parameter.
// if you want it to be specific, put an 'if conditon'
EX: if(index=1)
{
w+=10;
}
}
}

How to create a custom dialog

I need to create yes/no confirmation dialog in a foreign language. I think I need to create my own class by extending Dialog ?
Thanks
this should do the trick. My apologies to any Swedish chefs watching.
int answer = Dialog.ask("Gersh gurndy morn-dee burn-dee, burn-dee, flip-flip-flip-flip-flip-flip-flip-flip-flip?", new String[] {"Hokey dokey","Bork bork bork"}, new int[] {Dialog.OK,Dialog.CANCEL}, Dialog.CANCEL);
Edits:
The above explained better:
public final static int NEGATIVE = 0;
public final static int AFIRMATIVE = 1;
public final static int DEFAULT = NEGATIVE;
int answer = Dialog.ask("question?", new String[] {"afirmative button label", "negative button label"}, new int[] {AFIRMATIVE,NEGATIVE}, DEFAULT);
As you can see from the above it is possible to change all the text (language) values on a Dialog just by using this method so you shouldn't need a custom class to create a Dialog in another language.
It's even simpler if you use the standard BB localizing approach the simpler method (Dialog.ask(res.getString(SOMEQUESTION)) will automatically have it's afirmative and negative buttons adjusted for the language set in the phones options. You will only need to add the question as a string resource.
You can find a list of valid methods and constructors here:
http://www.blackberry.com/developers/docs/5.0.0api/net/rim/device/api/ui/component/Dialog.html
More Edits below:
I thought my above answer was what you were after but if you do need to further customize the dialog in a new class you may do it like this:
public class MyDialogScreen extends MainScreen implements LocalResource {
private int exitState;
...
protected void sublayout( int width, int height ) {
setExtent( dialogWidth, dialogHeight );
setPosition( XPOS, YPOS );
layoutDelegate( dialogWidth, dialogHeight );
}
// do some stuff and assign exitState appropriately
// e.g. a button that sets exitState = 1 then pops this screen
// another button that sets exitState = 2 then pops this screen
...
public int getExitState()
{
return this.exitState;
}
In the above I've created a new screen and I have overridden the sublayout method to specify a custom width, height and xy positions in layoutDelegate. When you push this screen you will see it as a dialog like box above the previous screen on the stack at the XY positions you specified.
Make sure to use pushModal. This will allow you to access the getExitState method after the screen has been popped from the display stack.
E.g
MyDialogScreen dialog = new MyDialogScreen();
UiApplication.getUiApplication().pushModalScreen(dialog);
int result = dialog.getExitState();
Cheers
Ray

Add vertical spacing between fields on VerticalFieldManager

I am adding fields to a VerticalFieldManager. Is there a method of adding vertical spacing between fields?
There's several solutions to this, one being that you can create a custom field to be used as a spacer between your other fields.
private static class SpacerField extends Field
{
private int spacerWidth;
private int spacerHeight;
private SpacerField(int width, int height) {
spacerWidth = width;
spacerHeight = height;
}
protected void layout(int width, int height) {
setExtent(getPreferredWidth(), getPreferredHeight());
}
protected void paint(Graphics g) {
// nothing to paint; this is a blank field
}
public int getPreferredHeight() {
return spacerHeight;
}
public int getPreferredWidth() {
return spacerWidth;
}
}
//...
// Usage
add(new LabelField("Before Spacer"));
add(new SpacerField(0, 100));
add(new LabelField("After Spacer"));
Setting the padding or margins of your contained fields is another solution. It's up to you on what you think is the best way of managing things.
There are more elogant ways of doing this using the setPositionChild() methods but a simple work around is to give your fields padding using the setPadding(int top, int right, int bottom, int left) method.
myField.setPadding(5, 0, 5, 0);
I actually find that most times, what you want to do is call setMargin() on the vertical field manager's child fields, not sePadding(). I certainly think creating a SpacerField is now unnecessarily complex.
myField.setMargin(5, 0, 5, 0);
It depends on which kind of child field we're talking about, and how its size is specified, but in general, setPadding() will actually make the field bigger, which could have a visual impact, depending on how the field's background and border are drawn. setMargin() does not make the field any bigger, which I think is more consistent with what the question is asking.
See Mr Vincenzo's answer here, with pictures!
Note: setMargin() and setPadding() were officially added to the Field API relatively late in the BlackBerry Java evolution, but were available as undocumented methods before that.
Another Note: I have seen strange problems when using setMargin(), like the one described in this question. In that case, I did have to resort to one of the other two solutions here.

Resources