Arindam Mukherjee
Arindam Mukherjee

Reputation: 2285

Touch event is not working

I am developng one app for touch and type phone. Now in one screen of the application, there is 3 buttons.

Here if i touch in the three buttons, it is working. But if i touch outside the buttons, means in the top bar or in the bottom bar, then also touch event is working. Suppose in the below, Add More button is focused. So if i am touching in the bottom bar, then also button touch is working. To resolve this problem, i wrote the below code:

protected boolean touchEvent(TouchEvent event) {
    try {

        int touchXPos = event.getX(1);
        int touchYPos = event.getY(1);
        int addBtnXPos = btnAddMore.getLeft();
        int saveBtnXPos = btnSave.getLeft();
        int helpBtnXpos = btnHelp.getLeft();

        int vfmTitleTouch = m_vfmTitle.getHeight();
if(event.getEvent() == TouchEvent.CLICK)
      {
        if ((touchXPos >= addBtnXPos + 40) && (touchXPos <= (addBtnXPos + btnAddMore.getWidth() + 40)) && (touchYPos <= screenHeight -10) && (touchYPos >= (screenHeight -10 - btnAddMore.getHeight())) /*&& (touchYPos < (screenHeight-gmYPos)) */) {
            Logger.out("touchEvent", "------------------------------1");
            showPopUp();

        } else if ((touchXPos >= saveBtnXPos + 40) && (touchXPos <= (saveBtnXPos + 40 + btnSave.getWidth())) && (touchYPos <= screenHeight -10) && (touchYPos >= (screenHeight -10 - btnSave.getHeight())) /* && (touchYPos < (screenHeight-gmYPos))*/) {
            Logger.out("touchEvent", "------------------------------2");
            saveToDb();

        } else if ((touchXPos >= helpBtnXpos) && (touchXPos <= (helpBtnXpos + btnHelp.getWidth())) && (touchYPos <= (btnHelp.getTop() + btnHelp.getHeight())) && (touchYPos >= btnHelp.getTop()) /* && (touchYPos < (screenHeight-gmYPos))*/ ) {
            Logger.out("touchEvent", "------------------------------3");
            UiApplication.getUiApplication().pushScreen(new HelpScreen());

        } else if ((touchYPos <= screenHeight - hfmBtn.getHeight()) && (touchYPos >= (vfmTitleTouch))) {
            Logger.out("touchEvent", "------------------------------4");
            //Logger.out("touchEvent", "touchY::::" +touchYPos  + "Vfm Title::::" +vfmTitleTouch  + "  "+"GM Y Pos"+  gmYPos);

        } 
            return true;
        }
    } catch (Exception e) {
    }

    return super.touchEvent(event);
}

But it is not working..Can any one help me out?? And touch event should work in the middle of the screens like for check boxes also..

Thanks..This is the screen shot of the screen

Upvotes: 0

Views: 305

Answers (1)

Rupak
Rupak

Reputation: 3674

Check the answers of the question BlackBerry touchEvent outside Field triggers fieldChanged provided by Paul Sylliboy and Arhimed.

I have pasted those links on comment first, but later removed it from there to share the code snippet for killing unwanted touch events, which I found very useful.

protected boolean touchEvent(TouchEvent message) {
    int event = message.getEvent();
    // you can add other events 
    if (event == TouchEvent.CLICK || event == TouchEvent.UNCLICK) {
        // Kill all click events that happen outside any fields
        if (getFieldAtLocation(message.getX(1), message.getY(1)) == -1)         
            return true;
    }

    return super.touchEvent(message);
}

// The regular getFieldAtLocation returns wrong values in 
// open spaces in complex managers, so we override it
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;
}

Upvotes: 2

Related Questions