Cilenco
Cilenco

Reputation: 7179

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?

Upvotes: 0

Views: 217

Answers (2)

kAnNaN
kAnNaN

Reputation: 3679

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);
}

Upvotes: 1

Digvesh Patel
Digvesh Patel

Reputation: 6533

img_image.setBackground(getResources().getDrawable( R.drawable.icon));

Upvotes: 0

Related Questions