Android ImageView's onClickListener does not work

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" />

Ok,

I managed to solve this tricky issue. The thing was like I was using FrameLayout. Don't know why but it came to my mind that may be the icon would be getting hidden behind some other view.

I tried putting the icon at the end of my layout and now I am able to see the Toast as well as the Log.

Thank you everybody for taking time to solve the issue.. Was surely tricky..


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

Tags:

Android