You are on page 1of 6

Sign In | Register

ANDROID STUDIO FOR BEGINNERS


Android Studio for beginners, Part 2: Explore and
code the app
Use Android Studio's project editor to write your first animated Android app

By Jeff Friesen
JavaWorld |
AUG 23, 2016 12:28 PM PT

Page 2 of 2

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
Sign In | Register
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.

Internationalizing mobile apps


It's a good practice to store strings directly in strings.xml and reference these
resources from elsewhere, rather than hardcode strings in source files and other
resource files. By following this practice, you make it easier to adapt an app to other
international markets, potentially increasing your app's earning potential.

Save strings.xml

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
Sign In | Register

<animation-list xmlns:android="http://schemas.android.com/apk/res/and
roid"
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"). When "true" is assigned to oneshot, you must invoke
AnimationDrawable()'s 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 0
through 2) identifies an image file whose name starts with android. The duration
attribute identifies the number of milliseconds that must elapse before the next
item element's drawable is displayed.

Save animate.xml

In the project window, right-click the drawable sub-branch of the res sub-branch.
Figure 3 shows the resulting pop-up menu.
Sign In | Register

Figure 3. Add android_animate.xml as a new drawable resource

Select New from the pop-up menu, followed by File. Figure 4 shows the resulting
New File dialog box.
Sign In | Register

Figure 4. Enter android_animate.xml

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 android0.png, android1.png, and 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.

Jeff Friesen teaches Java technology (including Android) to everyone.

Follow

Page 2 of 2
Copyright © 2018 IDG Communications, Inc.
Sign In | Register