1. sagare's Avatar
    Hi All,

    Here is the code i wrote for creating the customButtonField



    package com.rim.samples.device.helloworlddemo;


    import net.rim.device.api.ui.*;
    import net.rim.device.api.system.*;


    /**
    *Custom button field that uses images as button backgrounds.
    */
    public class PictureBackgroundButtonField extends Field
    {
    private String _label;
    private int _labelHeight;
    private int _labelWidth;
    private Font _font;

    private Bitmap _currentPicture;
    private Bitmap _onPicture;
    private Bitmap _offPicture;

    /**
    *Constructor.
    *@param imgOn, imgOff - images on top of which the label (if any) will be drwan.
    *@param text - the text to be displayed on the button
    *@param style - combination of field style bits to specify display
    attributes
    */
    public PictureBackgroundButtonField(String imgOn, String imgOff, String text, long style)
    {
    super(style);
    _onPicture = Bitmap.getBitmapResource(imgOn);
    _offPicture = Bitmap.getBitmapResource(imgOff);
    _font = Font.getDefault().derive(Font.PLAIN, Font.getDefault().getHeight() - 4); //getFont();
    _label = text;
    _labelHeight = _font.getHeight();
    _labelWidth = _font.getAdvance(_label);
    _currentPicture = _offPicture;
    }

    public PictureBackgroundButtonField(Bitmap imgOn, Bitmap imgOff, String text, long style)
    {
    super(style);
    _onPicture = imgOn;
    _offPicture = imgOff;
    _font = Font.getDefault().derive(Font.PLAIN, Font.getDefault().getHeight() - 4); //getFont();
    _label = text;
    _labelHeight = _font.getHeight();
    _labelWidth = _font.getAdvance(_label);
    _currentPicture = _offPicture;
    }

    /**
    *@return The text on the button
    */
    String getText()
    {
    return _label;
    }

    /**
    *Field implementation.
    *@see net.rim.device.api.ui.Field#getPreferredHeight()
    */
    public int getPreferredHeight()
    {
    //return _labelHeight + 4;
    return _currentPicture.getHeight();
    }

    /**
    *Field implementation.
    *@see net.rim.device.api.ui.Field#getPreferredWidth()
    */
    public int getPreferredWidth()
    {
    //return _labelWidth + 8;
    return _currentPicture.getWidth();
    }

    /**
    *Field implementation. Changes the picture when focus is gained.
    *@see net.rim.device.api.ui.Field#onFocus(int)
    */
    protected void onFocus(int direction)
    {
    _currentPicture = _onPicture;
    invalidate();
    }

    /**
    *Field implementation. Changes picture back when focus is lost.
    *@see net.rim.device.api.ui.Field#onUnfocus()
    */
    protected void onUnfocus()
    {
    _currentPicture = _offPicture;
    invalidate();
    }

    /**
    *Field implementation.
    *@see net.rim.device.api.ui.Field#drawFocus(Graphics, boolean)
    */
    protected void drawFocus(Graphics graphics, boolean on)
    {//Do nothing
    }

    /**
    *Field implementation.
    *@see net.rim.device.api.ui.Field#layout(int, int)
    */
    protected void layout(int width, int height)
    {
    setExtent(Math.min(width, getPreferredWidth()), Math.min(height, getPreferredHeight()));
    }

    /**
    *Field implementation.
    *@see net.rim.device.api.ui.Field#paint(Graphics)
    */
    protected void paint(Graphics graphics)
    {
    //First draw the background colour and picture
    //graphics.setColor(Color.LIGHTPINK);
    //graphics.fillRect(0, 0, getWidth(), getHeight());
    graphics.drawBitmap(0, 0, _currentPicture.getWidth(), _currentPicture.getHeight(), _currentPicture, 0, 0);
    if (_label != null && (_label.length() > 0)) {
    //Then draw the text
    graphics.setColor(0x47a13d);
    graphics.setFont(_font);
    int yOffset = (_currentPicture.getHeight() - _font.getHeight()) / 2;

    graphics.drawText(_label, 4, yOffset, (int) (getStyle() & DrawStyle.ELLIPSIS | DrawStyle.HALIGN_MASK), _currentPicture.getWidth() - 6);
    }
    }

    /**
    *Overridden so that the Event Dispatch thread can catch this event
    *instead of having it be caught here..
    *@see net.rim.device.api.ui.Field#navigationClick(int, int)
    */
    protected boolean navigationClick(int status, int time)
    {
    fieldChangeNotify(1);
    return true;
    }

    }





    I use it in the sample helloWorld application that comes with the jde as follows





    package com.rim.samples.device.helloworlddemo;

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

    /*
    * BlackBerry applications that provide a user interface must extend
    * UiApplication.
    */
    class HelloWorldDemo extends UiApplication
    {
    /**
    * Entry point for application.
    */
    public static void main(String[] args)
    {
    // Create a new instance of the application.
    HelloWorldDemo theApp = new HelloWorldDemo();

    // To make the application enter the event thread and start processing messages,
    // we invoke the enterEventDispatcher() method.
    theApp.enterEventDispatcher();
    }

    /**
    * <p>The default constructor. Creates all of the RIM UI components and pushes the
    * application's root screen onto the UI stack.
    */
    private HelloWorldDemo()
    {
    // Push the main screen instance onto the UI stack for rendering.
    pushScreen(new HelloWorldScreen());
    //pushScreen(new TestScreen());
    }
    }


    /**
    * Create a new screen that extends MainScreen, which provides default standard
    * behavior for BlackBerry applications.
    */
    /*package*/
    final class HelloWorldScreen extends MainScreen implements FieldChangeListener
    {

    private PictureBackgroundButtonField _searchButton;

    /**
    * HelloWorldScreen constructor.
    */
    HelloWorldScreen()
    {
    // Add a field to the title region of the screen. We use a simple LabelField
    // here. The ELLIPSIS option truncates the label text with "..." if the text
    // is too long for the space available.
    LabelField title = new LabelField("Hello World Demo" , LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
    setTitle(title);

    // Add a read only text field (RichTextField) to the screen. The RichTextField
    // is focusable by default. In this case we provide a style to make the field
    // non-focusable.
    //add(new RichTextField("Hello World!" ,Field.NON_FOCUSABLE));

    _searchButton = new PictureBackgroundButtonField(Bitmap.getBitmapResou rce("button_on_small.png"),
    Bitmap.getBitmapResource("button_off_small.png"), "Find", Field.FOCUSABLE | Field.FIELD_HCENTER);
    _searchButton.setChangeListener(this);
    add(_searchButton);
    }

    /**
    *We are implementing this method to intercept property changes on the send button.
    */
    public void fieldChanged(Field field, int context)
    {
    if (field == _searchButton){
    System.out.println("Search Button Field Changed ************************************************** *************************");
    }
    }

    /**
    * Display a dialog box to the user with "Goodbye!" when the application
    * is closed.
    *
    * @see net.rim.device.api.ui.Screen#close()
    */
    public void close()
    {
    // Display a farewell message before closing application.
    Dialog.alert("Goodbye!");
    System.exit(0);

    super.close();
    }
    }





    Now note that here irrespective of where i click on my screen the fieldChanged method is called for _searchButton.I expect the fieldChanged method of _searchButton to be called when i click on thatcustom button only and not when i click anywhere else on the screen.Can anyone plz help me with this.Waiting for your valuable suggestions.





    Thanks

    Sagar
    05-14-09 02:12 AM
LINK TO POST COPIED TO CLIPBOARD