Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - ImageView On Click

Tags:

android

touch

Im trying to trace a click when my image view is clicked, but I cannot seem to pick up the touch, any ideas?

imgView.setOnClickListener(new View.OnClickListener(){
    public void onClick(View v) {
        Log.v(TAG, " click");
    }
});
like image 717
JeffLemon Avatar asked Sep 06 '11 21:09

JeffLemon


People also ask

Is ImageView clickable in Android?

Using clickable imagesYou can turn any View , such as an ImageView , into a button by adding the android:onClick attribute in the XML layout. The image for the ImageView must already be stored in the drawable folder of your project.

How to make ImageView clickable in android?

To make a View clickable so that users can tap (or click) it, add the android:onClick attribute in the XML layout and specify the click handler. For example, you can make an ImageView act like a simple Button by adding android:onClick to the ImageView . In this task you make the images in your layout clickable.


2 Answers

Images normally aren't clickable, therefore I would suggest that you put the definition

android:clickable="true"
android:focusable="true"

in your layout.xml to the imageView.

If this is not helping, please edit+update your question and show us the part of your layout, where you define the image to have a look at it.

EDIT: Tried it, and your code worked with me too - even with the upper case id name. Can you please have a close look at your LogCat? Mine sometimes doesn't really update, as long as I don't choose the device again.

To do so in Eclipse, go to the View "Devices" (or show it first via "Window") and click once on your device / virtual device.

If you still don't find your log entry in the LogCat-View, try to create a filter (via the green plus and giving it the String you defined with TAG as "by Log Tag").

Have a look at android developers > Using DDMS at the section "Using LogCat"

like image 176
sunadorer Avatar answered Oct 11 '22 13:10

sunadorer


You forgot to override the onClick method. This works for me and should do for you as well :-)

imgView.setOnClickListener(new View.OnClickListener() {
   //@Override
   public void onClick(View v) {
      Log.v(TAG, " click");         
   }        
});

Here's to get the X and Y coordinates:

imgView.setOnTouchListener(new OnTouchListener() { 
   @Override
   public boolean onTouch(View v, MotionEvent event) {
      // X = event.getX()
      // Y = event.getY()
      return false;
   }            
});
like image 31
Michell Bak Avatar answered Oct 11 '22 12:10

Michell Bak