-1

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

Sniffer
  • 1,495
  • 2
  • 14
  • 30

3 Answers3

0

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.

Shynline
  • 1,497
  • 1
  • 13
  • 18
0

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);
        }
    };
Reza.Abedini
  • 2,227
  • 2
  • 16
  • 18
0

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.

mmdreza baqalpour
  • 1,106
  • 9
  • 18