Blackberry: how to implement BitmapField as Button

1k Views Asked by At

I'm kind of new for this blackberry programming... i want to make custom button from bitmap and BitmapField, but when I'm clicking the bitmapfield, it always show the menu item first("open") then do the run method.

i want to make it click-able, when i clik the trackpad it will do the run method(directly do the run method without showing the "open" menu) but i can't figure out how to make it that way...

this is my code :

private void createFields() 
{
    Bitmap atas = Bitmap.getBitmapResource("banerputih.png");
    add(new BitmapField(atas, Field.FIELD_HCENTER));

    //separator
    add(new SeparatorField());
    Bitmap satu = Bitmap.getBitmapResource("wisata.png");
    Bitmap dua = Bitmap.getBitmapResource("trasport.png");
    Bitmap tiga = Bitmap.getBitmapResource("other.png");
    Bitmap empat = Bitmap.getBitmapResource("aboutme.png");
    Bitmap three = Bitmap.getBitmapResource("3.gif");
    add( new BitmapField(satu, Field.FOCUSABLE | Field.FIELD_HCENTER |Field.HIGHLIGHT_FOCUS));
    add(new BitmapField(dua, Field.FOCUSABLE | Field.FIELD_HCENTER | Field.HIGHLIGHT_FOCUS));
    add(new BitmapField(tiga, Field.FOCUSABLE | Field.FIELD_HCENTER | Field.HIGHLIGHT_FOCUS));
    add(new BitmapField(empat, Field.FOCUSABLE | Field.FIELD_HCENTER | Field.HIGHLIGHT_FOCUS));
    add(new SeparatorField()); 
    add(new BitmapField(three, Field.FIELD_HCENTER));
    add(new SeparatorField());

    //Disini ditambahin Gambar
    //panjang 480 pixel

}

//menambahkan menu open TODO
MenuItem _openAction = new MenuItem("Open",100000,10)
{

    public void run() 
    {
        getValue();

    }

};

//menu about TODO
MenuItem _AboutAction = new MenuItem("About",100,100)
{
    public void run()
    {
        UiApplication.getUiApplication().pushScreen(new AboutScreen());
    }
};

//method getValue();
protected void getValue()
{
    Field f = getFieldWithFocus();
    if (f instanceof BitmapField)

    {
        BitmapField b = (BitmapField)f;
        b.getIndex();
        //untuk cek index
        //Dialog.alert(Integer.toString(b.getIndex()));

        if (b.getIndex() == 2)
        {

            UiApplication.getUiApplication().pushScreen(new wisataScreen());
        }
        else if (b.getIndex()== 3)
        {
            UiApplication.getUiApplication().pushScreen(new TransportScreen());
        }
        else if (b.getIndex()== 4)
        {
            UiApplication.getUiApplication().pushScreen(new HotelScreen());

        }else if (b.getIndex()== 5)
        {
            UiApplication.getUiApplication().pushScreen(new AboutScreen());
        }
    }
}

public void makeMenu(Menu m, int instance)
{
    m.add(_openAction);
    m.add(_AboutAction);
    super.makeMenu(m, instance);
}

}

2

There are 2 best solutions below

0
On

when creating a ButtonField, you should always specify the style ButtonField.CONSUME_CLICK. If you don’t, the click event will be passed onto the screen, and a menu will open when the user clicks the button, though your button will still cause an action to be performed. for example:

ButtonField clearButton = new ButtonField("Clear", ButtonField.CONSUME_CLICK);

1
On

You shouldn't have to write a simple component like a bitmap button field by yourself. RIM has provided some nice samples that you can freely include and they are described here. You can pull down the source from GitHub and the only you likely care about the most is the BitmapButtonField which extends the BaseButtonField.

In general, I would also change your structure in your example code so that the each button/bitmap field manage its own menu and click handling rather than the parent screen routing catching/routing the clicks. That will allow you to reuse your controls cleanly.

Secondly, when the user clicks on the button you dont want them to see "Run" option. You should just run the action as per standard BB UX. Thats the behavior you'll see in the samples. If you really need the ability to give the user a "double click" experience then you can implement a makeContextMenu method within the BaseButtonField.