I am using the below code to instantiate a 9patch image and set it as the background of a button. The following image shows the non-ideal result.
InputStream = MyClass.class.getResourceAsStream("/images/btn_default_normal.9.png");
Drawable d = NinePatchDrawable.createFromStream(in, null);
button.setBackgroundDrawable(d);

Tried the below code as well, which seems to result in an ANR caused by native android code. It’s not really clear what happens, but the application exists without warning, the log says something about an ANR, and I see the following stacktrace quite a bit in the log.
InputStream = MyClass.class.getResourceAsStream("/images/btn_default_normal.9.png");
Bitmap bitmap = BitmapFactory.decodeStream(in);
byte[] chunk = bitmap.getNinePatchChunk();
NinePatchDrawable drawable = new NinePatchDrawable(bitmap, chunk, new Rect(), null);
button.setBackgroundDrawable(drawable);
at android.graphics.NinePatch.validateNinePathChunk(Native Method)
at android.graphics.NinePatch.<init>
at android.graphics.drawable.NinePatchDrawable.<init>
the
createFromStream()method is part of the Drawable class (which NinePatchDrawable extends) So it is returning to you a plain Drawable object, rather than a nine patch. That is why your button turns out looking that way I suspect.Where is the image that you are trying to make in to a nine-patch? It seems from your example that it may be included in with your application resources (perhaps in one of the drawable folders?) if this is the case is there a reason that using the ID of the drawable will not work for your situation? you should be able to do something like this:
If I am missing something(which I definitely could be.) and there is a reason you can’t use the resource ID. From the docs it looks like your second block of code is closer to what you’ll want.
But the constructor you’re using is deprecated. Try using this one instead. which would look like this:
The only difference being the first paramter should be your Applications Resources object.
However, honestly I could’ve sworn I read somewhere at some point that the system was incapable of working with NinePatchDrawables dynamically as objects. Even though the NinePatchDrawable object exists, I was under the impression that it was not working / not intended to be part of the public APIs
EDIT:
Does the answer on this question help? Create a NinePatch/NinePatchDrawable in runtime
Also my answer at the bottom of that question reminds me exactly why I was under the impression that it was not working / not indeded for part of the public APIs the docs for the getNinePatchChunk say
But it looks like they managed to get it working anyway.