[ACCEPTED]-Expand clickable area of an ImageView by using padding?-android

Accepted answer
Score: 36

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

for equal padding on all sides

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

or 2 to set the padding on each side

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

Hope that 1 helps !

Score: 12

Instead of resizing the image (Peter Knego's 11 answer) or increasing padding (Saimon's 10 answer) I suggest to set the minWidth and minHeight attributes 9 in combination with scaleType="center":

<ImageView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:minWidth="40dp"
  android:minHeight="40dp"
  android:scaleType="center"
  android:src="@drawable/your_image" />

That makes sure that 8 small images have at least a size of 40x40dp 7 while the image is not up-scaled if it's 6 smaller. If one day the image is going to 5 be replaced with another image larger than 4 minWidth and minHeight it will not grow larger than 40x40dp 3 as it gets downscaled. Thus a minimum clickable 2 dimension of 40x40dp is always guaranted 1 and the image is always displayed nicely.

Score: 9

Suggestions (never tried it myself):

  1. Create 5 ImageView as large as you want than put 4 image into it without scaling

    ImageView.setScaleType(ImageView.ScaleType.CENTER).
    
  2. Brute force 3 approach: create new larger png that has 2 original image centered and the rest of 1 it is transparent.

Score: 3

Use like below; where width/height is your 4 touch radius

<ImageView
  android:id="@+id/editProfile"
  android:layout_width="32dp"
  android:layout_height="32dp"
  android:padding="8dp"
  android:background="?attr/selectableItemBackgroundBorderless"
  android:src="@drawable/ic_edit"
  android:scaleType="fitCenter"
/>

In the above code, I wanted 3 my src size to be viewed as 24dp width/height. And 2 touch radius of 32dp. so I had used padding 1 of 8dp.

More Related questions