Linked Questions

Popular Questions

Android TransitionDrawable with multiple items

Asked by At

I want to create a Button in Android with a text, and a background image. The background image should crossfade every X time.

I have this working using a TransitionDrawable with 2 images.

But I can't get this to work with more than 2 images.

What I have :

In Java code I create a Button and set a background (which is a TransitionDrawable defined in XML). And I start the transition.

final Button b = new Button(getApplicationContext());
b.setText("Some text");
StateListDrawable background = (StateListDrawable) b.getBackground();
TransitionDrawable td = (TransitionDrawable) background.getCurrent();

In XML I define in tile.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="">
    <item android:state_pressed="true" >
                android:color="#449def" />
    <item android:drawable="@drawable/transition">
                android:color="#0000ff" />

And finally a transition.xml

<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android=""     android:oneshot="false">
  <item android:drawable="@drawable/desert"/> 
  <item android:drawable="@drawable/hydrangeas" /> 
  <item android:drawable="@drawable/jellyfish" /> 

Now the effect is that when I start the app the desert image is shown. This image crossfades to the hydrangeas image as it should. But the jellyfish image is never shown.

In the doc for TransitionDrawables it is stated that you can specify more than 2 drawables but I can't get this to work.

I also tried this without any XML but in pure JAVA but this gave exactly the same problem :-(

Related Questions