Search code examples
androidandroid-layoutnine-patch

Nine Patch work in XML but not in code


In my application I have a 9.png. If I set it in XML via

android:background="@drawable/image"

it works great but if I use this code: bubble.setBackgroundResource(R.drawable.image) to set the 9.png programmaticly it does not work. Any ideas why?


Solution

  • Try this :

    private Drawable displayNinePatch(Bitmap bitmap, Context context) {
        byte[] chunk = bitmap.getNinePatchChunk();
        if(NinePatch.isNinePatchChunk(chunk)) {
            return new NinePatchDrawable(context.getResources(), bitmap, chunk, new Rect(), null);
        } else return new BitmapDrawable(bitmap);
    }