• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

touch event on image made on canvas


Jun 17, 2010
i have have made an image on canvas in the following way

Bitmap bmp = BitmapFactory.decodeResource(getResources(),R.drawable.pushpin);
            canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 50, null);

now i want to listen touch event on the image , how can i do that

thanks and regards
This is the wrong forum for that request, but I'll answer it:

If you are using a canvas from a SurfaceView, for example, you'll need to override the method "onTouchEvent(TouchEvent event)"

public boolean onTouchEvent(final MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN: {
// Code on finger down
float posX = ev.getX();
float posY = ev.getY()'

// 80x80 pixel square located 20 pixels left, 20 pixels top.
float x1 = 20, x2 = 100, y1 = 20, y2 = 100;

if ((posX >= x1 && posX <= x2) && (posY >= y1 && posY <= y2)) {
// we are in the square
} else {
// we are somewhere else on the canvas

case MotionEvent.ACTION_UP: {
// Code on finger up
case MotionEvent.ACTION_MOVE: {
// Code on finger move

Hope that helps,

@@Moderator, could you move this?
Upvote 0
You can't get a touch event on the bitmap because the bitmap doesn't support any kind of touch handlers, it is simply a picture. The canvas is also another bitmap, it just supports an API that allows you to draw onto it with ease. The canvas and bitmap do not support being touched because that is out of the scope of what they do.

Your other alternative is to make a layout and add an ImageButton or ImageView to it, then you "setContentView(R.id.my_layout_file_name)" on the Activity. Then you can register a touch handler by calling "setOnClickListener()" on the ImageButton or ImageView.

Start with the Android documentation: User Interface | Android Developers

If you're still having problems, let me know.
Upvote 0


We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.