9

I have a list view structure, with Relative layout that uses alternating background images for odd/even elements. I'm trying to set the background drawable dynamically by calculating the position. It worked fine with the normal bitmap. But when I tried to use the ninepatch image it breaks the UI, all the elements get distorted. What am I doing wrong? Could it be how the ninepatch image is created or is there a different way to use a ninepatch image compared to a normal bitmap.

My List View XML goes like this

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content" android:id="@+id/id01"
       android:background="@drawable/my_9patch_bg_image">
       <ImageView  />
       <RelativeLayout>
         <ImageView  />
          <TextView  />
          <TextView  />
       </RelativeLayout>
</RelativeLayout>

May be the solution here might work for my problem. It's exact though I have to try it.

Community
  • 1
  • 1
Sharief Shaik
  • 1,064
  • 5
  • 12
  • 26
  • May be you have badly-formated 9-patch? – Dmitry Ryadnenko Feb 18 '11 at 11:41
  • I have tried different variations of the 9 patch image all of which give the preview I desired. But the content inside my first RelativeLayout is gone when that particular list view activity is up. Worked fine with normal bitmap image though. – Sharief Shaik Feb 18 '11 at 14:49
  • A screenshot of what's happening, an image of what you want to happen and the 9 patch might help people determine what the problem is. – ChrisJD Dec 12 '11 at 21:45
  • here http://stackoverflow.com/questions/3904852/android-layout-broken-with-9-patch-background?lq=1 is same problem with code exapmle :) – Pauli Mar 12 '15 at 21:40

4 Answers4

33

Since nobody answered you for a year now... I had the same issue you need to setpadding of zero (even if you don't need it).

Good luck.

zwebie
  • 2,349
  • 1
  • 27
  • 43
0

select all sides except corners as the stretching edges

Jeff Bootsholz
  • 2,971
  • 15
  • 70
  • 141
0

you can use this xml as background to show as cardview.

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape android:dither="true" android:shape="rectangle">
        <corners android:radius="2dp" />
        <solid android:color="#e2e2e2" />
    </shape>
</item>
<item android:bottom="2dp">
    <shape android:dither="true" android:shape="rectangle">
        <corners android:radius="2dp" />
        <solid android:color="@color/white" />
    </shape>
</item>

Hardik Vasani
  • 876
  • 1
  • 8
  • 14
0

You can use nine patch in the same way you use a normal drawable. I assume you have a problem with the 9 patch itself. In the /tools directory of your android sdk you have the draw 9 patch tool. It will help you to correct your 9 patch drawable and to preview it for different sizes of your view.

Daniel
  • 11
  • I tried fixing the image with the draw9patch tool. Tried different variations but didn't get it to work. What should be the ideal way to draw if the image is supposed to scale in (+x,-y) direction? – Sharief Shaik Feb 18 '11 at 14:53