Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid TransitionDrawable with multiple items
    primarykey
    data
    text
    <p>I want to create a Button in Android with a text, and a background image. The background image should crossfade every X time.</p> <p>I have this working using a TransitionDrawable with 2 images.</p> <p>But I can't get this to work with more than 2 images.</p> <p>What I have :</p> <p>In Java code I create a Button and set a background (which is a TransitionDrawable defined in XML). And I start the transition.</p> <pre><code>final Button b = new Button(getApplicationContext()); b.setTextColor(getResources().getColor(R.color.white)); b.setText("Some text"); b.setBackgroundDrawable(getResources().getDrawable(R.drawable.tile)); StateListDrawable background = (StateListDrawable) b.getBackground(); TransitionDrawable td = (TransitionDrawable) background.getCurrent(); td.startTransition(2000); </code></pre> <p>In XML I define in tile.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;item android:state_pressed="true" &gt; &lt;shape&gt; &lt;solid android:color="#449def" /&gt; &lt;/shape&gt; &lt;/item&gt; &lt;item android:drawable="@drawable/transition"&gt; &lt;shape&gt; &lt;solid android:color="#0000ff" /&gt; &lt;/shape&gt; &lt;/item&gt; &lt;/selector&gt; </code></pre> <p>And finally a transition.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;transition xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"&gt; &lt;item android:drawable="@drawable/desert"/&gt; &lt;item android:drawable="@drawable/hydrangeas" /&gt; &lt;item android:drawable="@drawable/jellyfish" /&gt; &lt;/transition&gt; </code></pre> <p>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.</p> <p>In the doc for TransitionDrawables it is stated that you can specify more than 2 drawables but I can't get this to work.</p> <p>I also tried this without any XML but in pure JAVA but this gave exactly the same problem :-(</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload