not display cursor in custom BasicEditField bb - blackberry

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...

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

How to move drop down left in blackberry + java

I am using ObjectChoiceField example. it is showing drop down at the end of right side, but i need to show some margin on the left side .
how we can achieve this ?
String choices[] = {"Monday","Tuesday","Wednesday","Thursday","Friday", "Saturday", "Sunday"};
int iSetTo = 0;
VerticalFieldManager vfm=new VerticalFieldManager(){
protected void paint(Graphics graphics) {
graphics.setBackgroundColor(Color.BLACK);
graphics.clear();
super.paint(graphics);
}
};
vfm.add(new ObjectChoiceField("Day of the week",choices,iSetTo){
protected void paint(Graphics graphics) {
graphics.setColor(Color.WHITE);
super.paint(graphics);
}
});
I am using this example:
![enter image description here][1]
You can do this with many fields by setting their margin. Field#setMargin() has been available since BBOS 6.0 officially, but was undocumented and usable in 5.0 (at least), too. So, at this point, I consider it perfectly acceptable to use in 5.0+ apps.
For example:
public ChoiceMarginScreen() {
super(MainScreen.VERTICAL_SCROLL | MainScreen.VERTICAL_SCROLLBAR);
String choices[] = {"Monday","Tuesday","Wednesday","Thursday","Friday", "Saturday", "Sunday"};
int iSetTo = 0;
VerticalFieldManager vfm = new VerticalFieldManager();
vfm.setBackground(BackgroundFactory.createSolidBackground(Color.BLACK));
ObjectChoiceField days = new ObjectChoiceField("Day of the week", choices, iSetTo) {
protected void paint(Graphics graphics) {
graphics.setColor(Color.WHITE);
super.paint(graphics);
}
};
// set margin XYEdges(top, right, bottom, left):
days.setMargin(new XYEdges(0, 150, 0, 0));
vfm.add(days);
add(vfm);
}
Note that you also don't need to override the VerticalFieldManager#paint() method just to set a solid background color. You can use setBackground() for that. For setting the white font color on your object choice field, though, overriding paint() is a fine solution.
Also: I just went off your screen shot, and assumed you wanted the choice field moved to the left, but with no other field to the right of it. If you want the choice field to the left to make room for another field on its right, then you probably want to put both those fields in a HorizontalFieldManager, representing a row, and then add that HorizontalFieldManager to your VerticalFieldManager. But, that's not what you showed, so I offer my answer above.

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 setting the position of a RichtextField in FullScreen

I am writing an application in BlackBerry, where I want to do some custom painting at the top portion of the screen in the paint method of FullScreen and at the same time, I want a RichtextField positioned at the lower portion of the screen. I tried using setPosition methods in the Field class, but to no avail. So how do I set the position of the RichtextField that is added to the FullScreen class?
You can use a SpacerField for that purpose:
class SpacerField extends Field {
int localWidth, localHeight;
SpacerField(int width, int height) {
super(Field.NON_FOCUSABLE);
localWidth = width;
localHeight = height;
}
protected void layout(int width, int height) {
// TODO Auto-generated method stub
setExtent(localWidth, localHeight);
}
protected void paint(Graphics graphics) {
}
public int getPreferredWidth() {
return localWidth;
}
public int getPreferredHeight() {
return localHeight;
}
}
and add it to your Screen before your RichTextField. Be sure to give a suitable width (Display.getWidth() ?) and height when constructing the SpacerField.
Note: I had found the code at this forum discussion a few months ago when I needed to do something similar.
The best way to position objects is to extend a Manager and use it to position and size the objects the way you want. Check the documentation for net.rim.device.api.ui.Manager and net.rim.device.api.ui.Field for information on how manager control their children.

Resources