Reputation: 21
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
Upvotes: 0
Views: 275
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
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
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
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
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