Search code examples
androidxmleclipsenine-patch

How do I use a 9-patch?


I have a 9-patch image and what I want to do is to use it to "seperate" the adMob ad from the app. I want to draw a light blue line exactly where the ad is.

This is the 9-patch:

enter image description here

I added it to layout as ImageView and this is the XML code:

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/adView"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:src="@drawable/ab_transparent_example" />

And this is the result:

enter image description here

But what I want to get is the 9-patch to take the size of the screen, to make a full blue line from left to right of the screen. I've never worked with 9-patch before, what am I doing wrong?


Solution

  • The diagram below is a pretty good visual explanation of what a 9-patch is. As for your specific problem, you should set the 9-patch as the background to the image.

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/adView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:background="@drawable/ab_transparent_example" />
    

    9-patch Diagram