Guy
Guy

Reputation: 6522

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?

Upvotes: 0

Views: 579

Answers (2)

Bozic Nebojsa
Bozic Nebojsa

Reputation: 3706

First you need to know is that 9-patch is image as any other, regarding layout placing it. The difference is in way 9- patch image scale.

In this situation, try to set layout_width to "fill_parrent" or "match_ parent"

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

Upvotes: 0

James McCracken
James McCracken

Reputation: 15766

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

Upvotes: 1

Related Questions