HI all i have implement Custom BasicEditField to set hint and to input long text .
please see my code
vfm_searchBox = new VerticalFieldManager()
{
//Main.Quicksearchinput is background image for inputtext
public void paint(Graphics g)
{
g.setColor(Color.WHITE);
g.drawBitmap(0,0,Main.Quicksearchinput.getWidth(),Main.Quicksearchinput.getHeight(),Main.Quicksearchinput,0,0);
super.paint(g);
}}
There is one HorizontalFieldManager to scroll text.
hfm_searchBox = new HorizontalFieldManager(Manager.HORIZONTAL_SCROLL) ;
There is one Basiceditfield to input text .
txtSearch = new BasicEditField(BasicEditField.NO_NEWLINE)
{
public void paint(Graphics g)
{
if(super.getText().length() == 0)
{
g.setColor(Color.GRAY);
g.setFont(g.getFont().derive(Font.PLAIN,18));
g.drawText("Enter Event title or subtitle", 0, 0);
super.paint(g);
}
else
{
g.setColor(Color.BLACK);
super.paint(g);
}
}};
The BasicEditField looks nice with Backgroundimage and hint but problem is that when i am nothing input in textfield it is working as endless scroll . i have set Horizontalfield width depend on BasiceditField but its width by default set to unlimited .
hfm_searchBox.add(txtSearch);
vfm_searchBox.add(hfm_searchBox);
how to prevent endless scroll ?
Thanks in Advance !!!
What is the HorizontalFieldManager's virtual width? This refers to the scrollable space whereas the width refers to the extent on the screen. You can try extending HorizontalFieldManager and overriding the sublayout method, calling setVirtualExtent in it.
As Tamar said, the Field's Virtual Width is the key concept to keep track of.
You can see my solution to a very similar question here. I provide a full code example that's probably not too far from what you're trying to do. The code I use goes a step further, and dynamically limits scrolling only to the end of where the text currently reaches. If you don't do this, and simply set one constant virtual width, then you can have the field actually scroll too far to the right. By dynamically calling setVirtualExtent(), you always get just enough scrolling, but not too much.
Related
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);
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.
Developing on the blackberry (OS 7.0) and I have an extended Vertical Field manager created as such:
_myVFM = new MyViewManager(Manager.USE_ALL_WIDTH | Manager.USE_ALL_HEIGHT | Manager.VERTICAL_SCROLL){};
However, when I scroll the view, the virtual scroll view size appears way too big.
i.e, I can scroll quite alot further down than is needed and I cant work out why?
Any body any quick ideas? I do have a background image in there that is created as such:
public void paint(Graphics graphics)
{
Bitmap backgroundBitmap = Bitmap.getBitmapResource("bg.png");
Bitmap newBackground = new Bitmap(Display.getWidth(), Display.getHeight());
backgroundBitmap.scaleInto(newBackground, Bitmap.FILTER_LANCZOS, Bitmap.SCALE_TO_FILL);
graphics.clear();
graphics.drawBitmap(0, 0, Display.getWidth(),
Display.getHeight(), newBackground, 0, 0);
super.paint(graphics);
}
Please and thanks,
Burrows
You can redefine the method to define the height of the manager
protected void setExtent(int width, int height) {
super.setExtent(width, myHeight);
}
To not repeat the background image is necessary to redefine the following method, returning false
protected boolean isScrollCopyable() {
return false;
}
Another comment is that it is a bad practice to obtain an image from the paint method. What you are doing is every time you call the paint method is going to get this image.
It's best to get the image once and then use it.
public MyScreen() {
super(NO_VERTICAL_SCROLL);
I managed to fix the issue in my extension of MainScreen using 'NO_VERTICAL_SCROLL' style parameter
Combined with Rupak's suggestion of setting the Background in the constructor of my Vertical Field Manager rather than overriding paint (
https://gist.github.com/3248319)
everything seems Good now - thanks all for your help.
Burrows
No need to USE_ALL_HEIGHT in your constructor.
// instead use this
_myVFM = new MyViewManager(Manager.USE_ALL_WIDTH | Manager.VERTICAL_SCROLL){};
Trying to add a background image to a textfield but when a user types over and goes into a horizontal scroll the image seems to be replicated over causing it to look like the following:
Basically i want the 1st textfield to look like the 2nd after the user has inputed a large value. I have a class that extends horizontalfieldManager and in my paint function is the following:
protected void paint(Graphics g)
{
g.clear();
if (mLabelMode)
{
g.setColor(mLabelColor);
}
else
{
g.setColor(mColor);
}
int x = this.getHorizontalScroll();
g.drawImage(x, 0, mBackground.getWidth(), mBackground.getHeight(), mBackground, 0, 0, 0);
super.paint(g);
}
Any help would be greatly appreciated.
have a look at this:
Using image for a TextEditField in Blackberry
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.