Question

I have defined a BitmapButtonField in Blackberry. But the default grey color background does not go. And the Bluish onFocus border? How to change them in BB 4.5

enter image description here

Was it helpful?

Solution

Hi this is bitmap button and background color Red

manager=new Field(Field.FOCUSABLE){
                protected void onFocus(int direction) {
                    super.onFocus(direction);
                    flag1=true;
                    invalidate();
                }
                protected void onUnfocus() {
                    flag1=false;
                    invalidate();

                }
                protected void layout(int width, int height) {
                    setExtent(image.getWidth(),image.getHeight());
                }
                protected void paint(Graphics graphics) {
                    if(flag1){
                        graphics.setBackgroundColor(Color.RED);
                        graphics.clear();
                        graphics.drawBitmap(0, 0, image.getWidth(),image.getHeight(), image_hover,0,0);
                    }else{
                        graphics.drawBitmap(0, 0, image.getWidth(),image.getHeight(), image,0,0);
                    }
                }
                protected boolean navigationClick(int status, int time) {
                    fieldChangeNotify(1);
                    return true;
                }
            };
            manager.setChangeListener(this);
            add(manager);

output as following image enter image description here This is background image and with default background color

manager1=new Field(Field.FOCUSABLE){
                protected void onFocus(int direction) {
                    super.onFocus(direction);
                    flag2=true;
                    invalidate();
                }
                protected void onUnfocus() {
                    button=image;
                    flag2=false;
                    invalidate();

                }
                protected void layout(int width, int height) {
                    setExtent(image.getWidth(),image.getHeight());
                }
                protected void paint(Graphics graphics) {
                    if(flag2)
                        graphics.drawBitmap(0, 0, image.getWidth(),image.getHeight(), image_hover,0,0);
                        else
                        graphics.drawBitmap(0, 0, image.getWidth(),image.getHeight(), image,0,0);
                }
                protected boolean navigationClick(int status, int time) {
                    fieldChangeNotify(1);
                    return true;
                }
            };
            manager1.setChangeListener(this);
            add(manager1);

out put as followingenter image description here

Fully functional class is following for better understanding

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.container.MainScreen;

public class StartUp extends UiApplication{
    public static void main(String[] args) {
        StartUp up=new StartUp();
        up.enterEventDispatcher();
    }
    public StartUp()
    {
        pushScreen(new Demoscreen());
    }
    class Demoscreen extends MainScreen implements FieldChangeListener
    {
        Bitmap image,image_hover;
        boolean flag1,flag2;
        Field manager=null,manager1=null;
        public Demoscreen() {
            image=Bitmap.getBitmapResource("Download_48x48.png");
            image_hover=Bitmap.getBitmapResource("Download_48x48.png");
            manager=new Field(Field.FOCUSABLE){
                protected void onFocus(int direction) {
                    super.onFocus(direction);
                    flag1=true;
                    invalidate();
                }
                protected void onUnfocus() {
                    flag1=false;
                    invalidate();

                }
                protected void layout(int width, int height) {
                    setExtent(image.getWidth(),image.getHeight());
                }
                protected void paint(Graphics graphics) {
                    if(flag1){
                        graphics.setBackgroundColor(Color.RED);
                        graphics.clear();
                        graphics.drawBitmap(0, 0, image.getWidth(),image.getHeight(), image_hover,0,0);
                    }else{
                        graphics.drawBitmap(0, 0, image.getWidth(),image.getHeight(), image,0,0);
                    }
                }
                protected boolean navigationClick(int status, int time) {
                    fieldChangeNotify(1);
                    return true;
                }
            };
            manager.setChangeListener(this);
            add(manager);
            manager1=new Field(Field.FOCUSABLE){
                protected void onFocus(int direction) {
                    super.onFocus(direction);
                    flag2=true;
                    invalidate();
                }
                protected void onUnfocus() {
                    flag2=false;
                    invalidate();
                }
                protected void layout(int width, int height) {
                    setExtent(image.getWidth(),image.getHeight());
                }
                protected void paint(Graphics graphics) {
                    if(flag2)
                        graphics.drawBitmap(0, 0, image.getWidth(),image.getHeight(), image_hover,0,0);
                        else
                        graphics.drawBitmap(0, 0, image.getWidth(),image.getHeight(), image,0,0);
                }
                protected boolean navigationClick(int status, int time) {
                    fieldChangeNotify(1);
                    return true;
                }
            };
            manager1.setChangeListener(this);
            add(manager1);
        }
        public void fieldChanged(Field field, int context) {
            if(field==manager)
            {
                displayDialog("I am first field");
            }else if(field==manager1)
            {
                displayDialog("I am second  field");
            }

        }
        public void displayDialog(final String message)
        {
            UiApplication.getUiApplication().invokeLater(new Runnable() {
                public void run() {
                    Dialog.inform(message);
                }
            });
        }
    }
}

For more details please visit following link for BackgroundsBackground logic for 4.5 OS

OTHER TIPS

Check following links for customized Button Field:

Implement advanced buttons, fields, and managers.

User Defined Buttons - Create a Custom Button From an Image

Blackberry Custom Button Field

How to create a Custom Button Field in Blackberry

Normally overriding applyTheme(..) (an empty implementation) removes default styling of a field. Also we need to override paint(..), paintBackground(..), drawFocus(..) and change their implementation according to current focus status of the field. Below is a code snippet for the case when I extend a Field for making Custom Button Field.

private final Bitmap focusedBitMap = Bitmap.getBitmapResource("focused.png");
private final Bitmap unfocusedBitmap = Bitmap.getBitmapResource("unfocused.png");

protected void paint(Graphics graphics) {
    // paint background
    graphics.setBackgroundColor(isFocus() ? Color.RED : Color.GREEN);
    graphics.clear();

    // draw button image
    Bitmap myBitmap = isFocus() ? focusedBitMap : unfocusedBitmap;
    if (myBitmap != null) {
        // image position calculation
        int imageX = 0;
        int imageY = 0;
        graphics.drawBitmap(imageX, imageY, 
            myBitmap.getWidth(), myBitmap.getHeight(), 
            myBitmap, 0, 0);
    }
}

protected void drawFocus(Graphics graphics, boolean on) {
}

public boolean isFocusable() {
    return true;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top