Android - Set a border around an ImageView

Andy Res picture Andy Res · Sep 24, 2012 · Viewed 78.1k times · Source

I have a cell with a fixed width and height, let it be 100x100px. Inside that cell I want to display an ImageView with a border around.
My first idea was to put a background resource to the ImageView, and add a padding of 1dp to create the border effect:

<LinearLayout
        android:layout_width="100dp"
        android:layout_height="100dp" >

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/image_border"
        android:padding="1dp"
        android:src="@drawable/test_image" />

</LinearLayout>

Apparently this should work, but it doesn't, or at least not as expected.
The problem is that the ImageView background fills the entire 100x100px cell's space, thus, if the image's width is less than 100px, then the top and bottom border will appear larger.

Notice the yellow border, I need it to be exactly 1px around the ImageView:

enter image description here

Any help, idea, suggestion of any kind is much, much appreciated.

Answer

Christine picture Christine · Sep 24, 2012

If you put the padding=1 and the background color in the LinearLayout, you'll have a 1px yellow border.