Can you create different actions for clicking on the same ImageView but different parts? Say I have an ImageView and I want for it to act differently if I click on the top of the ImageView and differently if I click on the middle part of the ImageView . Can I achieve this in android
-
1Look at this https://stackoverflow.com/a/4023317/6055194 – Eugene Babich Sep 17 '18 at 07:52
-
possible duplicate of https://stackoverflow.com/questions/16670774/clickable-area-of-image – Muhammad Zahab Sep 17 '18 at 07:52
-
1Possible duplicate of [clickable area of image](https://stackoverflow.com/questions/16670774/clickable-area-of-image) – cakan Sep 17 '18 at 07:53
3 Answers
The best way is to add an OnTouchListener
to the view and handle click event by yourself related to the touch coordinate.
But if you need an easy way which I don't suggest because it can lead to performance issue you can add transparent views on top of your ImageView
and add different OnClickListener
for them.

- 1,497
- 1
- 13
- 18
if you have fixed sections in your imageview you can use a frame layout and add 3(for example) transparent views on your imageview and then set click listeners on those views. but if you have dynamic sections you can handle it by combining onTouchListener and onClickListener, for example :
// just store the touch X,Y coordinates
View.OnTouchListener touchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// save the X,Y coordinates
if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
lastTouchDownXY[0] = event.getX();
lastTouchDownXY[1] = event.getY();
}
// let the touch event pass on to whoever needs it
return false;
}
};
View.OnClickListener clickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
// retrieve the stored coordinates
float x = lastTouchDownXY[0];
float y = lastTouchDownXY[1];
// use the coordinates for whatever or check the clickable areas
Log.i("TAG", "onLongClick: x = " + x + ", y = " + y);
}
};

- 2,227
- 2
- 16
- 18
I think the best way for this is that you declare a frameLayout as parent view then you can declare views on your imageView then clicking each view will perform what you want.

- 1,106
- 9
- 18