0

What I am having:

  • I am having a imageview on a linear layout. I want to detect onTouch of imageview.
  • I do not want to use onClick because my implementation requires onTouch Imageview is the child of linearLayout

What is happening:

  • Two touch events are firing when i click on image one from image and another from the linear layout(parent)

Question:

  • How can I disable onTouch of linearLayout(parent)retaining the onTouch of Imageview

Code:

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    imgUsrClrId.setOnTouchListener(imgSourceOnTouchListener);
}


 OnTouchListener imgSourceOnTouchListener= new OnTouchListener(){

    @Override
    public boolean onTouch(View view, MotionEvent event) {

        Log.d("", "");

        return true;
    }};
Devrath
  • 42,072
  • 54
  • 195
  • 297
  • Check out this.. this may help http://stackoverflow.com/questions/19442082/how-to-prevent-ontouch-for-parent-linear-layout-from-executing-multiple-times-ch – Praveena Nov 07 '14 at 08:33
  • 1
    @Praveen ... I need `OnTouchListener()` so i can't use that solution ! – Devrath Nov 07 '14 at 09:23

2 Answers2

1

Touch event is fired for only one view at a time, and here in your code touch event is fired for imageview but as we know touchListener will be called for every MotionEvent.ACTION_DOWN, MotionEvent.ACTION_UP, and MotionEvent.ACTION_MOVE. So if you want only one event to be fired at a time, ie MotionEvent.ACTION_DOWN or MotionEvent.ACTION_UP then write it in this way:

 @Override
 public boolean onTouchEvent(MotionEvent ev) {

        final int action = ev.getAction();

        switch (action) {

            // MotionEvent class constant signifying a finger-down event

            case MotionEvent.ACTION_DOWN: {
                 //your code

                break;
            }

            // MotionEvent class constant signifying a finger-drag event  

            case MotionEvent.ACTION_MOVE: {

                   //your code

                  break;

            }

            // MotionEvent class constant signifying a finger-up event

            case MotionEvent.ACTION_UP:
              //your code

                break;

        }
        return true;
    }
CuriousSuperhero
  • 6,531
  • 4
  • 27
  • 50
Anupriya
  • 1,663
  • 3
  • 17
  • 28
0

There are no multiple touch events generated from different views its all touch events from same ImageView I did test like below

Have a trace the viewID from which it

 ImageView imageView = (ImageView) findViewById(R.id.imageView);
 Log.i("Tag","ImageView ID :"+imageView.getId());
 imageView.setOnTouchListener(new View.OnTouchListener()
 {
        @Override
        public boolean onTouch(View v, MotionEvent event)
        {
            Log.i("Tag","OnTouch View ID :"+v.getId());
            return true;
        }
 });

and when you return true from onTouch event will be consumed.

and here is output

ImageView ID :2131230721

OnTouch View ID :2131230721
OnTouch View ID :2131230721
OnTouch View ID :2131230721
Kirtan
  • 1,782
  • 1
  • 13
  • 35
  • How can I reduce the click event from generating twice to a single event ! – Devrath Nov 07 '14 at 09:20
  • can you tell me the purpose to do that because touch event is really there for providing us touch data whenever its there not just single time ? – Kirtan Nov 07 '14 at 09:23
  • Bit of an ugly solution but you could keep a state boolean per image to indicate whether it has been touched and only act when it isn't already doing stuff, then release it again after it is done. This should work because the touch events happen miliseconds apart, where it ignores the second event. – G_V Nov 07 '14 at 09:28
  • @user3427079 ... This was a fix & yes it worked ...but why the onTouch event fires twice ? any ideas ? – Devrath Nov 07 '14 at 09:49
  • No idea, are you sure you aren't setting multiple onClicks on the items? Like in both XML and code or in multiple places in the code somehow? – G_V Nov 10 '14 at 10:36