19

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");
    }
});
JeffLemon
  • 281
  • 3
  • 6
  • 14

3 Answers3

48

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"

sunadorer
  • 3,855
  • 34
  • 42
  • 1
    I'm perfectly able to use the suggested code without setting `android:clickable`. – Michell Bak Sep 06 '11 at 22:23
  • 4
    setonclicklistener makes a view clickable if it doesn't have that as a default. – Yashwanth Kumar Sep 06 '11 at 22:33
  • 1
    ok good to know. guess I got confused - you would need that if you want to use the android:onClick and have a link to a method there. well then I hope we get an update on the second part of my answer. – sunadorer Sep 06 '11 at 22:38
  • 1
    @Machado good point. This attribute didn't exist when I first gave this answer. Added it now in case others come across this post – sunadorer Jan 06 '20 at 16:01
7

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;
   }            
});
Michell Bak
  • 13,182
  • 11
  • 64
  • 121
  • "Annotation type used to mark methods that override a method declaration in a superclass. Compilers produce an error if a method annotated with @Override does not actually override a method in a superclass." Source: http://developer.android.com/reference/java/lang/Override.html – Michell Bak Sep 06 '11 at 22:00
  • I get the error: The method onClick(View) of type new View.OnClickListener(){} must override a superclass method – JeffLemon Sep 06 '11 at 22:01
  • Could you please post your entire code, or at least everything that's relevant to the ImageView? – Michell Bak Sep 06 '11 at 22:02
  • protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); img=(ImageView)findViewById(R.id.ImageView01); // Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.refresh); img.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.v(TAG, " click"); } }); – JeffLemon Sep 06 '11 at 22:08
  • 2
    Isn't the @Override annotation just a marker that can but doesn't need to be used? The overriding takes place anyway - no matter if its there or not. – sunadorer Sep 06 '11 at 22:11
  • Please see my updated answer. This seems to be a common issue if you're using a JRE below 1.6.0. It seems that all you need to do is comment out the @Override annotation. – Michell Bak Sep 06 '11 at 22:12
  • Got this working now, thanks. How can I get the x and y pos of the click? – JeffLemon Sep 06 '11 at 22:56
  • For that, you'll need an OnTouchListener. Please see my updated answer. – Michell Bak Sep 06 '11 at 23:16
3

1) First make your image view clickable by adding-> android:clickable="true" .
2) Then go to your java file and add declare a variable for example-> private static ImageView imgview;
3) Then add this functionality:

public void OnclickButtonListener() {

    imgview =  findViewById(R.id.imageView3);

    imgview.setOnClickListener(new View.OnClickListener() {
                                      @Override
                                      public void onClick(View v) {
                                          Intent intent=new Intent(timetable.this,MapsActivity.class);
                                          startActivity(intent);
                                      }
    });
}

and call this function in the constructor. Hopefully this would work.

Bradmage
  • 1,233
  • 1
  • 15
  • 41
Hamza Tayyab
  • 79
  • 2
  • 3