17

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):

    ImageView btnNew = (ImageView) findViewById(R.id.newbutton);
    btnNew.setOnClickListener(new View.OnClickListener() {

          @Override
          public void onClick(View v) {

            // do stuff
          }

        });      

and here's the part from xml:

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

When running this code, and clicking the image i get this error:

01-24 19:14:09.534: ERROR/AndroidRuntime(1461): java.lang.IllegalStateException: Could not find a method clickImage(View) in the activity

HERE'S THE SOLUTION:

The XML:

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

The code :

    public void clickNew(View v)
{
    Toast.makeText(this, "Show some text on the screen.", Toast.LENGTH_LONG).show();
}
user484146
  • 301
  • 1
  • 7
  • 14

5 Answers5

27

As other said: make this an ImageButton and define its onClick attribute

<ImageButton
     android:layout_height="wrap_content"
     android:layout_width="wrap_content"
     android:layout_gravity="left"
     android:onClick="scrollToTop"
     android:src="@drawable/to_top_button"
/>

The image is here encoded in a file res/drawable/to_top_button.png. If the user clicks on the button, the method scrollToTop() is called. This method needs to be declared in the class that sets the Layout with the ImageButton as its content layout.

public void scrollToTop(View v) {
    ...
}

Defining the OnClick handler this way saves you a lot of typing and also prevents the need to anonymous inner classes, which is beneficial for the memory footprint.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
Heiko Rupp
  • 30,426
  • 13
  • 82
  • 119
  • 9
    Ok, this one works, but now my image is inside a button.. So it's braking my layout. To fix this i added the line android:background="@null" into the xml and all works perfect! – user484146 Jan 24 '11 at 19:09
  • The correct background to use is `android:background="?android:selectableItemBackground"`. – George Hilliard Oct 27 '14 at 18:44
3

Does an ImageButton do what you want?

The error message you get implies that you do not have a method in your activity that matches your onClick handler.

You should have something like clickImage(View view) in your activity with the click handling implementation.

Cheryl Simon
  • 46,552
  • 15
  • 93
  • 82
  • I tried with clickImage(View view) but doesn't work, cause of the OnClickListner.. If an imagebutton is transparent and will show the image only (no button), i can use it. – user484146 Jan 24 '11 at 18:31
  • Oh, if you are explicitly setting an on click listener in onCreate, you should not have a onClick attribute in your layout. – Cheryl Simon Jan 24 '11 at 18:32
  • I have tried removing android:onClick="clickImage" from the code, and the result is that nothing at all happens when clicking. – user484146 Jan 24 '11 at 18:48
1

You could just use the ImageButton class... http://developer.android.com/reference/android/widget/ImageButton.html

Jems
  • 11,560
  • 1
  • 29
  • 35
1

Use a ImageButton ;)

Rainbowbreeze
  • 1,503
  • 1
  • 9
  • 14
0

You've set the onclick method to call "clickImage" when the image is clicked in your XML, but you haven't created a clickImage method in your code. You shouldn't need to set the onclick listener at all. Just implement the method from your XML and you should be set.

Keith
  • 21
  • 1