Vishal Vijay
Vishal Vijay

Reputation: 2528

How can I give a border to image view in Android?

In my new android app I need to give border to imageview by using something like .9.png.

The border size should change with respect to image which I have given to imageview and If possible I need to give a background image to imageview as I'm going to apply a transparent png image to imageview.

Should I create a new custom view for this?

Upvotes: 2

Views: 1908

Answers (1)

Heinrisch
Heinrisch

Reputation: 5935

I think you should create a custom view. Do a LinearLayout with the background as the border and have the ImageView centered inside the LinearLayout. Use 9-patch to get a correct stretch and create a content area so that the border is showing (use draw9patch in android SDK/Tools).

Example:

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/border">

    <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            />
    </LinearLayout>

Then 9-patch your border (border.9.png in this example). You can have the same stretching as content area.

Upvotes: 1

Related Questions