If you see black dots that you drew on the 9-patch image, this is because you did not precompile your images.
To set 9-patch images as background at runtime, they must be precompiled when the border is removed and encoded into a PNG block.
You can do this by pasting .9.png images into the res/drawable your application, compiling it and creating .apk (in Eclipse, right-click on project root > Android Tools > Export Unsigned Application Package ). Then unzip .apk and you will have .9.png images with 9 data patches collected in them.
With your precompiled images with 9 patches, do something similar to download the image:
private Drawable loadNinePatch(String path, Context context) { Bitmap bitmap = BitmapFactory.decodeFile(path); byte[] chunk = bitmap.getNinePatchChunk(); if(NinePatch.isNinePatchChunk(chunk)) { return new NinePatchDrawable(context.getResources(), bitmap, chunk, new Rect(), null); } else return new BitmapDrawable(bitmap); }
and then set it as the background of your button:
button.setBackgroundDrawable(loadNinePatch("/data/data/your.app/files/button_background.9.png", context));
source share