Unstable of Custom Edit Field - blackberry

I built custom edit field because i want to change the background color.
first, second and third shown unstable, sometime display when focusing, disappear when lost focusing.
I want the result like third image where the focusing at where also static display all the field.
Here is my Custom_EditField:
public class Custom_EditField extends EditField {
private int width, row, color;
private boolean isfocus;
Custom_EditField(long style, int width, int row) {
super(style);
this.width = width;
this.row = row;
}
public int getPreferredHeight() {
return Font.getDefault().getHeight() * row;
}
public int getPreferredWidth() {
return width;
}
protected void onFocus(int direction) {
color = Color.GRAY;
isfocus = true;
}
protected void onUnfocus() {
color = Color.GOLD;
isfocus = false;
}
protected void layout(int maxWidth, int maxHeight) {
super.layout(maxWidth,
Math.min(maxHeight, Font.getDefault().getHeight() * row));
super.setExtent(maxWidth,
Math.min(maxHeight, Font.getDefault().getHeight() * row));
}
protected void paint(Graphics graphics) {
int rectHeight = getPreferredHeight();
int rectWidth = getPreferredWidth();
try {
if (isfocus) {
graphics.setBackgroundColor(color);
} else {
graphics.setBackgroundColor(color);
}
color = Color.BLACK;
graphics.setColor(color);
graphics.drawRect(0, 0, rectWidth, rectHeight);
super.paint(graphics);
} finally {
graphics.setColor(color);
}
}
}

I'm not 100% sure which problem you're asking about:
how to draw your own focus background colors for an EditField, or
how to fix the problem with fields disappearing as you move focus
1) If it's the second problem (disappearing), then I would guess that you're having the same problem as in your other question, with the Custom_TopField buttons disappearing
So, if these Custom_EditField objects are created by a manager that extends VerticalFieldManager, but also implements sublayout() itself to perform all the positioning, then try the solution I suggested in the other question (don't extend VerticalFieldManager, just extend Manager).
2) If that doesn't work, it's possible that your paint() method is not getting triggered when it should. Try adding a call to invalidate() in your focus methods, along with a call to the superclass methods onFocus() and onUnfocus():
protected void onFocus(int direction) {
color = Color.GRAY;
isfocus = true;
invalidate();
super.onFocus(direction);
}
protected void onUnfocus() {
color = Color.GOLD;
isfocus = false;
invalidate();
super.onUnfocus();
}
3) And, you might need to implement this, too:
public boolean isFocusable() {
return true;
}
But, first, check your manager class, to be sure this isn't the same problem as in your other question.
Also ...
In this class, you are basing the height of the edit field on the row number. Is this really what you want? Row 0 would be 0 size, and each row after that would get taller and taller. Is that really the UI? Normally, you would have all the rows' edit fields be the same size, and simply lay them out with a different y offset. Most likely, this class should not need to know which row it has been placed on. That information is normally the responsibilty of a manager object.

protected EditField getEditField() {
EditField edit_field = new EditField("", "", 10, EditField.NO_NEWLINE
| BasicEditField.FIELD_VCENTER) {
protected boolean navigationClick(int status, int time) {
}
protected void onFocus(int direction) {
Set the boolean during focus
}
protected void onUnfocus() {
}
protected void paintBackground(Graphics graphics_paint) {
if(that boolean is true)
{
Set your custom background during focus.
}else{
Set your custom background during unfocus.
}
}
protected void paint(Graphics graphics_paint) {
int old_color = graphics_paint.getColor();
try {
if(that boolean is true)
{
Set your custom Font color during any event like focus and click.
}else{
Set your custom background Font color during any event like unfocus.
}
super.paint(graphics_paint);
} finally {
graphics_paint.setColor(old_color);
}
}
public void layout(int width, int height) {
}
};

Related

Blackberry scrolling doesn't redraw fields

I have a very strange problem at the moment.
Basically when I scroll in a screen, the fields don't get redrawn completely or consistently.
I have a Screen (NO_VERTICAL_SCROLL), with a manager as a titlebar. Below that I have a vertical field manager (VERTICAL_SCROLL) with labelfields. When I scroll the vfm one or two lines of the labelfields, which were already visible, get redrawn. The section I'm scrolling down to has absolutely nothing drawn.
I tried invalidate(), and calling doPaint in a scrollchangelistener, but its actually much worse. It results in the titlebar only being partially redrawn.
In the code below I used a custom FontManager, DimenManager, and ImageResourceManager to return values dependent on screen size. I used a custom BitmapButtonField and ClickableLabel in order to change the state of a field when a click is being held in.
public class BaseScreen extends MainScreen implements StringsResource
{
protected ResourceBundle resources;
public BaseScreen(long style)
{
super(style);
StandardTitleBar titlebar = new StandardTitleBar();
titlebar.addSignalIndicator();
titlebar.addClock();
titlebar.addNotifications();
setTitle(titlebar);
resources = ResourceBundle.getBundle(BUNDLE_ID, BUNDLE_NAME);
}
}
public class TandCScreen extends BaseScreen
{
final String copy_text1 = "long text here";
final String copy_text2 = "even longer text here";
ColoredLabelField label_title;
ColoredLabelField label_subtitle;
ColoredLabelField label1;
ColoredLabelField label2;
ClickableLabel label3;
public TandCScreen()
{
super(NO_VERTICAL_SCROLL | NO_VERTICAL_SCROLLBAR);
label_title = new ColoredLabelField(resources.getString(STRING_APP_NAME), Color.WHITE, DrawStyle.HCENTER | USE_ALL_WIDTH);
label_title.setBackground(BackgroundFactory.createSolidBackground(0x60223b));
label_subtitle = new ColoredLabelField(resources.getString(STRING_TANDC_TITLE), 0x58585b, DrawStyle.HCENTER | USE_ALL_WIDTH);
label1 = new ColoredLabelField(copy_text1, 0x58585b, USE_ALL_WIDTH);
label2 = new ColoredLabelField("", 0xa7a9ab, USE_ALL_WIDTH);
label3 = new ClickableLabel("Read more...")
{
protected void unclick()
{
super.unclick();
UiApplication.getUiApplication().invokeLater(new Runnable()
{
public void run()
{
ColoredLabelField label = new ColoredLabelField(copy_text2, 0xa7a9ab, 0);
label.setFont(FontManager.body());
label.setMargin(0, 0, DimenManager.interField(), 0);
label2.getManager().replace(label2, label);
label3.getManager().delete(label3);
}
});
}
};
label_title.setFont(FontManager.subtitle());
label_subtitle.setFont(FontManager.subtitle());
label1.setFont(FontManager.body());
label2.setFont(FontManager.body());
label3.setFont(FontManager.body());
BitmapButtonField button_accept = new BitmapButtonField(ImageResourceManager.buttonAccept(), ImageResourceManager.buttonAcceptHover(), FIELD_HCENTER)
{
protected void click()
{
super.click();
setImage(ImageResourceManager.buttonAcceptSelected());
setFocusImage(ImageResourceManager.buttonAcceptSelected());
}
protected void unclick()
{
super.unclick();
PersistentStoreManager.setTandCAccepted(true);
UiApplication.getUiApplication().pushScreen(new LoginScreen());
close();
}
};
BitmapButtonField button_decline = new BitmapButtonField(ImageResourceManager.buttonDecline(), ImageResourceManager.buttonDeclineHover(), FIELD_HCENTER)
{
protected void click()
{
super.click();
setImage(ImageResourceManager.buttonDeclineSelected());
setFocusImage(ImageResourceManager.buttonDeclineSelected());
}
protected void unclick()
{
super.unclick();
close();
}
};
int margin = (VariableManager.DISPLAY_WIDTH - button_accept.getPreferredWidth()) / 2;
// calculate where to put ellipsis
Font font = label2.getFont();
int max_length = (VariableManager.DISPLAY_WIDTH - margin * 2) * 2;
int i = copy_text2.length() - 1;
while (font.getAdvance(copy_text2.substring(0, i)) + font.getAdvance("...") >= max_length)
i--;
label2.setText(copy_text2.substring(0, i).trim() + "...");
VerticalFieldManager vfm = new VerticalFieldManager(VERTICAL_SCROLL | VERTICAL_SCROLLBAR);
vfm.add(new NullField());
vfm.add(label_subtitle);
vfm.add(new Seperator());
vfm.add(label1);
vfm.add(label2);
vfm.add(label3);
vfm.add(button_accept);
vfm.add(button_decline);
vfm.setMargin(0, margin, 0, margin);
// paddings
int padding = (DimenManager.header() - label_title.getPreferredHeight()) / 2;
label_title.setPadding(padding, 0, padding, 0);
label_subtitle.setPadding(DimenManager.interField(), 0, DimenManager.interField(), 0);
label1.setMargin(DimenManager.interField(), 0, DimenManager.interField(), 0);
label3.setMargin(DimenManager.interField(), 0, DimenManager.interField(), button_accept.getPreferredWidth() - label3.getPreferredWidth());
button_decline.setMargin(DimenManager.interField(), 0, DimenManager.interButton(), 0);
add(label_title);
add(vfm);
}
protected boolean onSavePrompt()
{
return false;
}
protected void makeMenu(Menu menu, int instance)
{
if (instance == Menu.INSTANCE_CONTEXT)
{
ContextMenu contextMenu = ContextMenu.getInstance();
contextMenu.setTarget(this);
contextMenu.clear();
this.makeContextMenu(contextMenu);
menu.deleteAll();
menu.add(contextMenu);
}
else
{
super.makeMenu(menu, instance);
}
}
protected void makeContextMenu(ContextMenu contextMenu)
{
}
/**
* Clickable labelfield which changes color on down press, and fires action
* on release. Action is canceled if touch moves outside field bounds.
*
* #author kevin
*
*/
private class ClickableLabel extends LabelField
{
private boolean canceled = true;
private boolean consumed = false;
protected boolean pressed = false;
public ClickableLabel(String label)
{
super(label, LabelField.FOCUSABLE | USE_ALL_WIDTH);
setFont(FontManager.body());
}
protected void paint(Graphics g)
{
// background
if (pressed)
{
g.setColor(0x2C1721);
}
else if (isFocus())
{
g.setColor(0x993C6B);
}
else
{
g.setColor(0x60223B);
}
int padding_y = (getPreferredHeight() - getFont().getHeight()) / 2;
int padding_x = getPaddingLeft();
g.drawText(getText(), padding_x, padding_y);
}
public int getPreferredHeight()
{
return ImageResourceManager.highlight().getHeight();
}
protected void layout(int width, int height)
{
height = getPreferredHeight();
super.layout(width, height);
setExtent(width, height);
}
// --------- Highlight selected row ---------
protected void onFocus(int direction)
{
super.onFocus(direction);
invalidate();
}
protected void onUnfocus()
{
super.onUnfocus();
invalidate();
}
// --------------------------------------------
protected void drawFocus(Graphics graphics, boolean on)
{
}
/**
* Called when trackpad pressed, or touchscreen touched
*/
protected void click()
{
pressed = true;
invalidate();
}
/**
* Called when trackpad released, or touchscreen released
*/
protected void unclick()
{
cancel();
}
protected void cancel()
{
pressed = false;
invalidate();
}
protected boolean navigationClick(int status, int time)
{
if (status != 0)
{
if (consumed)
{
consumed = false;
}
else
{
click();
}
}
return true;
}
protected boolean navigationUnclick(int status, int time)
{
if (status != 0)
{
if (consumed)
consumed = false;
else
unclick();
}
return true;
}
protected boolean touchEvent(TouchEvent message)
{
int x = message.getX(1);
int y = message.getY(1);
if (x < 0 || y < 0 || x > getExtent().width || y > getExtent().height)
{
// Outside the field
if (!canceled)
{
cancel();
}
canceled = true;
return false;
}
if (message.getEvent() == TouchEvent.UP)
{
if (canceled)
cancel();
else
unclick();
consumed = true;
return true;
}
if (message.getEvent() == TouchEvent.DOWN)
{
click();
consumed = true;
canceled = false;
return true;
}
return super.touchEvent(message);
}
}
private class Seperator extends SeparatorField
{
protected void paint(Graphics graphics)
{
graphics.setColor(0xa7a9ab);
super.paint(graphics);
}
}
}
Thanks in advance for any suggestions
I don't think it is possible to do anything but guess at your problem without looking at your code. But whatever your problem is, I am confident that it is based on a misunderstanding of how to use Managers within a Screen. So I recommend that you review the following articles to improve your knowledge in this area and so hopefully resolve the problem yourself:
Start here:
UI Introduction
This provides the Background around Managers and Fields.
Then read this article:
MainScreen Explained
I suspect as a result of reading this article, you may be able to discard your 'title bar' and use setTitle() or setBanner() to provide this function.
I hope this resolves your problems.
A few other points:
In all my years of BB programming, I have never had to use doPaint() to get something painting the way I wanted. I can't think of a situation that this will in fact help. So if you think you need it, try invalidate() instead.
I have used invalidate() when I making a change to the Field that will change its on screen appearance (but not its size). I have used it in a scroll change listener. But it is a method of last resort.
Remember that LabelFields are not focusable, so in OS's before 6, that made them a problem to scroll.
Found the issue. On the labelfields, there was a setExtent(getPreferredWidth(), getPreferredHeight()); that was reducing the size of the area to redraw. Very stupid mistake.
Thanks to everyone who tried to help.

how to do instant update in BB RIM?

I want to manually display a field.
public class Main_NewsDetail extends MainScreen {
private Custom_FontField slider;
private boolean a;
public Main_NewsDetail() {
super(USE_ALL_WIDTH);
slider = new Custom_FontField(
Bitmap.getBitmapResource("slider_thumb_normal.png"),
Bitmap.getBitmapResource("slider_progress_normal.png"),
Bitmap.getBitmapResource("slider_base_normal.png"),
Bitmap.getBitmapResource("slider_thumb_focused.png"),
Bitmap.getBitmapResource("slider_progress_focused.png"),
Bitmap.getBitmapResource("slider_base_focused.png"),
Bitmap.getBitmapResource("slider_thumb_pressed.png"),
Bitmap.getBitmapResource("slider_progress_pressed.png"),
Bitmap.getBitmapResource("slider_base_pressed.png"), 35, 10, 5,
5, FOCUSABLE);
if(a)
add(slider);
}
public class Custom_NewsDetailBottom extends Manager implements
FieldChangeListener {
Custom_NewsDetailBottom() {
super(Manager.USE_ALL_WIDTH | Manager.NO_VERTICAL_SCROLL
| Manager.NO_HORIZONTAL_SCROLL);
Background background = BackgroundFactory
.createBitmapBackground(bg);
setBackground(background);
fontbtn = new Custom_ButtonField(font, fontactive, fontactive) {
protected boolean navigationClick(int status, int time) {
a = !a; <-- here is to control field display
return true;
}
};
fontbtn.setChangeListener(this);
add(fontbtn);
}
protected void sublayout(int width, int height) {
Field field = getField(0);
layoutChild(field, font.getWidth(), font.getHeight());
setPositionChild(field, getGap(), 5);
width = Math.min(width, getPreferredWidth());
height = Math.min(height, getPreferredHeight());
setExtent(width, height);
}
public int getPreferredHeight() {
return 70;
}
public int getPreferredWidth() {
return Display.getWidth();
}
protected void paint(Graphics graphics) {
int rectHeight = getPreferredHeight();
int rectWidth = getPreferredWidth();
graphics.drawRect(0, 0, rectWidth, rectHeight);
super.paint(graphics);
}
private int getGap() {
return ((getPreferredWidth() / 4) - font.getWidth()) / 2;
}
public void fieldChanged(Field field, int context) {
if (field == sharebtn) {
} else if (field == commentbtn) {
Main.getUiApplication().pushScreen(new Main_Comments());
} else if (field == otherbtn) {
Main.getUiApplication().pushScreen(new Menu_Others());
}
}
public boolean keyDown(int keycode, int status) {
if (Keypad.key(keycode) == Keypad.KEY_ESCAPE) {
delete(slider);
return true;
}
return super.keyDown(keycode, status);
}
}
}
Above is the code that display a screen. In fontbtn, When click will change the variable true / false. However, it cannot update instant to display the field slider.
slider is something like seekbar in Android. In android, when click then can setvisibility but not blackberry RIM, so how to control it?
First of all, I would recommend not using variables with names like this if you can avoid it:
private boolean a;
Try to give it a name that's more descriptive, as that will help us understand your code better.
Next, it looks like you are testing the variable a before it ever has a chance to change, in the Main_NewsDetail constructor. So that won't work. Maybe try this in the Main_NewsDetail class:
/** separate boolean used because Field.isVisible() doesn't seem totally robust */
private boolean isSliderVisible = false;
private void setSliderVisible(boolean isVisible) {
if (isVisible != isSliderVisible) {
if (isVisible) {
add(slider);
} else {
delete(slider);
// but, we still retain the "slider" member variable, so it can be
// added again later
}
isSliderVisible = isVisible;
// I'm not actually sure that this is needed. I include it because I can't run this code right now!
invalidate();
}
}
Then, in your button click handler:
fontbtn = new Custom_ButtonField(font, fontactive, fontactive) {
protected boolean navigationClick(int status, int time) {
setSliderVisible(!isSliderVisible);
return true;
}
};
Now, this code can work because your Main_NewsDetail has only one Field in it, the slider. If there are actually multiple Field objects (which you probably will have), then you may need more complicated logic. You may want to show the slider in the same location every time. For that, you can record the index of the slider, in the list of all the Main_NewsDetails fields (for example, is the slider the 1st field, the 2nd, the 5th?). Then, instead of calling add(slider), you would do:
private void setSliderVisible(boolean isVisible) {
if (isVisible != isSliderVisible) {
if (isVisible) {
insert(slider, sliderIndex);
You might need to make Main_NewsDetail a subclass of Manager and implement the sublayout() method. That way, you can make sure that if isSliderVisible, you always lay out the slider in the same position.
Try calling Manager's invalidate-method to force a repaint for the managed area:
Marks this entire manager as requiring repainting.
Invoke this method to signal that this manager's entire region requires repainting.

Custom BitmapField bug on unfocus and scroll (BlackBerry)

I have been having this annoying problem when trying to implement a picture gallery on BlackBerry 6.
Everything works, however when the focus changes from the top buttons to say the pictures further down the screen, the images seem to glitch and not paint themselves correctly. Please see the images below for an example:
(Focus is on the top of the screen(not shown))
(Focus is now on the bottom left image, note that the top image is now blank for an unknown reason)
And this happens no matter how many pictures I add to the tumbnail gallery.
Now here is my code, (a part of it concerning the drawing of the thumbnails)
public ProductImage(String productName){
super(VERTICAL_SCROLL|VERTICAL_SCROLLBAR);
currentProduct = productName;
createGUI();
}
public void createGUI(){
deleteAll();
try{
Storage.loadPicture();
}catch(NullPointerException e){
e.printStackTrace();
}
this.setTitle(new LabelField(_resources.getString(PRODUCT_IMAGE), Field.FIELD_HCENTER));
if(ToolbarManager.isToolbarSupported())
{
Toolbar tb = new Toolbar();
setToolbar(tb.createToolBar());
}
else{
Toolbar tb = new Toolbar();
add(tb.createNavBar());
}
picVector = Storage.getPicture(currentProduct);
EncodedImage enc = EncodedImage.getEncodedImageResource("camera.png");
EncodedImage sizeEnc = ImageResizer.sizeImage(enc, Display.getHeight(), Display.getHeight());
takenPicture = new BitmapField(enc.getBitmap());
vfMain = new VerticalFieldManager();
vfMain.add(logo);
vfMain.add(new SeparatorField());
add(vfMain);
prepareBmpFields();
}
private void prepareBmpFields() {
System.out.println("This is the vector size: " + picVector.getPicVector().size());
LayoutManager manager = new LayoutManager();
FieldChangeListener itemListener = new ButtonListener();
mBmpFields = new ImageButtonField[picVector.getPicVector().size()];
for (int i = 0; i < picVector.getPicVector().size(); i++) {
/*EncodedImage image = EncodedImage
.getEncodedImageResource((String)imageVector.elementAt(i));*/
byte[] data = getData((String)picVector.getPicVector().elementAt(i));
//Encode and Resize image
EncodedImage eImage = EncodedImage.createEncodedImage(data,0,data.length);
eImage = ImageResizer.resizeImage(eImage, mImgWidth, mImgHeight);
ImageButtonField currentImage = new ImageButtonField(eImage.getBitmap());
currentImage.setAssociatedPath((String)picVector.getPicVector().elementAt(i));
mBmpFields[i] = currentImage;
mBmpFields[i].setChangeListener(itemListener);
manager.add(mBmpFields[i]);
}
vfMain.add(manager);
}
private class LayoutManager extends VerticalFieldManager {
public LayoutManager() {
super(VERTICAL_SCROLL | VERTICAL_SCROLLBAR);
}
protected void sublayout(int width, int height) {
int columns = mScrWidth / (mImgWidth + 2 * mImgMargin);
int scrWidth = Display.getWidth();
int rows = mBmpFields.length / columns
+ (mBmpFields.length % columns > 0 ? 1 : 0);
int counter = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
int posX = j * (mImgWidth + 2 * mImgMargin) + mImgMargin;
int posY = i * (mImgHeight + 2 * mImgMargin) + mImgMargin;
if(mBmpFields.length > counter){
Field field = mBmpFields[counter];
layoutChild(field, mImgWidth, mImgHeight);
setPositionChild(field, posX, posY);
counter++;
};
}
}
if(Display.getWidth() < Display.getHeight()){
setExtent(mScrWidth, (int)(mScrHeight*1.25));
}
else{
setExtent(mScrWidth, (int)(mScrHeight*2));
}
}
public int getPreferredWidth() {
return mScrWidth;
}
public int getPreferredHeight() {
return mScrHeight;
}
}
}
I have removed many non relevant parts of the code, but the needed code is there.
Does anyone know what could be causing this problem? Thanks for your help!
Edit: as requested, here is my implementation of ImageButtonField class:
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Characters;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.component.BitmapField;
public class ImageButtonField extends BitmapField{
String associatedPath ="";
BitmapField image2;
public ImageButtonField(Bitmap image) {
super(image);
}
public void setAssociatedPath(String path){
associatedPath = path;
}
public String getAssociatedPath(){
return associatedPath;
}
public boolean isFocusable() {
return true;
}
protected void applyTheme(Graphics arg0, boolean arg1) {
}
protected void drawFocus(Graphics graphics, boolean on) {
}
protected void onFocus(int direction) {
// only change appearance if this button is enabled (aka editable)
if (isEditable()) {
invalidate(); // repaint
}
super.onFocus(direction);
}
public void onUnfocus() {
invalidate(); // repaint
super.onUnfocus();
}
protected boolean navigationClick(int status, int time) {
fieldChangeNotify(0);
return true;
}
protected boolean trackwheelClick(int status, int time) {
fieldChangeNotify(0);
return true;
}
protected void paint(Graphics graphics) {
super.paint(graphics);
if (isFocus()) {
graphics.setGlobalAlpha(128);
graphics.setColor(0x888888);
graphics.fillRect(0, 0, getWidth(), getHeight());
}else{
graphics.setGlobalAlpha(0);
graphics.setColor(0x000000);
graphics.fillRect(0, 0, getWidth(), getHeight());
//graphics.drawBitmap(0, 0, getWidth(), getHeight(), image2.getB, 0, 0);
}
}
protected boolean keyChar(char character, int status, int time) {
if(Characters.ENTER == character || Characters.SPACE == character) {
fieldChangeNotify(0);
return true;
}
return super.keyChar(character, status, time);
}
}
Ok, so you can disregard my first answer, but since I didn't have your ImageButtonField code at the time, I don't want to throw it out ... maybe someone else will find it useful.
In the end, I didn't need to make any changes to ImageButtonField, but I did change your LayoutManager class. The way I figured out that it was the problem was I just started replacing your custom UI classes with built-in ones. I replaced ImageButtonField with BitmapField. That didn't fix it. Then, I replaced LayoutManager with FlowFieldManager and that fixed it. So, I knew where the problem was.
My solution:
private class LayoutManager extends Manager {
public LayoutManager() {
super(VERTICAL_SCROLL | VERTICAL_SCROLLBAR);
}
protected void sublayout(int width, int height) {
setExtent(width, height);
// TODO: maybe always set the same virtual extent?
if (Display.getWidth() < Display.getHeight()) {
setVirtualExtent(mScrWidth, (int) (mScrHeight * 1.25));
} else {
setVirtualExtent(mScrWidth, (int) (mScrHeight * 2));
}
int columns = mScrWidth / (mImgWidth + 2 * mImgMargin);
// int scrWidth = Display.getWidth();
int rows = mBmpFields.length / columns + (mBmpFields.length % columns > 0 ? 1 : 0);
int counter = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
int posX = j * (mImgWidth + 2 * mImgMargin) + mImgMargin;
int posY = i * (mImgHeight + 2 * mImgMargin) + mImgMargin;
if (mBmpFields.length > counter) {
Field field = mBmpFields[counter];
layoutChild(field, mImgWidth, mImgHeight);
setPositionChild(field, posX, posY);
counter++;
}
}
}
}
public int getPreferredWidth() {
return mScrWidth;
}
public int getPreferredHeight() {
return mScrHeight;
}
}
I can't say for sure that I understand why your original code wasn't working, but I can say that I wouldn't have done a few of the things in the original code:
The original code was extending VerticalFieldManager but was doing all the work itself, in sublayout(). So, I don't think there was any point extending VerticalFieldManager. I changed it to just extend Manager.
The original code was calling setExtent() with different sizes. I don't think that's what you wanted. Extent is the actual size of the Field. Virtual extent is the virtual size, which is what you want to set larger than the actual extent, in order to enable scrolling. You don't need to dynamically calculate different extents for portrait vs. landscape because the width and height parameters passed to sublayout() will already reflect that. I'm not sure you really even need to be setting different virtual extents either. I think you should probably always set the virtual extent height to the number of rows times picture height, accounting for margins.
You had an unused variable scrWidth in your original code. I commented it out above.
You also posted this question recently, right? Am I correct in assuming that the ImageButtonField you refer to here is the same one you were working on in the other question?
I can't see your full implementation of ImageButtonField, which you should probably post here, too. However, looking at the answers to your other question, I have a feeling that you're doing some custom focus handling in ImageButtonField, and maybe it's not being done quite right. In any case, that class may be where the problem is.
I have a similar Field subclass of my own, and here are the focus handling methods I define:
public class CustomButtonField extends Field {
private Bitmap _button; // the currently displayed button image
private Bitmap _on; // image for 'on' state (aka in-focus)
private Bitmap _off; // image for 'off' state (aka out-of-focus)
protected void onFocus(int direction) {
// only change appearance if this button is enabled (aka editable)
if (isEditable()) {
_button = _on;
invalidate(); // repaint
}
super.onFocus(direction);
}
protected void onUnfocus() {
_button = _off;
invalidate(); // repaint
super.onUnfocus();
}
protected void drawFocus(Graphics graphics, boolean on) {
// override superclass implementation and do nothing
}
public boolean isFocusable() {
return true;
}
I also have a custom implementation of paint(). I won't show it all here, because a lot of the code probably has nothing to do with your problem, but my paint() does include this call:
graphics.drawBitmap(_padding, _padding, _fieldWidth, _fieldHeight, _button, 0, 0);
You might not care about the fact that I have separate images for focused, and unfocused states ... maybe you show the same image at all times.
But, probably the thing to check is your onFocus() and onUnfocus() methods. You may need to add a call to invalidate() as I have.
Looking at Rupak's answer to your other question, it would also be good to check your ImageButtonField.paint() method, and make sure you aren't neglecting to do important drawing steps if the field is not in focus.

How to set focus to FieldManager in BlackBerry?

How to draw focus to a VerticalFieldManager in BlackBerry.
I have tried this but not working.
VerticalFieldManager vv=new VerticalFieldManager(Manager.focusFOCUSABLE);
You can try calling Field.setFocus on it, but since a manager is a container, I'm not sure you are going to see "the focus" over it.
If it doesn't work, you can try also overriding the paint method and draw your own custom focus when isFocus returns true.
this is the way to do it:
VerticalFieldManager vv=new VerticalFieldManager(FOCUSABLE) {
protected void paintBackground(Graphics g) {
int prevColor = g.getColor();
int bgColor;
if (isFocus()) {
bgColor = Color.Blue;
} else {
bgColor = Color.White;
}
g.setColor(bgColor);
g.fillRoundRect(0, 0, getPreferredWidth(), getPreferredHeight(), 0, 0);
g.setColor(prevColor);
}
public void onFocus(int direction) {
super.onFocus(direction);
this.invalidate();
}
public void onUnfocus() {
super.onUnfocus();
this.invalidate();
}
};
_focusAnchor = new NullField(FOCUSABLE);
add(_focusAnchor);
try this -
VerticalFieldManager vv=new VerticalFieldManager(FOCUSABLE);
then add items to the vv.

Blackberry, created a custom button and would like image to change image after it is draWN

I have created a custom button field for my BlackBerry project. I would like to change the picture displayed on the button after it is drawn, but cannot figure out how. I have code that changes the member variable that stores the bitmap, but do not know how to tell the blackberry to update it.
// CODE TO CHANGE BUTTONS Image
Bitmap image2 = Bitmap.getBitmapResource("aftera.png");
MyBut.image=image2;
// don’t know how to redraw buttn?????
// BUTTON CODE
public class cPictureButton extends Field{
public Bitmap image;
public cPictureButton( Bitmap image, long style)
{
super(style);
this.image=image;
}
public int getPreferredHeight()
{
return image.getHeight();
// return getFont().getHeight();
}
public int getPreferredWidth()
{
return image.getWidth();
// return getFont().getAdvance(label)+8;
}
protected void drawFocus(Graphics g, boolean on)
{
}
protected void paint(Graphics g)
{
int w=image.getWidth();
int h=image.getHeight();
g.drawBitmap(0, 0, w, h, image, 0, 0);
if (isFocus() )
g.drawRect(0,0,image.getWidth(), image.getHeight());
}
protected void layout(int width, int height) {
// TODO Auto-generated method stub
setExtent(Math.min(width, getPreferredWidth()),
Math.min(height, getPreferredWidth()));
}
public boolean isFocusable() {
return true;
}
protected boolean navigationClick(int status, int time)
{
fieldChangeNotify(0);
return true;
}
}
Calling invalidate() on your button field should take care of it.
Also, you still have the bug in your layout() method that I pointed out in your earlier question: Why does my rectangular bitmap get clipped to a square when I draw it?

Resources