Note that there are some explanatory texts on larger screens.

plurals
  1. POClickable image - android
    text
    copied!<p>How do i make a image clickable? I have tried some ways, but without success. Here's the last code i tried (it's clickable but gets error):</p> <pre><code> ImageView btnNew = (ImageView) findViewById(R.id.newbutton); btnNew.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // do stuff } }); </code></pre> <p>and here's the part from xml:</p> <pre><code> &lt;ImageView android:src="@drawable/tbnewbutton" android:text="@string/hello" android:layout_width="wrap_content" android:layout_alignParentRight="true" android:id="@+id/newbutton" android:clickable="true" android:onClick="clickImage" android:layout_height="wrap_content" android:layout_alignParentTop="true" /&gt; </code></pre> <p>When running this code, and clicking the image i get this error:</p> <blockquote> <p>01-24 19:14:09.534: ERROR/AndroidRuntime(1461): java.lang.IllegalStateException: Could not find a method clickImage(View) in the activity</p> </blockquote> <p><strong>HERE'S THE SOLUTION:</strong></p> <p>The XML:</p> <pre><code> &lt;ImageButton android:src="@drawable/tbnewbutton" android:text="@string/hello" android:layout_width="wrap_content" android:layout_alignParentRight="true" android:id="@+id/newbutton" android:clickable="true" android:onClick="clickNew" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:background="@null" /&gt; </code></pre> <p>The code :</p> <pre><code> public void clickNew(View v) { Toast.makeText(this, "Show some text on the screen.", Toast.LENGTH_LONG).show(); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload