Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 152
Registered: ‎07-17-2009
My Device: Not Specified

How to handle ButtonField & BitmapField Click (Touch) events in Blackberry Storm?

I have created a ButtonField & a BitmapField like..

 

 

public class MyCanvas extends MainScreen implements FieldChangeListener { HorizontalFieldManager hfm; private Bitmap startBitmap; private BitmapField startBitmapField; private ButtonField okButton; MyCanvas() { hfm = new HorizontalFIeldManager(); startBitmap = Bitmap.getBitmapResource("start.png"); startBitmapField = new BitmapField(startBitmap); startBitmapField.setChangeListener(this); hfm.add(startBitmapField); okButton = new ButtonField("Oke", ButtonField.CONSUME_CLICK | ButtonField.NEVER_DIRTY); okButton.setChangeListener(this); hfm.add(okButton); } public void fieldChanged(Field field, int context) { if(field == startBitmapField) { System.out.println("Touched START..."); } else if(field == okButton) { System.out.println("Touched Ok..."); } } }

 

 

But the ButtonField or BitmapField click is not happening in Blackberry 4.7 simulator.

 

I want to build it for Blackberry Storm so I m using Blackberry 4.7

 

How to handle click/touch events for ButtonField & BitmapField for Blackberry Storm?

 

 

Developer
Posts: 984
Registered: ‎02-10-2009
My Device: Not Specified

Re: How to handle ButtonField & BitmapField Click (Touch) events in Blackberry Storm?

Try with overriding touchEvent. Like:

 

protected boolean touchEvent( TouchEvent event ) { switch( event.getEvent() ) { case TouchEvent.CLICK: //handle click event here return true; case TouchEvent.DOWN: //handle down event here return true; case TouchEvent.MOVE: //handle move event here return true; } return false; }

 

Regards

Bikas

 

 

Developer
Posts: 152
Registered: ‎07-17-2009
My Device: Not Specified

Re: How to handle ButtonField & BitmapField Click (Touch) events in Blackberry Storm?

I have 2 buttons. Then how to check the TouchEvent.CLICK of that buttons?

 

like in ..

 

 

public void fieldChanged(Field field, int context) { if(field == deleteButton) // if Delete button is clicked { System.out.println("Touched DEL"); } else if(field == okButton) // if Okbutton is clicked { System.out.println("Touched OK"); } }

 

 

 

 

 

Developer
Posts: 984
Registered: ‎02-10-2009
My Device: Not Specified

Re: How to handle ButtonField & BitmapField Click (Touch) events in Blackberry Storm?

Try with writing your button click event inside the TouchEvent.CLICK block. Something like:

 

switch( event.getEvent() ) { case TouchEvent.CLICK: if(deleteButton.isFocus()) { System.out.println("Touched DEL"); } else if(okButton.isFocus()) { System.out.println("Touched OK"); } return true; //............. //............. }

 

Regards

Bikas

 

 

Developer
Posts: 152
Registered: ‎07-17-2009
My Device: Not Specified

Re: How to handle ButtonField & BitmapField Click (Touch) events in Blackberry Storm?

[ Edited ]

 

 

Its working but everytime the same button is invoked which is having focus.

 

Means if "Ok" button is having focus then even though u clicked on Delete button Ok button is called

 

Now I m testing on Simulator 4.7. Is it Simulaor issue? Will the same code work on device?

 

I m creating the ButtonField & BitmapFields as 

 

 

okButtonField = new ButtonField("Ok", BitmapField.HIGHLIGHT_SELECT | BitmapField.FOCUSABLE);

 

startBitmapField = new BitmapField(startBitmap, BitmapField.HIGHLIGHT_SELECT | BitmapField.FOCUSABLE);

 

m i right?

 

 

Message Edited by shreyasb on 08-31-2009 05:31 AM
Developer
Posts: 84
Registered: ‎09-21-2008
My Device: Not Specified

Re: How to handle ButtonField & BitmapField Click (Touch) events in Blackberry Storm?

[ Edited ]

You should be able to move the TouchEvent handler into the individual button fields to get better granularity over which field is touched..

 

 

okButtonField = new ButtonField("Ok", BitmapField.HIGHLIGHT_SELECT | BitmapField.FOCUSABLE){

protected boolean touchEvent( TouchEvent event ) {

switch( event.getEvent() ) {

case TouchEvent.CLICK:

//handle click event here

return true;

case TouchEvent.DOWN:

//handle down event here

return true;

case TouchEvent.MOVE:

//handle move event here

return true;

}

return false;

}

}

 


 

Message Edited by knight9 on 08-31-2009 10:28 AM
Developer
Posts: 152
Registered: ‎07-17-2009
My Device: Not Specified

Re: How to handle ButtonField & BitmapField Click (Touch) events in Blackberry Storm?

  I m creating the ButtonField & BitmapFields as

 

okButtonField = new ButtonField("Ok", BitmapField.HIGHLIGHT_SELECT | BitmapField.FOCUSABLE); startBitmapField = new BitmapField(startBitmap, BitmapField.HIGHLIGHT_SELECT | BitmapField.FOCUSABLE);

 

Its working with..

protected boolean touchEvent(TouchEvent event)
{
switch( event.getEvent() )
{
case TouchEvent.DOWN: ........
return true;
case TouchEvent.MOVE: .......
return true;
case TouchEvent.UP: ........
return true;

case TouchEvent.CLICK:
if(deleteButton.isFocus())
{
System.out.println("Touched DEL ..........");
}
else if(okButton.isFocus())
{
System.out.println("Touched OK ..........");
}
else if(startBitmapField.isFocus())
{
System.out.println("Touched START ..........");
}
return true;
}
return false;
}

 

but everytime the same button is invoked which is having focus.

Means if "Ok" button is having focus then even though u clicked on "Delete" button "Ok" button is called.

 

So how to change the focus on Button Click? means whichever ButtonField or BitmapField is clicked, should get the focus?

is there any method to check  "button.isClicked() like button.isFocus() " ?

 


 

 

Developer
Posts: 984
Registered: ‎02-10-2009
My Device: Not Specified

Re: How to handle ButtonField & BitmapField Click (Touch) events in Blackberry Storm?

[ Edited ]

Try this way:

 

protected boolean touchEvent(TouchEvent event)
{
switch( event.getEvent() )
{
case TouchEvent.DOWN: ........
return true;
case TouchEvent.MOVE: .......
return true;
case TouchEvent.UP: ........
return true;

case TouchEvent.CLICK:
int index = getFieldAtLocation(event.getX(1),
event.getY(1));
// Ignore click events outside any fields
if (index == -1)return true;
Field field = getField(index);
if(field.equals(deleteButton))
{
System.out.println("Touched DEL ..........");
}
else if(field.equals(okButton))
{
System.out.println("Touched OK ..........");
}
else if(field.equals(startBitmapField))
{
System.out.println("Touched START ..........");
}
return true;
}
return false;
}

public int getFieldAtLocation(int x, int y)
{
XYRect rect = new XYRect();
int index = getFieldCount() -1;
while (index >= 0)
{
getField(index).getExtent(rect);
if (rect.contains(x, y))
break;
--index;
}
return index;
}

 

Regards

Bikas

 

 

Message Edited by bikas on 09-03-2009 06:36 PM
Developer
Posts: 152
Registered: ‎07-17-2009
My Device: Not Specified

Re: How to handle ButtonField & BitmapField Click (Touch) events in Blackberry Storm?

[ Edited ]

Thanks Bikas..

 

I got TouchEvent.CLICK for all fields. But now the problem is ..


 

Cliking is working in HFM of ButtonFields but outside of Field area also. Means even though I clicked on blank area in HFM where there is no ButtonField then also Button Click is invoked.

When I call ..

 

 

int fieldIndex = getFieldAtLocation(x, y);

 

if there is no Field added to Manager then "fieldIndex" should come "-1". But everytime it comes 1 or 2. I checked x, y values. They are correct then why does "fieldIndex" does not come "-1"  if clicked outside Field area.

 

Any solution?

 

 

Message Edited by shreyasb on 09-03-2009 08:38 AM
Developer
Posts: 984
Registered: ‎02-10-2009
My Device: Not Specified

Re: How to handle ButtonField & BitmapField Click (Touch) events in Blackberry Storm?

Override getFieldAtLocation(x,y).

Have a look at my above post.

 

Regards

Bikas