Question

Hello fellow BB programmers,

if you look at the native Contacts (Addressbook) application on a Blackberry 6 phone with touch screen - it has a very natural behaviour:

  1. On a short tap the default action is being executed - viewing the selected addressbook entry
  2. On a long touch and hold a menu with several actions is displayed:

Addressbook

I'm trying to create an app with a ListField and similar (and intuitive) behaviour myself: run default action on a short tap and display a menu in the middle of screen with several secondary actions on a longer touch.

I've searched a lot and unfortunately only managed to create a test app with exactly opposite behaviour sofar:

I listen for a TouchGesture.HOVER and run editMenu.run(). And for the short tap a menu comes by itself (I haven't found yet, what makes it appear, some method in MainScreen/Screen?). I've tried running onMenu(0) but the menu appears in the top/right corner instead of screen center.

MyList app

Below is my very simple test code MyList.java, please help me to fix it:

package mypackage;

import java.util.*;
import net.rim.device.api.collection.*;
import net.rim.device.api.collection.util.*; 
import net.rim.device.api.system.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.decor.*;
import net.rim.device.api.util.*;

public class MyList extends UiApplication {
    public static void main(String args[]) {
        MyList app = new MyList();
        app.enterEventDispatcher();
    }

    public MyList() {
        pushScreen(new MyScreen());
    }
} 

class MyScreen extends MainScreen {

    ObjectListField myList = new ObjectListField() {
        protected boolean touchEvent(TouchEvent event) {
            if (event.getEvent() == TouchEvent.GESTURE) {
                TouchGesture gesture = event.getGesture();
                if (gesture.getEvent() == TouchGesture.HOVER) {
                    System.err.println("XXX hover=" + gesture.getHoverCount() + ", index=" + myList.getSelectedIndex());
                    editMenu.run();
                    // onMenu(0);
                    return true;
                }
            }
            return super.touchEvent(event);
        }
    };

    private final MenuItem addMenu = new MenuItem("Add item", 0, 0) {  
        public void run() { 
            Status.show("Adding new item");
        }
    };

    private final MenuItem editMenu = new MenuItem("Edit item", 1, 0) {  
        public void run() { 
            Status.show("Editing existing item: " + myList.getSelectedIndex());
        }
    };

    private final MenuItem removeMenu = new MenuItem("Remove item", 2, 0) {  
        public void run() { 
            Status.show("Removing existing item: " + myList.getSelectedIndex());
        }
    };


    public MyScreen() {
        setTitle("How to display menu on long click?");
        myList.set(new String[] { "Item 1", "Item 2", "Item 3", "Item 4", }); 
        add(myList);

        addMenuItem(addMenu);
        addMenuItem(editMenu);
        addMenuItem(removeMenu);
    }
}

Thank you! Alex

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top