Ragu
Ragu

Reputation: 21

how to use Imageview as Clickable?

How can I use imageview as a button(clickable) in my project? As of now i am using button object but i want to display an image and not a button.

Thanks in advance

android

Upvotes: 0

Views: 275

Answers (5)

Atul O Holic
Atul O Holic

Reputation: 6792

ImageView do listen to click events i.e. OnClickListener. So if you plan to use them as clickable you do not need any additional set up for it.

Simply create an ImageView,

<ImageView
    android:id="@+id/imgClickable"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="your image"
/>

Then get its reference in your Java code,

 ImageView clikableImage = (ImageView) findViewById(R.id.imgClickable);
 clikableImage .setOnClickListener(this); //This make it clickable

And finally handle its onClick event by overriding

@override
public void onClick(View v){
    //Your action
}

Upvotes: 2

4this
4this

Reputation: 759

Wll how about using ImageButton.

Uoc can find it when you open a layout xml file, and at the Grapichal layout - You would see ImageButton unter the Images&Media options menu.

Upvotes: 0

ucsunil
ucsunil

Reputation: 7494

You can include the ImageView wherever you want on your layout and implement the OnClickListener interface to listen for click events. It would be something like:

ImageView image = (ImageView) findViewById(R.id.your_id);
imager.setOnClickListener(new View.OnClickListener() {
    // Do something
});

Upvotes: 0

Embattled Swag
Embattled Swag

Reputation: 1469

It's pretty simple. Either in your layout when you define the ImageView you can add android:onClick="someFunction" and which calls someFunction whenever you click the image (make sure you define it in your code) or you can programmatically just add an onClickListener to the ImageView.

Upvotes: 0

VipulKumar
VipulKumar

Reputation: 2395

You can use android:clickable="true" attribute. And then you can use onClickListener to implement a listener.

But if you want to use an image instead of button you should consider ImageButton. http://developer.android.com/reference/android/widget/ImageButton.html

Upvotes: 0

Related Questions