Suresh Balaraman
Suresh Balaraman

Reputation: 175

How to add touch event for drawRect in Android

I have created the rectangle using drawRect() in Canvas in onDraw() method

public void onDraw(Canvas canvas) 
{   

    canvas.drawRect(20, 30, 10, 10, Color.RED);

}

How to get the event for this rectangle?

Upvotes: 0

Views: 1028

Answers (2)

gone
gone

Reputation: 823

In your View's onTouchEvent method, which you should override to achieve this, you'll get a MotionEvent, then you can access the position where your touch event occurs like event.getX() and event.getY().Then , check if your Rect.contains(event.getX(),event.getY());

Upvotes: 1

Erik
Erik

Reputation: 3636

I don't think you can get an event for something you draw like that, unless you manually check if a click on the main canvas falls within the bounds of the rectangle.

Upvotes: 0

Related Questions