Skip to content Skip to sidebar Skip to footer

Onclicklistener In Canvas

Im developing an application which having Images as a Index on selection of particular image that activity will begin but I dont know how to set onClickListener or onTouchListener

Solution 1:

try something like this:

public boolean onTouch(View v, MotionEvent event) {
   if((event.getX(0)>=160) && 
      (event.getY(0)>=100) && 
     ( event.getX(0)<=160+BOOK_IMG_WIDTH) && 
      (event.getY(0)<=100+BOOK_IMG_HEIGHT))
      {
          //book selected
      }
   returntrue;
}

Solution 2:

Use ImageView instead of bitmap and add it to your layout:

book = newImageView(context);
book.setImageResource(R.drawable.book);
book.setOnTouchListener(newView.OnTouchListener() {
        publicbooleanonTouch(View v, MotionEvent event) {
            /*...*/
        }
    };);

Solution 3:

Save the images' coords in an ArrayList. Set OnClickListener on the context, get the point coords that has been clicked, find the image in the arraylist and do something :)

Post a Comment for "Onclicklistener In Canvas"