Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ImageView's onClickListener does not work

Tags:

android

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.

like image 762
Mahendra Liya Avatar asked May 21 '11 18:05

Mahendra Liya


1 Answers

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" /> 
like image 118
Houcine Avatar answered Sep 28 '22 09:09

Houcine