Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7522117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:25:47+00:00 2026-05-30T02:25:47+00:00

I am using the below code to instantiate a 9patch image and set it

  • 0

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

enter image description here

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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T02:25:48+00:00Added an answer on May 30, 2026 at 2:25 am

    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:

    button.setBackgroundResource(R.drawable.btn_default_normal);
    

    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:

    InputStream in = MyClass.class.getResourceAsStream("/images/btn_default_normal.9.png");
    Bitmap bitmap = BitmapFactory.decodeStream(in);
    byte[] chunk = bitmap.getNinePatchChunk();
    NinePatchDrawable drawable = new NinePatchDrawable(getResources(), bitmap, chunk, new Rect(), null);
    button.setBackgroundDrawable(drawable);
    

    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

    Returns an optional array of private data, used by the UI system for some bitmaps. Not intended to be called by applications.

    But it looks like they managed to get it working anyway.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am getting the following error when using the code below the error. can
i am using below code to change the the font type of text view.
Currently i'm using below code which works well. $(#topperAtBaseLevel:visible, #lowerAtBaseLevel:visible, #midAtBaseLevel).hide(); any optimised code?
I am using below code to create a reminder in Google calendar (using Google
In past, I am using Listview and using below code can show a particular
I want to enable auto update script in flex3. i am using below code
I am actually having a list of text boxes. I am using below code
using the below code for decoding json $categories = json_decode($data); $categories = $categories->data; where
I am using the below code to capture the ctrl + alt + Q
I am using the below code to close the window, by searching the window

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.