2

I tried the accepted solution posted here, but it appears to ignore padding. When the second view (in this case a button) displays, it's much smaller than the original which has padding. Is there a workaround for that? Thanks

Community
  • 1
  • 1
Metallicraft
  • 2,211
  • 5
  • 31
  • 52

1 Answers1

7

Yes, TransitionDrawable extends from LayerDrawable which ignores the padding. This is the getPadding() method in the base Android code, which gets rid of anything you specified:

    @Override
    public boolean getPadding(Rect padding) {
        // Arbitrarily get the padding from the first image.
        // Technically we should maybe do something more intelligent,
        // like take the max padding of all the images.
        padding.left = 0;
        padding.top = 0;
        padding.right = 0;
        padding.bottom = 0;
        final ChildDrawable[] array = mLayerState.mChildren;
        final int N = mLayerState.mNum;
        for (int i=0; i<N; i++) {
            reapplyPadding(i, array[i]);
            padding.left += mPaddingL[i];
            padding.top += mPaddingT[i];
            padding.right += mPaddingR[i];
            padding.bottom += mPaddingB[i];
        }
        return true;
    }

See base Android code here.

To deal with it, I had to first save the padding values before setting the faulty drawable background on my view:

    int bottom = theView.getPaddingBottom();
    int top = theView.getPaddingTop();
    int right = theView.getPaddingRight();
    int left = theView.getPaddingLeft();
    theView.setBackgroundResource(R.drawable.faulty_drawable);
    theView.setPadding(left, top, right, bottom);
dmon
  • 30,048
  • 8
  • 87
  • 96
  • Thanks for the response. However, I'm using the method mentioned in the supplied link, so I have something like: TransitionDrawable transition = (TransitionDrawable) btnTransition.getBackground(); transition.startTransition(200); So, I save the paddings before the transition and then set the paddings afterward. I guess I'm using it wrong, because it doesn't seem to help. – Metallicraft May 25 '11 at 02:17
  • 1
    Sorry if I was unclear, there's no "solution" (and this is a bug, in my eyes). What I meant is that you first need to set the background to another `Drawable` (that's not a `LayerDrawable`) which has the padding values that you want, _then_ you can save the padding values and restore them after you set your `TransitionDrawable`. Or, alternatively, you can set the padding values to the dimensions that you want. In my case, I always had a previous drawable to get the right padding values from. – dmon May 25 '11 at 02:25
  • I guess I understand. Sounds like I cannot do it with a button at all. Sorry, still getting used to Android development. – Metallicraft May 25 '11 at 03:17