Exploring and coding strings.xml
W2A relies on
strings.xml to store string data that is referenced from other locations. Refer back to Listing 2 and you'll notice that the
<Button> tag includes an
android:text="@string/animate" attribute. This attribute references the button's text via
@string/animate, which references a string resource named
animate that's stored in
strings.xml. Listing 3 presents this file's contents.
Listing 3. strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">W2A</string> <string name="animate">Animate</string> </resources>
As well as
animate, Listing 3 reveals a string resource identified as
app_name. This resource ID identifies the app's name and is referenced from the app's
AndroidManifest.xml file, typically from the
label attribute of the application element start tag.
In your project window, the values sub-branch of the res sub-branch includes a strings.xml sub-branch. Double-click this sub-branch to show the strings.xml tab, then replace its contents with Listing 3 and save the changes.
Exploring and coding animate.xml
Finally, W2A relies on
android_animate.xml, which stores an animation list of drawable items. Listing 4 presents the contents of this file.
Listing 4. android_animate.xml
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="true"> <item android:drawable="@drawable/android0" android:duration="500" /> <item android:drawable="@drawable/android1" android:duration="500" /> <item android:drawable="@drawable/android2" android:duration="500" /> <item android:drawable="@drawable/android0" android:duration="500" /> </animation-list>
Listing 4 starts with an
animation-list element that describes the drawable sequence. This element's
oneshot attribute determines if the animation will cycle in a loop (when this attribute is assigned
"false") or occur only once (when it's assigned
"true" is assigned to
oneshot, you must invoke
stop() method before its
start() method to generate another oneshot animation sequence.
Nested inside the
animation-list element is a sequence of
item elements. Each
item element identifies one drawable in the animation sequence via its
drawable attribute. The
@drawable/androidx resource reference (where
x ranges from
2) identifies an image file whose name starts with
duration attribute identifies the number of milliseconds that must elapse before the next
item element's drawable is displayed.
In the project window, right-click the drawable sub-branch of the res sub-branch. Figure 3 shows the resulting pop-up menu.
Select New from the pop-up menu, followed by File. Figure 4 shows the resulting New File dialog box.
Type android_animate.xml into the "Enter a new file name" text field and click OK. (You'll see an android_animate.xml sub-branch under the drawable sub-branch.) Then, replace the contents of the resulting android_animate.xml tab with the code from Listing 4 and save your changes.
You'll also need to copy the
android2.png files (referenced in Listing 4) from the code source associated with this article to the drawable branch. Assuming you're working in Windows, select these files from Windows Explorer and paste them into this branch (right-click the branch name and select Paste).
Conclusion to Part 2
Now that you understand W2A's architecture and have learned how to code a simple animated mobile app in Android Studio, you're ready to build and run your app. Turn to Part 3 when you're ready for this final step.