JeffLemon
JeffLemon

Reputation: 273

Android - ImageView On Click

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");
    }
});

Upvotes: 19

Views: 84469

Answers (4)

user26374192
user26374192

Reputation: 1

notificationLayout.setOnClickPendingIntent(R.id.notification_play_pause, playPausePendingIntent)
notificationLayout.setOnClickPendingIntent(R.id.notification_next, nextPendingIntent)
notificationLayout.setOnClickPendingIntent(R.id.notification_previous, previousPendingIntent)

Upvotes: -1

sunadorer
sunadorer

Reputation: 3892

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"

Upvotes: 48

Hamza Tayyab
Hamza Tayyab

Reputation: 79

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.

Upvotes: 3

Michell Bak
Michell Bak

Reputation: 13242

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;
   }            
});

Upvotes: 7

Related Questions