I have this picture (this is one picture, not four). I want to make it an imagebutton. But, I want to divide it to four parts, so when I will click on Paul it will open for me new intent about Paul, and so on for John, Ringo and George.
Could I make it? how?
Asked
Active
Viewed 32 times
-1

Hilla Barnea
- 27
- 2
-
1This might help: https://stackoverflow.com/questions/1967039/onclicklistener-x-y-location-of-event – user2740650 May 30 '20 at 16:16
-
Is this a single image or a separate one for each? – Ankit Aman May 30 '20 at 16:32
1 Answers
2
You can use View.OnTouchListener
to get coordinates from the click that occurred.
View.OnTouchListener onTouchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_UP:
float x = event.getX();
int width = v.getWidth();
int oneFourthWidth = width / 4;
if (x <= oneFourthWidth) {
// John Lennon
} else if (x > oneFourthWidth && x <= oneFourthWidth * 2) {
// Paul McCartney
} else if (x > oneFourthWidth * 2 && x <= oneFourthWidth * 3) {
// George Harrison
} else if (x > oneFourthWidth * 3) {
// Ringo Starr
}
break;
}
return true;
}
};
yourView.setOnTouchListener(onTouchListener);
The calculation of the exact coordinates of each image is up to each specific case.

Jenea Vranceanu
- 4,530
- 2
- 18
- 34