5

I have a TransitionDrawable. The two states are both nine patches and have a content area defined. The problem is the margin is just way too high - in fact it's basically twice as much as it should be. It seems like it's adding up the margins of the two nine patches.

The margin works as it's supposed to work if I set one of those ninepatches directly as a background and not use the TransitionDrawable, so the nine patch is definitely correct.

Has anybody encountered a problem like this?

EDIT: If I set the content area to the full size of the asset and define the padding in xml it works. OTherwise it'll always get the margin wrong. It seems like a bug in Android, so that seems to be the only solution, but maybe there's a nicer way to work around this :/

Maria Neumayer
  • 3,337
  • 3
  • 27
  • 44
  • Can you post your 9 patches and the xml layout they are in? It's hard debug without being able to see your code. I have run into this before, where my `StateListDrawable` did not have proper margins, and I realized the the content/stretchable areas did not match on my images, and Android took the common denominator. – Steven Byle Feb 15 '13 at 20:28
  • 1
    It seems to be a bug in the framework: http://code.google.com/p/android/issues/detail?id=48412 It works if I overwrite the padding in xml, but it's not really what I want to do – Maria Neumayer Feb 15 '13 at 20:53
  • Out of curiosity, what happens if you use the same drawable for both items in your transition in your `TransitionDrawable`? That would be a guaranteed way of knowing you are using the same size 9 patches with the same size content/stretchable areas, and that Android is doing something wrong... – Steven Byle Feb 15 '13 at 21:00
  • Yep exactly the same problem. It just adds up the paddings in both states. – Maria Neumayer Feb 15 '13 at 21:12
  • Even if I overwrite the padding it just adds up the padding again once I change state. – Maria Neumayer Feb 15 '13 at 21:28
  • Interesting... good find – Steven Byle Feb 15 '13 at 21:52
  • Here's the [code](https://android.googlesource.com/platform/frameworks/base/+/kitkat-release/graphics/java/android/graphics/drawable/LayerDrawable.java#361) where Android adds up the padding of all layers. – apricot Aug 20 '14 at 17:58

1 Answers1

3

Yep, you're right. Annoying indeed.

As you've proposed, I use the hard-coded negative paddings to compensate first_nine_patch's 9-patch padding so far.

<?xml version="1.0" encoding="UTF-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/first_nine_patch" />
    <item android:drawable="@drawable/second_nine_patch"
        android:top="-7dp"
        android:right="-16dp"
        android:bottom="-8dp"
        android:left="-4dp" >
    </item>
</transition>
riwnodennyk
  • 8,140
  • 4
  • 35
  • 37
  • Thanks a lot for sharing this workaround! Once they fixed this issue in Android, we will have to change it again. Annoying, indeed.. – netzpurist Aug 28 '14 at 18:47