CustomButtonField in BlackBerry - blackberry

Plz brifly explain the meaning of ....
First i create a CustomButtonField class, and then i found out that if i din't write the
public boolean isFocusable()
{
return true;
}
function, it doesnot responds to user events like mouse click....
and also plz explain briefly the meaning of the following functions
protected boolean navigationClick(int status, int time)
{
fieldChangeNotify(0);
return true;
}
protected boolean keyChar(char character, int status, int time)
{
if (character == Keypad.KEY_ENTER)
{
fieldChangeNotify(0);
return true;
}
return super.keyChar(character, status, time);
}

If you want a field to behave like a button
you must set Field.FOCUSABLE style bit.
setChangeListener to CustomButtonField.
protected boolean navigationClick(int status, int time) {
fieldChangeNotify(0);
return true;
}
if user clicks on this button, we can see that button has changed (fieldChangeNotify(0);)
and listener of the button works.
protected boolean keyChar(char character, int status, int time)
{
if (character == Keypad.KEY_ENTER)
{
fieldChangeNotify(0);
return true;
}
return super.keyChar(character, status, time);
}
by this method if 'Enter key' pressed, button says i am changed.(fieldChangeNotify(0);)

Related

Invoking navigationClick from navigationClick 'father' in Blackberry

Edited
...
I´m trying to deploy a new component in my BlackBerry app. I have a new class that extends of HorizontalFieldManager, where I instanced a new ListField. Inside of this, I overwrote the navigationClick method and I open a popup. The name of this component is CustomComponentHorizontal. I use this component in other component customized, its name is CustomComponentVertical. This is the code:
In Main.java:
...
CustomComponentVertical ccv=new CustomComponentVertical();
...
add(ccv);
In CustomComponentVertical
...
CustomComponentHorizontal cch=new CustomComponentHorizontal{
...
protected boolean navigationClick(int status, int time) {
//Should I do something here??
return super.navigationClick(status, time);
}
};
...
add(cch);
In CustomComponentHorizontal:
public class CustomComponentHorizontal extends HorizontalFieldManager {
ListField choiceField=null;
PopupScreen popup=null;
public CustomComponentHorizontal (){
choiceField = new ListField(){
public boolean navigationClick(int status, int time) {
Field focus = UiApplication.getUiApplication().getActiveScreen().getLeafFieldWithFocus();
if (focus instanceof ListField) {
popup = new ChoicePopupScreen(10, 50, choices);
popup.setChoice(choices);
UiApplication.getUiApplication().pushScreen(popup);
setPopup(popup);
return super.navigationClick(status, time);
}
};
}
}
}
My purpose is that when I click in my component, it is launched the navigationClick of listfield. When I put the focus in my component and click with trackpad, the popup no open. But, If I touch the screen over the component, and then I click with trackpad, popup open.
How could I open the popup from component without to use the touch event?
Thank you very much.
I copy pasted your code. There were some errors, and your callback was missing. So I fixed it up a bit and created a simple callback to test with, and the navigationClick fires. Hopefully this helps, let me know if there is a different issue I didn't understand.
public CustomComponent()
{
elements = new Vector();
elements.addElement("Element 0");
elements.addElement("Element 1");
elements.addElement("Element 2");
elements.addElement("Element 3");
elements.addElement("Element 4");
elements.addElement("Element 5");
choiceField = new ListField(elements.size())
{
public boolean navigationClick(int status, int time)
{
int index = getSelectedIndex();
Dialog.inform((String) elements.elementAt(index));
return true;
}
};
choiceField.setCallback(new ListFieldCallback()
{
public int indexOfList(ListField listField, String prefix, int start)
{
return elements.indexOf(prefix, start);
}
public int getPreferredWidth(ListField listField)
{
return Display.getWidth();
}
public Object get(ListField listField, int index)
{
return elements.elementAt(index);
}
public void drawListRow(ListField listField, Graphics graphics, int index, int y, int width)
{
String obj = (String) get(listField, index);
graphics.setColor(0x000000);
graphics.drawText(obj, 0, y, 0, width);
}
});
add(choiceField);
}

Back Button handling in blackberry

I want to come to home screen when clicking on the back button from my application in blackberry. I have searched for this in google and stackoverflow, But I didn't get any solution. Anybody help for this.
In My Starting Screen i wrote like this for back button.
protected boolean keyDown(int keycode, int time)
{
if (Keypad.key(keycode) == Keypad.KEY_ESCAPE)
{
close();
return true;
}
else
{
return super.keyDown(keycode, time);
}
}
In my HomeScreen i wrote like this.
protected boolean keyDown(int keycode, int time)
{
if (Keypad.key(keycode) == Keypad.KEY_ESCAPE)
{
UiApplication.getUiApplication().pushScreen(new StartingScreen());
return true;
}
else
{
return super.keyDown(keycode, time);
}
}
From my application HomeScreen I am able to come to starting screen of the my application. after that when i click on back button from starting screen i need to go to blackberry home screen that means i need to exit the app and come out of that. I wrote close(); to come to blackberry home screen. but its not working. it is again coming to my application homescreen.
Override keyDown method in your subclass of MainScreen.
protected boolean keyDown(int keycode, int time) {
int key = Keypad.key(keycode);
if(key==Characters.ESCAPE){
// do something here
return true;
}
return super.keyDown(keycode, time);
}
To come to home screen
while(!(UiApplication.getUiApplication().getActiveScreen() instanceof HomeScreen)){
UiApplication.getUiApplication().popScreen(UiApplication.getUiApplication().getActiveScreen());
}
I got the Solution... i have written lik this.
public boolean onClose()
{
int choose=Dialog.ask(Dialog.D_YES_NO, "Are you sure Want to Exit?");
if(choose==Dialog.YES)
{
System.exit(0);
}
return true;
}
Try this -
public class yourclass extends MainScreen{
public yourclass(){
}
}
public boolean onClose() {
Application.getApplication().invokeLater(new Runnable() {
public void run() {
//close this screen and push your home screen
}
});
return true;
}

I cannot access the KeyPressed function for when i press the ESCAPE key

The first problem is that the addKeyListener does is redlined wherever I place it. I've looked at various different examples online but it seems that Im missing something.
here is my code:
import net.rim.device.api.system.KeyListener;
import net.rim.device.api.ui.Keypad;
public class BBMIDLET extends javax.microedition.midlet.MIDlet implements KeyListener
public void startApp() {
Display.init(this);
addKeyListener(new TestKeyPadListener());
}
public class TestKeyPadListener implements KeyListener {
public boolean keyChar(char key, int status, int time) {
System.out.println("key: " + key);
return false;
}
public boolean keyDown(int keycode, int time) {
System.out.println("keycode: " + keycode);
if (Keypad.key(keycode) == Keypad.KEY_ESCAPE) {
System.out.println("Hi");
return false;
}
return true;
}
public boolean keyUp(int keycode, int time) {
throw new UnsupportedOperationException("Not supported yet.");
}
public boolean keyRepeat(int keycode, int time) {
throw new UnsupportedOperationException("Not supported yet.");
}
public boolean keyStatus(int keycode, int time) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
}
Thanks in advance
addKeyListener(KeyListener l) is not a method of javax.microedition.midlet.MIDlet or net.rim.device.api.system.KeyListener and you did not declare it anywhere else in your BBMIDLET class so it is undefined.

How to set action for button on this custom dialog?

I make a custom Dialog like as :
public class CustomDialog extends Dialog {
public CustomDialog(String s) {
super(s, new String[] {"View","Cancel"}, new int [] {1,2}, 1, Bitmap.getPredefinedBitmap(Bitmap.EXCLAMATION), Manager.FOCUSABLE);
}
How can I set action for "View button" and " Cancel button " ?
I searched and not found what I have to do .
Please help me !
Attach a DialogClosedListener to your CustomDialog using Dialog.setDialogClosedListener(). When someone clicks either of the buttons, the DialogClosedListener.dialogClosed() method will be called and the button index will be passed as the choice parameter.
Check out this code.. this might help you..
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.container.HorizontalFieldManager;
public class CustomAlertDialog extends Dialog {
public CustomAlertDialog() {
super("Your Custom message for Dialoug" , null, null, Dialog.DISCARD, null, Dialog.VERTICAL_SCROLL);
HorizontalFieldManager hfm = new HorizontalFieldManager();
ButtonField view = null;
view = new ButtonField("view") {
protected boolean navigationClick(int status, int time) {
// do what ever you want
return true;
}
protected boolean keyChar(char key, int status, int time) {
// do what ever you want
return true;
}
};
ButtonField cancel = null;
cancel = new ButtonField("Cancel") {
protected boolean navigationClick(int status, int time) {
// do what ever you want
return true;
}
protected boolean keyChar(char key, int status, int time) {
// do what ever you want
return true;
}
};
hfm.add(view);
hfm.add(cancel);
this.add(hfm);
}
}

BlackBerry Bitmap listener

I have a code similar to the one below, painting over the mapfields this mIcon several times.
How can I add a click listener to this bitmap ? I am using bb 5.0
public Bitmap mIcon;
mIcon = Bitmap.getBitmapResource("pcture1.png");
protected void paint(Graphics g) {
super.paint(g);
mDest = new XYRect(....);
g.drawBitmap(mDest, mIcon, 0, 0);
}
Override BitmapField and modify the isFocusable(), navigationClick(), keyChar(), and trackwheelClick() methods.
public class ImageButtonField extends BitmapField
{
public ImageButtonField(Bitmap image)
{
super(image);
}
public boolean isFocusable()
{
return true;
}
protected boolean navigationClick(int status, int time)
{
fieldChangeNotify(0);
return true;
}
protected boolean trackwheelClick(int status, int time)
{
fieldChangeNotify(0);
return true;
}
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);
}
}

Resources