Subscribe / Unsubscribe Enewsletters | Login | Register

Pencil Banner

Android Studio for beginners, Part 2: Explore and code the app

Jeff Friesen | Aug. 24, 2016
Use Android Studio's project editor to write your first animated Android app

Choosing and saving a new layout

You might recall that when we set up the app in Part 1, we selected the empty activity template. It turns out the XML layout this template provides isn't appropriate for our app. To choose a new layout, start by clicking the main.xml tab. Android Studio responds by displaying a user-friendly layout editor:

androidstudiop2 fig2

Figure 2. Android Studio's layout editor

Before we move on, click the Text tab (the Design tab is shown by default) at the bottom of the main.xml tab. Replace the template XML with the code from Listing 2, then save the contents of this window.

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"?>
   <string name="app_name">W2A</string>
   <string name="animate">Animate</string>

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.

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

<animation-list xmlns:android=""
   <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" />

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.


Previous Page  1  2  3  4  5  6  Next Page 

Sign up for CIO Asia eNewsletters.