What I am having:
imageview
on a linear layout. I want to detect
onTouch
of imageview
.onClick
because my implementation requires
onTouch Imageview
is the child of linearLayoutWhat is happening:
linear layout(parent)
Question:
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;
}};
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;
}