Search code examples
androiddrawabletext-manipulationanimationdrawable

Quick way to add hundreds of items to an XML <animation-list> element


I have to add up to 100 .png's to create an AnimationDrawable on Android.

Luckily, the png's are named in ascending order (p1, p2, p3, p4).

Manually adding all pngs into the XML element is painfully slow, so I'm wondering if there's a way to automatically 'increment' a filename to a certain limit.

The method does not have to use Android Studio. Any text manipulation program will do. Below is an example of a desired result:

<animation-list android:id="@+id/selected"
android:oneshot="false"
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/p1" android:duration="500" />
<item android:drawable="@drawable/p2" android:duration="500" />
<item android:drawable="@drawable/p3" android:duration="500" />
<item android:drawable="@drawable/p4" android:duration="500" />
...
...
...
<item android:drawable="@drawable/p99" android:duration="500" />


</animation-list>

Solution

    • Create the sequence from 1 to 99 in MS Excel/Apple Numbers/Google Sheets
    • Copy the entire sequence to your favorite text editor
    • Search with a regex for new lines (\n)
    • Using Sublime or Atom, press Alt + Enter
    • Move to the start of the line paste item android:drawable="@drawable/p
    • Now, move to the end of the line and paste " android:duration="500" />