How can I add a textbox so a user can enter numeric values into my form? I need the borders of the textbox control to be visible at all time.
Thanks.
You should implement your own class, inherited from TextBox and override void paint(Graphics g) method.
Smth, like that, sorry i write from mobile:
public void paint(Graphics g)
{
// set color
g.setColor(0x555555);
// draw 100*100 rectangle
g.drawRect(0, 0, 100, 100);
// dont forget to invoke
super.paint(g);
}
If you dont want use overriding,
In OS 4.6+ you can use Border and BorderFactory classes (search in All Classes list).
// Each integer represents the amount of space between the box and border
// The four parameters of an XYEdge object represents each edge,
XYEdges thickPadding = new XYEdges(10, 10, 10, 10);
// Sample text field with a thick and solid rounded border
// and single solid colour background.
RichTextField simpleField = new RichTextField("Solid rounded border, solid background");
// Create border and background objects
Border roundedBorder = BorderFactory.createRoundedBorder(thickPadding, Border.STYLE_SOLID);
// Set the objects for use
simpleField.setBorder(roundedBorder);
// Add the field to the screen
add(simpleField);
Related
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.
I have tried it overriding paint() method inside my own class which extends LabelField, but I ignore if there is another simpler way.
My code:
protected void paint(Graphics graphics) {
int previousColor = graphics.getColor();
graphics.setColor(0xFFFFFF);
graphics.drawText(getText(), 2, -2);
graphics.setColor(previousColor);
super.paint(graphics);
}
What I want to achieve is this:
EDIT: The answer by Abhisek produces the following result, in case anyone is interested:
If Abhishek's answer does not work, you can try to do it directly in the paint method. It's easy, just paint it once in the bg color, then paint it again over it (a few pixels down and left the previous text) in the fg color. Something like this:
protected void paint(Graphics graphics) {
graphics.setColor(0xFFFFFF);
graphics.drawText(getText(), 2, 0);
graphics.setColor(0x000000);
graphics.drawText(getText(), 0, 2);
}
Notice how you need 2 extra pixels of height and width, so probably you'll have to override getPreferredWidth, getPreferredHeight and/or layout.
A stab in the dark, but try deriving a font using Font.derive(style, height, units, aAntialiasMode, aEffects), pass Font.DROP_SHADOW_RIGHT_EFFECT in the aEffects parameter and apply it to the field in question.
Do tell us if it works; I haven't used it since it's undocumented!
I am trying to develop an application in which text is displayed on a background image.
Text must be displayed on an image and when the background image is changed text also must change.
To achive above requirement first you need to set Background image for your MainScreen for this there are more methods one is to override paint method of MainScreen.
Example code:
Bitmap screen1=Bitmap.getBitmapResource("Screen_1.jpg");
public void paint(Graphics graphics)
{
graphics.drawBitmap(0, 0, Display.getWidth(), Display.getHeight(),screen1, 0, 0);
super.paint(graphics);
}
};
After setting background image for screen you need to add LabelField or RichTextField to screen.
Example:
LabelField lf=new LabelField("text",Field.USE_ALL_WIDTH|DrawStyle.HCENTER);
add(lf);
When you need to change Backgound image set.
screen1=Bitmap.getBitmapResource("you image.jpg");
lf.setText("your labelfield text");
invalidate();
Hope this will help you.
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;
}
}
}
I am trying to draw a border around two text boxes, which works if I leave background colour of the main screen alone. The client's spec's call for a colour scheme with a blue background. When the EditFields are drawn to the screen, they appear as one field that spans the screen. There are 2 since each gets focus when it's supposed two and everything works otherwise. The two EditFields are then added to a GridFieldManager to control the layout.
I am subclassing the EditFields and adding the border around each of the EXEditFields, like so:
public class EXEditField extends EditField {
...
private void init( MainScreen scrn ) {
if ( this.hasVirtualKeyboard() )
this.vkbd = scrn.getVirtualKeyboard();
this.setMaxSize( this.MAX_CHARS );
this.setBorder( BorderFactory.createRoundedBorder(new XYEdges(0,0,0,0), Border.STYLE_SOLID) );
this.setBackground( BackgroundFactory.createSolidBackground(Color.WHITE) );
//this.setPadding( 3, 3, 3, 3 );
//this.setMargin( 0, 3, 0, 3 );
}
...
} // end class
Any help is greatly appreciated since there is not much in the way good Blackberry reference docs.
Ok, check this.
It is an open source library that provides some custom BlackBerry fields, including an EditField, with custom borders. You should be able to modify the code to display the borders you want.
You might try changing the paintBackground method within your custom EditField, try putting this code into your EditField class:
protected void paintBackground(Graphics graphics) {
graphics.setColor(Color.BLACK);
graphics.drawRect(0, 0, getWidth(), getHeight());
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, getWidth(), getHeight());
}