[android] Android ImageView's onClickListener does not work

I have an ImageView for which I wanted to implement the onClickListener. But when I click on the image, nothing happens. Event the Logcat does not show any errors.

Following is my import statement:

import android.view.View.OnClickListener;

Following is my layout code for the image:

<ImageView android:id="@+id/favorite_icon" 
    android:src="@drawable/small_star"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="top|right" android:paddingTop="63sp"
    android:paddingRight="2sp"  />

Following is the code in my activity which defines the event handler for onClickListener:

ImageView imgFavorite = (ImageView) findViewById(R.id.favorite_icon);
imgFavorite.setClickable(true);
imgFavorite.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i(SystemSettings.APP_TAG + " : " + HomeActivity.class.getName(), "Entered onClick method");
                Toast.makeText(v.getContext(),
                        "The favorite list would appear on clicking this icon",
                        Toast.LENGTH_LONG).show();
            }
        });

Am I missing something. Any help would be appreciated.

Thanks in advance.

This question is related to android

The answer is


Actually I just used imgView.bringToFront(); and it helped.


Had the same problem, thanks for the Framelayout tip! I was using two overlapped images in a framelayout (the one at top was an alpha mask, to give the effect of soft borders)

I set in the xml android:clickable="true" for the image I wanted to launch the onClickListener, and android:clickable="false" to the alpha mask.


For inner classes to pass the reference of activity, I usually create a reference of activity in onCreate() and make it as a member. As inner classes have access to members of parent class you can get hold of your activity and thus its context:

class MyClass extends Activity{
    private Activity thisActivity;
    @Override
protected void onCreate(Bundle savedInstanceState) {
            thisActivity = this;
    }
}

Hope that helps.


I defined an OnClickListener for ImageView as an OnClickListener for a button and it seems to be working fine. Here's what I had. Please try and let us know if it doesn't work.

final ImageView imageview1 = (ImageView) findViewById(R.id.imageView1);

imageview1.setOnClickListener(new Button.OnClickListener() {
    @Override
    public void onClick(View v) {
        try {
             Log.i("MyTag","Image button is pressed, visible in LogCat");;
        } catch (Exception e) {
            Log.e(TAG, e.toString());
        }
    }
});

can you Try this and tell me what happens ?? :

JAVA :

ImageView imgFavorite = (ImageView) findViewById(R.id.favorite_icon);
imgFavorite.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Toast.makeText(YourActivityName.this,
                "The favorite list would appear on clicking this icon",
                Toast.LENGTH_LONG).show();
    }
});

or you should add this :

imgFavorite.setClickable(true); 

KOTLIN :

imgFavorite.setOnClickListener { view ->
    Toast.makeText(this@YourActivityName, R.string.toast_favorite_list_would_appear, Toast.LENGTH_LONG).show()
}
// either you make your imageView clickable programmatically
imgFavorite.clickable = true

// or via xml on your layout file
<ImageView .... android:clickable="true" />

Check if other view has the property match_parent or fill_parent ,those properties may cover your ImageView which has shown in your RelativeLayout.By the way,the accepted answer does not work in my case.


Well my solution was another one, or let's say, the bug was:

I simply had another imageview with the same id in another <included /> twice-encapsuled xml layout, which was found before the one that i wanted to react on the click. Solution: Give one of them another id...


Same Silly thing happed with me.

I just copied one activity and pasted. Defined in Manifest.

Open from MainActivity.java but I was forgot that Copied Activity is getting some params in bundle and If I don't pass any params, just finished.

So My Activity is getting started but finished at same moment.

I had written Toast and found this silly mistake. :P


As a beginner to android development, my issue wasn't any of these solutions. I had several problems.

Firstly, I didn't realise that you had to use the bug symbol to use the debugger to hit breakpoints.

Then I was silently getting an error in my on click listener, I was trying to pass something to a method, but the method required a float value.

However in the ide, the method was shown as unused. It wasn't until I cast the value that the method was shown in the correct colour and wasn't marked as unused anymore.

Not sure why I didn't get an error from trying to use a method with the wrong type ?

Also I had to add the android:debuggable="true" to my manifest to get debugging to hit breakpoints.


Please Try this one.

ImageView imageview1 = findViewById(R.id.imageView1);
imageview1.setOnClickListener(new View.OnClickListener() {

        @Override

        public void onClick(View v) {

            Toast.makeText(YourActivity.this, "Here is your Text",Toast.LENGTH_SHORT).show();

        }

    });

Try by passing the context instead of the application context (You can also add a log statement to check if the onClick method is ever run) :

imgFavorite.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d("== My activity ===","OnClick is called");
            Toast.makeText(v.getContext(), // <- Line changed
                    "The favorite list would appear on clicking this icon",
                    Toast.LENGTH_LONG).show();
        }
    });

Most possible cause of such issues can be some invisible view is on the top of view being clicked, which prevents the click event to trigger. So recheck your xml layout properly before searching anything vigorously online.

PS :- In my case, a recyclerView was totally covering the layout and hence clicking on imageView was restricted.


Add android:onClick="clickEvent" to your image view.

<ImageView android:id="@+id/favorite_icon" 
    android:src="@drawable/small_star"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="top|right" android:paddingTop="63sp"
    android:paddingRight="2sp"
    android:onClick="clickEvent"  />

In your activity you can create a method with the same name (clickEvent(View v)), and that's it! You can see the log and the toast text too.

public void clickEvent(View v)
  {
    Log.i(SystemSettings.APP_TAG + " : " + HomeActivity.class.getName(), "Entered onClick method");
    Toast.makeText(v.getContext(),
            "The favorite list would appear on clicking this icon",
            Toast.LENGTH_LONG).show();
  }

The same thing is happening for me.

The reason is: I have used a list view with margin Top so the data is starting from the bottom of the image, but the actual list view is overlapping on the image which is not visible. So even if we click on the image, the action is not performed. To fix this, I have made the list view start from the below the image so that it is not overlapping on the image itself.