Friday, March 4, 2011

Reader Feedback: Analysis of an Exercise

At the end of each "Hour" in the Sam's Teach Yourself Android Application Development in 24 Hours book, there are exercises. We originally designed these exercises to be items people could try to extend their knowledge above and beyond what was learned verbatim in the chapter, given some hints and practice using the the Android documentation that is critical to become familiar with.

Here we present one such exercise that occurs in one of the first real coding hours and present our thought process for determining the solution. Hopefully this will help readers who may be struggling with the difficulty of the exercises. That said, we have also determined, based upon reader feedback, that a handful of exercises in the book are perhaps unfairly difficult. For the next edition of the book, many of these will be replaced or labelled as challenge exercises.

Walk-Through of an Exercise

This is from Hour 7 on page 125, if you'd like to follow along. As it's the end of the chapter, you already would have downloaded the code to work along with in the chapter. If not, it's available right here.
"Exercise #1: Modify the LayoutAnimationController in the QuizSplashActivity class to apply animations of each child view within a TableRow control in random order by using the setOrder() method with a value of 2 (random)."
Key Phrase #1: "Modify the LayoutAnimationController in the QuizSplashActivity"

Thought Process, Step 1: This tells me we're dealing with something to do with the LayoutAnimationController. Luckily, we just covered that on page 122, where we gave this following code:

Animation spinin = AnimationUtils.loadAnimation(this, R.anim.custom_anim);
LayoutAnimationController controller = new LayoutAnimationController(spinin);
TableLayout table = (TableLayout) findViewById(R.id.TableLayout01);
for (int i = 0; i < table.getChildCount(); i++) {
    TableRow row = (TableRow) table.getChildAt(i);
    row.setLayoutAnimation(controller);
}

Thought Process, Step 2: The code goes in QuizSplashActivity. Since I'm in Hour 7, I've been implementing this activity for the Splash Screen for most of the chapter.

Key Phrase #2: "to apply animations of each child view within a TableRow control"

Thought Process, Step 3: So far so good. The code is already doing that. We found the code in the section labelled, "Animating All Views in a Layout."

Key Phrase #3: "in random order"

Thought Process, Step 4: Hmm, I don't know how to do that. Let me read on, finishing the sentence...

Key Phrase #4: "using the setOrder() method with a value of 2 (random)."

Thought Process, Step 5: Oh, that's how! OK, hold on. I have two options: I could look up this method in the Android SDK docs, or just write it based on what was said in the exercise and see what happens. (Note: We prefer you look it up, like a good student, but experimentation doesn't hurt anything, either.) But perhaps we're lazy so... Let me just add that one line of code to the end of the initial listing, which will now look like:
Animation spinin = AnimationUtils.loadAnimation(this, R.anim.custom_anim);
LayoutAnimationController controller = new LayoutAnimationController(spinin);
TableLayout table = (TableLayout) findViewById(R.id.TableLayout01);
for (int i = 0; i < table.getChildCount(); i++) {
    TableRow row = (TableRow) table.getChildAt(i);
    row.setLayoutAnimation(controller);
}
controller.setOrder(2); // THIS IS THE ONE NEW LINE OF CODE, THE SOLUTION TO EXERCISE #1

Run the app and see what happens.... Hey! That's pretty neat. Each time I run it, the animations happen in a different order and sometimes some of them happen at the same time.

5 comments:

Web said...

Hi,

Its a very inetresting post. It helped me a lot in understanding the Android documentation in an easy way !! Keep on posting such good content, I would love to read and learn more.

Thanks
Webmaster
A-1 Technology

Mike Bosch said...

Very valuable post for me.

ssaito said...

guys, could you also post a solution to the exercises in Hour 6? For #1, I searched for "time" through the online SDK - can't seem to find a method that returns an INT version (there is System.nanoTime() which returns LONG). For #2, not sure what the difference betwen getPreferences() and getSharedPreferences() is. Perhaps in next book, you might put suggested solutions at end of book for the curious?

Thanks
Steve

hankwi101 said...

I like your book except I'm having trouble making some modifications to your code. I've adapted what you've written for a project I'm working on and now I'm in Chapter 8 where you implement the main menu screen, I have everything working except - this section for the onclick()
the only changes I've made are to the activity names -
my code
if (strText.equalsIgnoreCase(getResources().getString(R.string.menu_item_auctions))) {
// Launch the auction Activity
startActivity(new Intent(AgMenuActivity.this, AgAuctionsActivity.class));
} else if (strText.equalsIgnoreCase(getResources().getString(R.string.menu_item_help))) {
// Launch the Help Activity
startActivity(new Intent(AgMenuActivity.this, AgHelpActivity.class));
} else if (strText.equalsIgnoreCase(getResources().getString(R.string.menu_item_settings))) {
// Launch the Settings Activity
startActivity(new Intent(AgMenuActivity.this, AgSettingsActivity.class));
}

on the startactivity() call I get the error message "Intent can't be resolved to a type" in my androidmanifest file the activities are set and the only change I've made to the code is what you see.

Shane Conder said...

Hi Hankwi101,

It's a bit hard to see what might be wrong from that bit.

Have you compared carefully to the code download for Chapter 8?

Also, feel free to email us directly at androidwirelessdev@gmail.com with any more details that might be helpful.

Thanks for your feedback.