0

I am a beginner developer. I would like to build an app, but I am facing a problem.

Image

As the image shows, I have a button. If the user clicks on the top side of this button I would like to change the color of just the top line. Also on the other sides, just the side that the user clicked. If user clicked for example on the top side then the right side, I would like to change the color of both side which is shown by the picture.

Any ideas how to do this? The main problem for me, is I can not detect which area the user clicks.

buczek
  • 2,011
  • 7
  • 29
  • 40
Davinho
  • 107
  • 1
  • 9

1 Answers1

1

You can use touch detection like described here: https://developer.android.com/training/gestures/detector.html

@Override
public boolean onSingleTapConfirmed(MotionEvent event) {
    float clickedX = event.getX();
    float clickedY = event.getY();

    float btnTop = obtainTop(yourBtn);
    float btnBot = obtainBot(yourBtn);
    float btnLeft = obtainLeft(yourBtn);
    float btnRight = obtainRight(yourBtn);

    // detect where the button was clicked
    // change button how you need

    return true;
}

For obtain x y coordinates of View you can find something here Getting View's coordinates relative to the root layout

Community
  • 1
  • 1
Nik Kober
  • 868
  • 11
  • 16