Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expand clickable area of an ImageView by using padding?

Tags:

I have an ImageView, and I want it to be clickable. The image itself is only 32x32 pixels, but I want the clickable region to be larger so it's easier to hit. I was hoping I could set the layout margin values to increase the size of the imageview's clickable area:

<ImageView    layout_width="32dip"    layout_height="32dip"    layout_margin="20dip" /> 

That doesn't work, what I could do is just recreate the image resource to have a larger transparent region around it. That's a bit annoying because it's hard to tweak if I ever need to change the clickable region. It's not just a single png either, it's a statelistdrawable so I have to resize 3 different pngs if I ever need to tweak the clickable area.

Anything else I can do?

Thanks

like image 855
user291701 Avatar asked Nov 05 '10 16:11

user291701


People also ask

Can ImageView be clickable?

Using clickable imagesYou can turn any View , such as an ImageView , into a button by adding the android:onClick attribute in the XML layout. The image for the ImageView must already be stored in the drawable folder of your project.

How do you increase the clickable area of a button Android?

Remove margin, use padding around button. Surround the button with a say a LinearLayout that has the padding round the button. Add the same onclick to the LinearLayout as the Button.

What is SRC in ImageView?

android:scaleType. Controls how the image should be resized or moved to match the size of this ImageView. android:src. Sets a drawable as the content of this ImageView.

What is difference between ImageView and ImageButton?

ImageButton has the same property as ImageView . Only one feature is extra, which is, images set through ImageButton are clickable, and actions can be attached with them upon clicking. Just like a button, the setOnClickListener() can be used to set an event listener for click event on this.


1 Answers

Use padding. layout margins are used if for inserting space outside the boundary of the view.

for equal padding on all sides

  <ImageView        android:padding="20dip" /> 

or to set the padding on each side

<ImageView      android:paddingLeft="10dip"      android:paddingRight="15dip"      android:paddingTop="23dip"      android:paddingBottom="12dip" /> 

Hope that helps !

like image 66
Saimon Avatar answered Sep 19 '22 13:09

Saimon