Blackberry VerticalfieldManager virtual scroll view size too large - blackberry

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

Related

How can I add shadow to the text in a Blackberry LabelField?

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!

how to disable endless scroll in BasicEditField

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.

Borders Around EditFields - Blackberry

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

Image over a background Image in Blackberry

I want to set the position of one image over a background image. The position could be anywhere on the screen.
Can I have a sample code or a link or tutorial for that?
Here's how I do it:
This works in 4.6.0 and later because of BackgroundFactory
// Create the background image and the image field to put on top
Background bg = BackgroundFactory.createBitmapBackground(Bitmap.getBitmapResource(bgImgPath);
Bitmap bmp = Bitmap.getBitmapResource(imgPath);
BitmapField imgField = new BitmapField(bmp);
// Create the field manager
VerticalFieldManager manager = new VerticalFieldManager()
{
// Overide the sublayout of the field manager to set the position of
// the image directly
protected void sublayout(int width, int height)
{
setPositionChild(imgField, positionX, positionY)
setExtent(width, height)
}
};
// Set the background of the field manager
manager.setBackground(bg);
// add the bitmap field to the field manager
manager.add(imgField);
// add the field manager to the screen
add(manager);
For multiple images you can make a layout manager class and use that position all your images where you want them using similar techniques. There's a tutorial for making and using a layout manager, I'll try and dig it up and post it back here.
If your using 4.5.0 or earlier, I use a layout manager and just add the background image like any other image but add it first so it draws on the bottom.
Like I said I'll try and find that tutorial for the Layout Manager.
You can create a class that extends Manager class
Here you can specify the background image as well as you can position the other image at a position you want
class Test extends MainScreen
{
Test()
{
super();
Bitmap bmp = Bitmap.getBitmapResource("image1.png");
BitmapField bmpf = new BitmapField(bmp);
Mymanager obj = new Mymanager();
obj.add(bmpf);
}
}
class Mymanager extends Manager
{
final Bitmap background = Bitmap.getBitmapResource("back.png");
protected void paint(Graphics g)
{
g.drawrect(0,0,background.getWidth,background.getheight,background,0,0);
}
protected void sublayout(int width, int height)
{
Field field = getfield(0);
layoutchild(field,100,100);
setPositionchild(field,20,10);
setExtent(Display.getWidth,Display.getHeight);
}
}

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