Search code examples
androidimageviewnine-patch

Android: Using 9patch to create a frame around an imageview


I've been trying to create a frame around my imageview using a picture of a wooden frame. I turned the woodenframe picture into a 9patch and its still not wrapping around the imageview.

<RelativeLayout
    android:id="@+id/ChosenPic"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_weight="3"
    android:orientation="vertical"
    android:padding="10dp" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:baselineAlignBottom="true"
        android:src="@drawable/ic_launcher" />

    <ImageView
        android:id="@+id/ImageView02"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="fill_vertical|fill_horizontal"
        android:scaleType="fitXY"
        android:src="@drawable/woodenframe" >
    </ImageView>
</RelativeLayout>

enter image description here

So this is the ninepatch and i want it to fit around a picture of my choosing no matter the size.

EDIT:

This is what i want.


Solution

  • You have two options. I recommended the first one.

    First option:
    Put each image view in a separate LinearLayout and apply the 9patch to the LinearLayout it self not the image view

    Second option:
    Use or set the background resource of the image view in Java to the 9patch resource or the background attr in xml to the 9 patch resource and the src to the image it self.

    Note that it better to use a Layer-List and add a combination of two shapes or whatever rather than using a 9patch resource.

    Reference:
    https://developer.android.com/guide/topics/resources/drawable-resource.html#LayerList