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.
A NinePatch graphic is a standard PNG image that includes an extra 1-pixel border. It must be saved with the 9. png extension in the res/drawable/ directory of your project.
A 9 patch image is a regular png (. PNG) image which is needful for android app developers where they require to wrap any content within a background image without pixelating the background image.
Creating 9 patch background image using Photoshop:Draw the images in a regular way as you use to do in Photoshop. Make the 1px spacing around the image from all sides. And also make sure that the image doesn't have anything in this 1px area & has 100% transparency. Otherwise 9patch image will not work.
The Draw 9-patch tool is a WYSIWYG editor included in Android Studio that allows you to create bitmap images that automatically resize to accommodate the contents of the view and the size of the screen. Selected parts of the image are scaled horizontally or vertically based on indicators drawn within the image.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With