0

I am trying to add both OnClickListener and OnTouchListener to my image view. Following is how the image view is created

dialogImage = (ImageView)findViewById(R.id.dialogImage);

Following is how the listeners are set

dialogImage.setOnClickListener(dialogBoxClicked);
dialogImage.setOnTouchListener(imageViewSwiped);

Following is the listener method implementation

public OnClickListener dialogBoxClicked = new OnClickListener()
    {

        @Override
        public void onClick(View v) 
        {
                      //To do has been removed because the code is too big
            }

    };

OnTouchListener imageViewSwiped = new OnSwipeTouchListener()
    {
         public void onSwipeRight() 
         {
             currentlyActiveQuestion++;
             currentWord = words.get(currentlyActiveQuestion);
             setUI();
         }

          public void onSwipeLeft() 
          {
              currentlyActiveQuestion--;
              currentWord = words.get(currentlyActiveQuestion);
              setUI();
          }
    };

Here the OnTouchListener is implemented by a class called OnSwipeTouchListener to monitor left and right swipes. This class can be found here - https://stackoverflow.com/a/12938787/1379286

But the problem now is, when I set the OnTouchListener to the image view, the OnClickListener is not responding / do not do what it should do. ImageView is only responding to OnTouchListener. If I remove OnTouchListener then the OnClickListener works. I tested this in a virtual device WVGA5.1 and Galaxy Nexus in eclipse and not in a real phone because I do not have one.

How can I solve this?

EDIT

Any code example will be greatly appreciated

Community
  • 1
  • 1
PeakGen
  • 21,894
  • 86
  • 261
  • 463

3 Answers3

2

You may call View.performClick() when action_up. This results in the click event being called when an actual click happens. Hope it helps.

your_txtView.setOnClickListener(new TextView.OnClickListener(){
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }
});

your_txtView.setOnTouchListener(new TextView.OnTouchListener(){
        @Override
public boolean onTouch(View v, MotionEvent event) {
    if (MotionEvent.ACTION_DOWN == event.getAction()) {

    } else if (MotionEvent.ACTION_UP == event.getAction()) {
        v.performClick();
    }

    return true;
}
});
Ankit Aggarwal
  • 2,367
  • 24
  • 30
  • OK, is there any possiblility where we can not monitor the actual swiping in a virtual device? – PeakGen Jul 16 '13 at 08:48
  • @Knight: Could you elaborate. Why don't you want to monitor actual swiping? You are doing something on swiping right? Or I didn't understand you properly. – Ankit Aggarwal Jul 16 '13 at 08:53
  • No, I mean I followed your advice. Now can do swiping and click. But the problem is, most of the times it get "clicked" when I swipe. Sometimes it get clicked and swiped. So I thought it might be because I am using a virtual device, the mouse pointer is the one which indicates the "finger" – PeakGen Jul 16 '13 at 08:57
  • Yes, yes that is because of using a virtual device. Run your code on an actual device it will work. – Ankit Aggarwal Jul 16 '13 at 08:58
1

The OnTouchListener hooks the click-event. Handle the click event in it instead. Check out the answer on this question

Community
  • 1
  • 1
mach
  • 8,315
  • 3
  • 33
  • 51
0

From my experience, If you can't have both onTouchListener and onClickListener for the same view. If you want your onClickListener to work, set clickable="true" in the XML.

Naresh
  • 3,174
  • 1
  • 17
  • 22