91
Chapter 3: Your First Android Project
Figure 3-30:
The Properties
editor in Eclipse.
92
Part II: Building and Publishing Your First Android Application
Chapter 4
Designing the User Interface
In This Chapter
▶
Setting up the Silent Mode Toggle application
▶
Designing the layout
▶
Developing the user interface
▶
Adding an image and a button widget
▶
Making a launcher icon
▶
Previewing your work
C
ongratulations You discovered what Android is and how to build your first application. I’m happy to say that you are now getting into the fun
stuff. You’re going to build a real application that you can use and publish to the Android Market.
The application you’re going to build allows the user to toggle the mode of his or her phone’s ringer with a simple press of a button. This application
seems simple, but it solves a real-world problem.
Imagine that you’re at work and you’re about to go to a meeting. You can turn your phone volume down, all the way to silence, and then attend the meeting.
You’d never be “that guy” whose phone rings during a meeting, would you? The problem is that you like your ringer loud, but not too loud. You never
keep it on the loudest setting, only the second-to-loudest setting. When you leave your meeting, you remember to turn your phone ringer volume back
up, but you always have to go all the way to the max volume, and then down one setting, just to make sure that you have the correct setting. While this
isn’t a life-changing event, it’s kind of a nuisance having to do this each time you need to silence your phone’s ringer.
It would be great if you had an application that would allow you to touch a button to turn the ringer off, and then, when you leave the meeting, you
could touch the button again and the ringer would return to the last state that it was in. You’d never have to readjust your ringer again. That’s the
application you’re about to build.
94
Part II: Building and Publishing Your First Android Application
Creating the Silent Mode Toggle Application
Your task at hand is to create the Silent Mode Toggle application, and because you’re already an expert on setting up new Android applications, I’m not going
to walk you through it step by step. If you need a brief refresher on how to create a new Android app in Eclipse, review Chapter 3.
Before you create the new application, you need to close all the files you already have open in Eclipse. You can do this by closing each file individually
or by right-clicking the files and choosing Close All from the shortcut menu.
After you have closed all the files, you need to close the current project Hello Android in which you’re working. In Eclipse, in the Package Explorer,
right-click the Hello Android project and choose Close Project. By closing the project, you are telling Eclipse that you currently do not need to work with
that project. This frees resources that Eclipse uses to track the project state, therefore speeding up your application.
You’re now ready to create your new Silent Mode Toggle application. Create the new application by choosing File➪New Project. Choose Android
Project from the list, and then click the Next button. Use Table 4-1 for your project settings.
Table 4-1 Project Settings for Silent Mode Toggle
Setting Value
Application Name Silent Mode Toggle
Project name Silent Mode Toggle
Contents Leave the default selected create new project in workspace
Build target Android 2.2
Package name com.dummies.android.silentmodetoggle
Create activity MainActivity
Min SDK Version 8
95
Chapter 4: Designing the User Interface
Click the Finish button. You should now have the Silent Mode Toggle applica- tion in your Package Explorer, as shown in Figure 4-1.
If you receive an error that looks similar to this — “The project cannot be built until build path errors are resolved” — you can resolve it by right clicking on
the project and choosing Android Tools➪Fix Project Properties. This realigns your project with the IDE workspace.
Figure 4-1:
The Silent Mode Toggle
application in Eclipse.
Notice how you selected the build target of Android 2.2, and a Min SDK Version of 8. What you have done is told Android that your code can run on any device
that is running at least a version code of 8 Android 2.2. If you were to change this to version code 4, you would be saying that your app can run on any
device running version 4 or higher. How do I know which version this app can run on? I’ve already tested it, before I wrote the book When creating a new
application, you should check to see whether it can run on older versions.
Laying Out the Application
Now that you have the Silent Mode Toggle application created inside Eclipse, it’s time for you to design the application’s user interface. The user interface
is the part of your application where your users interact with your app. It is of prime concern to make this area of the application as snappy as possible
in all regards.
Your application is going to have a single button centered in the middle of the screen to toggle the silent mode. Directly above the button will be an
image for visual feedback, letting the user know whether the phone is in silent or regular ringer mode. A picture is worth a thousand words, so take
a look at Figures 4-2 and 4-3 to see what your application will look like.