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 8111965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T02:18:38+00:00 2026-06-06T02:18:38+00:00

I need help with NinePatchDrawable: My app can download themes from the network. Almost

  • 0

I need help with NinePatchDrawable:

My app can download themes from the network.
Almost all things work fine, except 9-Patch PNGs.

final Bitmap bubble = getFromTheme("bubble");
if (bubble == null) return null;

final byte[] chunk = bubble.getNinePatchChunk();
if (!NinePatch.isNinePatchChunk(chunk)) return null;

NinePatchDrawable d = new NinePatchDrawable(getResources(), bubble, chunk, new Rect(), null);
v.setBackgroundDrawable(d);

d = null;
System.gc();

getFromTheme() loads the Bitmap from the SD card. The 9-Patch PNGs are already compiled, that means they include the required chunk.

The way how I convert the Bitmap to a NinePatchDrawable object seems to be working, because the image is stretchable as well as I drew it.

The only thing that doesn’t work is the padding. I already tried to set the padding to the view like this:

final Rect rect = new Rect();   // or just use the new Rect() set
d.getPadding(rect);             // in the constructor
v.setPadding(rect.left, rect.top, rect.right, rect.bottom);

d.getPadding(rect) should fill the variable rect with the padding got from the chunk, shouldn’t it? But it doesn’t.

Result: The TextView (v) does not show the text in the content area of the 9-Patch image. The paddings are set to 0 in each coordinate.

Thanks for reading.

  • 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-06-06T02:18:39+00:00Added an answer on June 6, 2026 at 2:18 am

    Finally, I did it. Android wasn’t interpreting the chunk data correctly. There might be bug. So you have to deserialize the chunk yourself to get the padding data.

    Here we go:

    package com.dragonwork.example;
    
    import android.graphics.Rect;
    
    import java.nio.ByteBuffer;
    import java.nio.ByteOrder;
    
    class NinePatchChunk {
    
        public static final int NO_COLOR = 0x00000001;
        public static final int TRANSPARENT_COLOR = 0x00000000;
    
        public final Rect mPaddings = new Rect();
    
        public int mDivX[];
        public int mDivY[];
        public int mColor[];
    
        private static void readIntArray(final int[] data, final ByteBuffer buffer) {
            for (int i = 0, n = data.length; i < n; ++i)
                data[i] = buffer.getInt();
        }
    
        private static void checkDivCount(final int length) {
            if (length == 0 || (length & 0x01) != 0)
                throw new RuntimeException("invalid nine-patch: " + length);
        }
    
        public static NinePatchChunk deserialize(final byte[] data) {
            final ByteBuffer byteBuffer =
                ByteBuffer.wrap(data).order(ByteOrder.nativeOrder());
    
            if (byteBuffer.get() == 0) return null; // is not serialized
    
            final NinePatchChunk chunk = new NinePatchChunk();
            chunk.mDivX = new int[byteBuffer.get()];
            chunk.mDivY = new int[byteBuffer.get()];
            chunk.mColor = new int[byteBuffer.get()];
    
            checkDivCount(chunk.mDivX.length);
            checkDivCount(chunk.mDivY.length);
    
            // skip 8 bytes
            byteBuffer.getInt();
            byteBuffer.getInt();
    
            chunk.mPaddings.left = byteBuffer.getInt();
            chunk.mPaddings.right = byteBuffer.getInt();
            chunk.mPaddings.top = byteBuffer.getInt();
            chunk.mPaddings.bottom = byteBuffer.getInt();
    
            // skip 4 bytes
            byteBuffer.getInt();
    
            readIntArray(chunk.mDivX, byteBuffer);
            readIntArray(chunk.mDivY, byteBuffer);
            readIntArray(chunk.mColor, byteBuffer);
    
            return chunk;
        }
    }
    

    Use the class above as following:

    final byte[] chunk = bitmap.getNinePatchChunk();
    if (NinePatch.isNinePatchChunk(chunk)) {
        textView.setBackgroundDrawable(new NinePatchDrawable(getResources(),
              bitmap, chunk, NinePatchChunk.deserialize(chunk).mPaddings, null));
    }
    

    And it will work perfectly!

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

Sidebar

Related Questions

Need help with PHP/MySql. Need to select all the records from 'today'. My table
Need help writing a script downloads data from google insight using c# this is
need help regarding USSD Gateway. I have to develop an app, which will directly
need help with logging all activities on a site as well as database changes
I need help: My profile expired and I can't renew it - don't know
Need help with an error message that I just can't figure out. I am
Need help to convert code from asp control to input type to fetch file
Need help with deleting call log from multi numbers, using the following code i
Need help resolving a display problem on Fancybox content in IE8. It works fine
Need help with this error. Can't see what's wrong. Error: Warning: mysql_query() expects parameter

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.