Android - Hello World
Objective #1: Create a HelloWorld app "from scratch".
- Create a new Java Android project in Eclipse.
Click the File/New/Other/Android/Android Project menu command. Depending on what version of Eclipse that you are using, you may find this Android Project wizard in a different place under the File menu command. Instead of Android Project, it may be named Android Application Project.
- Type HelloWorldAndroid as the Application Name and the Project Name since you may have a plain Java project already named HelloWorld in the same Eclipse workspace. Even though spaces are allowed in project names, I prefer to concatenate & capitalize the words in project names (e.g. WordsWithFriends, AngryBirds).
- Type com.johndoe.helloworldandroid as the package name
but substitute your name for johndoe of course. Make sure that the package name is typed with lowercase letters.
In general, when creating a project, you should have a unique package name for that project. The pattern that we will use in this course will be to name a package as com.johndoe.nameofproject where johndoe is replaced by your whole name and nameofproject is replaced with the name of the Android Java project.
- Choose the desired Build SDK level such as 4.1 or 2.3.3. See the instructor if you have questions about this.
- Click the Next button as many times as you need in order to get to and click the Finish button. If you are asked to specify an Activity name, you may type MainActivity.
- Click the Run/Run menu command or the green triangle on the toolbar to run the application. You will need to choose Android Application in the Run As window that appears.
- It make take awhile for the application to run since it needs to launch an AVD emulator that you previously set up.
If you did not set up an emulator then follow these additional steps:
- Click the Window/AVD Manager menu command
- Click the New button
- Type a name such as emulator4.1 that matches the Target SDK that you choose in the next step
- Choose a Target SDK that was specified by the instructor
- Unlock the emulator phone that eventually opens on your screen. If the application doesn't automatically run on the emulator phone's screen, then run the Android application again with the emulator now unlocked. Do not close the emulator phone window since it takes awhile for it to open up each time you re-execute the application.
- Here are some links for further practice in creating simple Hello World projects:
Objective #2: Open an Android project from a compressed, zip file.
- You can download the zip file for this project, extract it and then use the File / Import menu command to import it into your Eclipse workspace.
Objective #3: Working with the emulator
- Depending on the Android SDK that you are working with each emulator is different.
- You may have to unlock the emulator in order to see an app execute.
- You can click the Home button to go to the Android home screen.
- You can click the Apps icon to see a list of apps. The app that you are testing should be found in the list with your app_name value as its name.
- You can uninstall an app by clicking the System icon and choosing Manage Applications.
- While the HelloAndroidWorld app executes in the emulator, you can press the Menu button. This application's menu only contains a menu option named Settings which doesn't really do anything in this demo app. But the stub is there for you to add menu options when you learn about OptionsMenus later in this course.
Objective #4: Trace a Hello World app.
- The instructor will trace the Hello Android World app in class.
- AndroidManifest.xml - To best understand how an app executes, I think you should first view its manifest file. Double-click the AndroidManifest.xml file that is found in the Package Explorer window. Click the AndroidManifest.xml tab that you'll see at the bottom of the main editing window. This is the actual XML file. The other tabs give you more user-friendly ways to view the same information but I prefer that you understand the XML code itself.
The Android system requires a project to have a manifest file that specifies main properties of the project and its environment including the following:
- reference to the package name
- the minimum and target SDK versions
- the existence of an Activity (i.e. screen). Note that an app can have (and often does) have multiple activities. An app doesn't even need to have an one activity since it could run as a behind-the-scenes service. We will study filters later in this course but you should now realize that this activity is being told (i.e. through an intent) launch when the app is opened.
- the label of the Activity
android:label="@string/title_activity_main"
which is set to the value stored in the string reference (i.e. variable) named title_activity_main.
String references are stored in the project's strings.xml file. (I'll save you the trouble from looking. The value stored there is "MainActivity". Storing text indirectly with this kind of reference is wise and is discussed further down the page. If you weren't a wise programmer though, you could use this more direct but hardwired approach
android:label="MainActivity"
- MainActivity.java - Since the manifest file specifies that MainActivity is the name of the only activity in this project, there is a Java source file (and class) named MainActivity. As required in Java, the name of this source file must be the same as the name of the class.
Notice the package that is referenced at the top of the file. Don't forget to update the package name if you've copied and pasted code from a tutorial website. Also notice the required imports since we are using those SDK classes.
Execution begins in the onCreate method
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
The only important line of code that you need to understand at this point is
setContentView(R.layout.activity_main);
This Java source file is specifying that the interface (i.e. view) of this project is found in the resource named activity_main which is found in the layout subfolder of the resources folder. R stands for resources by the way. You don't necessarily have to have one interface for each activity source file. For example, you could have different interfaces that match different users' hardware or preferences and use conditional logic to pick which one is brought up first.
We will not discuss the onCreateOptionsMenu method at this time since we will be studying menus later in this course.
- activity_main.xml - While the name of the activity in this project is MainActivity, you'll see in the Java source file that the activity's look and interface is specified by the attributes and values in the XML file named activity_main.xml.
Since this XML reference is not a Java class, the name of this file does not have to be named ActivityMain. In this case, the programmer chose to use the name activity_main.xml (in lowercase with an underscore separating the two words). This is a fine style to help match up your source code with your corresponding XML interface files.
- screen snapshot of the emulator - Notice the label MainActivity at the top of the window. Also notice the phrase "Hello World!" in the middle of the window.
- strings.xml - It is wise to type the pieces of text that you use in an Android app in your strings.xml file. Since they are not hard-wired into your code, this allows you to edit these phrases more easily without accidentally damaging your program. Also, it allows you to create separate strings.xml files for different languages (i.e. English, Spanish, etc.) It also emphasizes the MVC approach to programming where the interface (i.e. View) is separated from the logic (i.e. Model).
In this case the app_name, which you saw referenced in the manifest file is set to the value "HelloWorldAndroid".
The phrase that you see on the screen is like a string variable named hello_world and set to the value "Hello world!".
The word Settings that appears on the menu when the menu button is pressed is stored in the string resource menu_settings.
Finally the label "MainActivity" that appears at the top the activity's screen is stored in title_activity_main.
<string name="app_name">HelloWorldAndroid</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>