Implementing Application Activities


Build your own Android App Dev Empire

Get Instant Access

To implement a base Activity class, you simply copy the source file called Then you create a new class file called QuizActivity. This class should look very simple for now:

In the project code for this chapter, and future chapters, you will see that there is only a single drawable resource directory named /drawable. Whenever the Android system goes about picking a drawable resource, it only looks in this directory instead of the three default directories, /drawable-ldpi, /drawable-mdpi, and /drawable-hdpi. For this book, the icon quizicon.png file contains the hour number to differentiate each project visually.

package com.androidbook.triviaquiz6;


public class QuizActivity extends Activity {

public static final String GAME_PREFERENCES = "GamePrefs";

You will add to this class later. Next, you update the QuizSplashActivity class to extend from the QuizActivity class instead of directly from the Activity class.

Creating the Rest of the Application Activities

You can copy the QuizSplashActivity five more times, once for each activity you need: QuizMenuActivity, QuizHelpActivity, QuizScoresActivity, QuizSettingsActivity, and QuizGameActivity.

Note the handy way that Eclipse updates the class name when you copy a class file. You can also create class files by right-clicking the package name and choosing New Class. Eclipse presents a dialog where you can fill in class file settings.


Note that there is an error in each Java file. This is because each activity is trying to load the main.xml layout file, which no longer exists. You need to modify each class to load the specific layout associated with the activity. For example, in the QuizHelpActivity class, you modify the setContentView() method to load the layout file you created for the help screen as follows: setContentView(R.layout.fteip);

You make similar changes to the other activity files, such that each call to setContentView() loads the corresponding layout file.

Updating the Android Manifest File

You now need to make some changes to the Android manifest file. First, you modify the application icon resource to point at the @drawable/quizicon icon you created. Second, you need to register all your new activities in the manifest file so they will run properly. Finally, you set the Debuggable application attribute to true and verify that you have QuizSplashActivity set as the default activity to launch.

For more tips on working with Eclipse, check out Appendix B, "Eclipse IDE Tips and Tricks."

For the book code, we've only created a single icon. However, even if you've created three differently sized icons and placed them in the three default directories (/drawable-ldpi, /drawable-mpi, and /drawable-hdpi), only a single reference to the icon is required. Just make sure all of the icons are named exactly the same. This allows the Android system to choose the most appropriate one for the hardware the application is running on.

Was this article helpful?

0 0

Post a comment