-1

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? enter image description here

1 Answers1

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