4

I made a nine-patch, and a custom viewgroup, then I made the background of that viewgroup to be the nine-patch.

The problem is: The nine-patch is ignoring the "content area" settings.

So: How I use a nine-patch properly in a custom view?

OR:

How I grab the content area from the nine-patch so I can use it on the OnMeasure and OnLayout math?

speeder
  • 6,197
  • 5
  • 34
  • 51

3 Answers3

6

Also use boolean getPadding(Rect padding) on the NinePatchDrawable to get the padding for the content (your content + 9patch padding = total group dize)

Kamen
  • 3,575
  • 23
  • 35
  • Totally correct awnser, thanks :) Too bad Android documentation suck and do not explain that properly. – speeder Jun 01 '11 at 19:18
0

Get the background's padding in your onMeasure to decrease the available width (this is for laying out cells in a grid):

    @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    Drawable background = getBackground();
    Rect padding = new Rect();
    if (background != null && background.getPadding(padding));

    int width = MeasureSpec.getSize(widthMeasureSpec);
    int availableWidth = width - padding.left - padding.right;

And for onLayout, the padding is applied:

    @Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    int availableWidth = r - l - getPaddingLeft() - getPaddingRight();
Sveinung Kval Bakken
  • 3,715
  • 1
  • 24
  • 30
0

You may need to draw the object directly on the ViewGroup by overriding its onDraw method:

public void onDraw(Canvas canvas) {
 NinePatchDrawable bg =  (NinePatchDrawable) resources.getDrawable(id);
 if (bg != null) {
  bg.setBounds(0, 0, getWidth(), getHeight());
  bg.draw(canvas);
  }
 }
rob
  • 9,933
  • 7
  • 42
  • 73