12

What do I need to put in for an attribute if I want to put a null there?

For example, this piece of code:

<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:drawable="@drawable/ef_gradient_blue" />
    <item android:drawable="@null" />

</transition>

But this won't work, as my app crashes when it tries to load this drawable. I need it to be null, so it can make a transition from fully opaque to completely see through.

In my Java, I load it like this:

mTransitionBlue = (TransitionDrawable)mProjectResources.getDrawable(R.drawable.transition_blue);

But I get a runtime exception from this, saying it can't find the transition_blue.xml, which is the XML specified before. So how do I achieve this?

ThaMe90
  • 4,196
  • 5
  • 39
  • 64
  • What do expect transition to transit to? I mean, what do mean under "null" as drawable? Black view? Fully transparent view? – inazaruk May 18 '11 at 09:23
  • I want it to transition from the drawable it has, to just a clear background... A fade effect if you would call it like that... – ThaMe90 May 18 '11 at 09:47

1 Answers1

27

You should use a transparent drawable there (android lib has it predefined):

<transition xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:drawable="@drawable/ef_gradient_blue" />
    <item android:drawable="@android:color/transparent" />
</transition>

the @android:color/transparent's value is #00000000. It's important to have the first two digits 0, since they define the alpha value of the color (the rest is red, green and blue).
So you can use

<item android:drawable="#00000000" />

as well, or redefine it among your colors.

For reference you could take a look at the Android Developers 2D Graphics and Transition Drawables articles.

rekaszeru
  • 19,130
  • 7
  • 59
  • 73
  • This does solve the issue of the crash, but now, the transition is instantaneously, and not flowing from one drawable into the other. And as a side effect, it will now change altogether to a different drawable. – ThaMe90 May 18 '11 at 09:31
  • It might be a matter of sizing, if your view is empty. You could try creating an `ef_transparent.xml` drawable (as you have the `ef_gradient_blue`, just set it's color to `#00000000`), and set that as your `android:drawable` in for the second `item`. – rekaszeru May 18 '11 at 09:38
  • I think I know why this doesn't work though, because startTransition() causes the TransitionDrawable to go from layer 1 to layer 2, layer 2 will be painted on top of layer 1. However, because layer 2 is transparent, layer 1 will still be visible. Am I right? – ThaMe90 May 18 '11 at 10:01
  • Apparently, I missed a setting on the TransientDrawable, which causes it to fade from one to the other, not just place the second over the first. The setting was TransientDrawable.setCrossFadeEnabled to true. This causes the first layer to fade out, and the second to fade in simultaneous. I thank you, for putting me on to this lead. – ThaMe90 May 18 '11 at 10:13
  • I have used this approach but it is giving me overdraw. – Manmohan Jun 20 '19 at 13:40