0

I am in a position to implement different actions for OnClick and OnTouch listeners for an ImageView.I have referred several links in stackoverflow but couldn't achieve my goal. how can I implement this.I've referred

how to use both Ontouch and Onclick for an ImageButton?

Android onTouch with onClick and onLongClick

Can't handle both click and touch events simultaneously

OnTouch/OnClick listeners. Use both?

actually my context was when i click on imageview it will goto other activity and when i swipe on it it has to liked by the user. any help.

4
  • why you want to use ontouch linstener? Commented Apr 7, 2014 at 12:23
  • to drag the imageview either left or right(left -meaning user Dislikes the item & right - user likes it) and for click action he has to move to other activity to check all the images of that product(item). Commented Apr 7, 2014 at 12:28
  • if you use the onTouchListener you can add a gesture detector and override onTap, onLongTap, onFling and the others Commented Apr 7, 2014 at 12:28
  • i have used gesturedetector, in that implemented onFling method(swipe right and swipe left used) it is working but onclick is not working at this point... Commented Apr 7, 2014 at 12:35

3 Answers 3

3
@Override public boolean onTouch(View v, MotionEvent event) { System.out.println("ajay +event.getAction()" + event.getAction()); switch (event.getAction()) { case MotionEvent.ACTION_MOVE: layOutParams.x = initialX + (int) (event.getRawX() - initialTouchX); layOutParams.y = initialY + (int) (event.getRawY() - initialTouchY); break; case MotionEvent.ACTION_DOWN: initialX = layOutParams.x; initialY = layOutParams.y; initialTouchX = event.getRawX(); initialTouchY = event.getRawY(); if (initialTouchX == event.getRawX() && initialTouchY == event.getRawY()) { return false;// to handle Click } break; case MotionEvent.ACTION_UP: if (initialTouchX == event.getRawX() && initialTouchY == event.getRawY()) { return false;// to handle Click } break; } return true; } }; 

onClick Implemenation

overLayview.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub System.out.println("ajay onClick Lisner"); } }); 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you ajay....I have asked this exactly one year back..(April 7,2014)..I had done this earlier.
1

You dont need to implement both behaviors, define MIN_SWIPE_DISTANCE and check at ACTION_UP in onTouch, so you will have something like this.

pseudo

private static final int MIN_SWIPE_DISTANCE=300; private int oldX,newX; public boolean onTouch(View v, MotionEvent event) { if(event.getAction()==MotionEvent.ACTION_DOWN) // touch down { oldX = event.getX(); //get touch position } else if(event.getAction()==MotionEvent.ACTION_UP) // touch up { newX = event.getX(); //get last touch position if (Math.abs( newX-oldX )> MIN_SWIPE_DISTANCE) //its a swipe else // its a click } } 

1 Comment

what is oldX and newX? i mean for different screens how can i give different values for different screens? any way Thanks
0

I think you will need to decide if the gesture is a swipe or not and act acrodingly

see here how to achieve it https://stackoverflow.com/a/938657/1393632

1 Comment

it is fine for gesture meaning swipe(Touch)...but click action not working at this situation.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.