0

I have a simple ImageView:

        <ImageView
            android:id="@+id/something"
            android:layout_height="500dp"
            android:layout_gravity="center"
            android:layout_width="150dp"                
            android:scaleType="centerCrop"
            android:padding="3dp"
            android:src="@drawable/myPic">
        </ImageView>

How can I set its border width and colour in a) xml b) *.java file?

Steven
  • 177
  • 1
  • 1
  • 8
  • 2
    Possible duplicate of [Border for an Image view in Android?](http://stackoverflow.com/questions/3263611/border-for-an-image-view-in-android) – Linh Feb 18 '16 at 10:13

3 Answers3

0

You can do it in several ways:

Just set Padding and change background color then the padding area will be shown as border.

<ImageView
     android:id="@+id/something"
     android:layout_height="500dp"
     android:layout_gravity="center"
     android:layout_width="150dp"                
     android:scaleType="centerCrop"
   android:background="#ff0000"
     android:padding="3dp"
     android:src="@drawable/myPic">
   </ImageView>

Or create a xml file in your drawable folder named border_back.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF" />
<stroke android:width="1dp" android:color="#000" />
<padding android:left="3dp" android:top="3dp" android:right="3dp"
    android:bottom="3dp" />
</shape>

and now use as background like this:

<ImageView
     android:id="@+id/something"
     android:layout_height="500dp"
     android:layout_gravity="center"
     android:layout_width="150dp"                
     android:scaleType="centerCrop"
   android:background="@drawable/border_back"
     android:padding="3dp"
     android:src="@drawable/myPic">
   </ImageView>
Asim Roy
  • 9,915
  • 4
  • 28
  • 40
0

put this xml in your drawable folder with name img_view.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"><solid android:color="#FFF" /><stroke android:width="2dp" android:color="#000" /><padding android:left="2dp" android:top="2dp" android:right="2dp"    android:bottom="2dp" /></shape>

And in your imageview Property set Background

android:src="@drawable/img_view"
Vishal Thakkar
  • 2,117
  • 2
  • 16
  • 33
0

Here is an idea. Do one thing.

1. Just take your imageView inside a linear layout.

2. Give the background color to the linear layout that you want to use for imageview border. and then provide the padding for your imageview.

You would see the result ! :) Let me know if this works!

Vivek Bhardwaj
  • 530
  • 5
  • 16