I was inspired by my professor of ECL500 class, Jordan Anastasiade, to make this blog post about Android Development. As you are aware, Android apps are developed in Java. As long as you know the basics of Java development, you shouldn't have problems developing apps for Android. There are, however, a couple of new conventions you need to learn and get familiar with before you can develop one. First of all, we will be developing apps in Eclipse. So I suggest that you download the latest version of Eclipse Classic (Indigo).
After that, you will need to download and install the Android SDK. Finally, you will need to install the Android ADT plugin in Eclipse. For more information, please follow this tutorial starting from Step 3. After you have set everything up, you will need to install a couple of packages from the Android SDK manager. Please install everything under "Tools" and "Android 4.0.3 (API 15)" - we will be developing apps using the latest version of Android. The following packages are essential throughout this tutorial and future ones:
- SDK Platform
- Documentation for Android SDK
- ARM EABI v7a System Image - this image is needed for the Android emulator!
The first thing we will tackle on this post is Activity. In Android, an Activity is an application component that provides a screen in which users can interact. The following diagram shows the important state paths of an Activity. The square rectangles represent callback methods you can implement to perform operations when the Activity moves between states. The colored ovals are major states the Activity can be in.
To create an activity, one must create a subclass of Activity class. As you can see in the diagram, an activity has a couple of callback methods that the operating system automatically calls on each cycle. These methods are pretty self-explanatory, but in order to work with them, you must override them in a class you create extending the Activity class.
Create a new project in Eclipse:
- Click "File" then under "New", click "Project..."
- Expand "Android", select "Android Project" then click "Next"
- Type a Project Name, then click "Next"
- Check "Android 4.0.3" for the build target then click "Next".
Note: You may choose whatever you wish, it will still work for this tutorial. But please note that newer versions have newer methods and features
- Type a Package Name (eg. test.part1), then click "Finish"
Eclipse will create a bunch of files. The important ones are under "src" and "res" directories and the "AndroidManifest.xml" file. Open that one Java file in "src" directory under the package name you made up. There, you should see something similar to this:
package test.part1;
import android.app.Activity;
import android.os.Bundle;
public class TestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
As you can see, it already override the onCreate() method for you. The first thing it does is call the onCreate() method of the parent. The next line set its content view or the layout of your activity. On this case, it is saying to use the "main" layout, which also Eclipse have created for you.
The main layout is located under the "res" directory, expand it. There, you should see some or all of the following directories:
- drawable-hdpi
- drawable-ldpi
- layout
- values
Everything under "res" sets the feel and looks of your app. Starting from the resolution of the phone, to the layout and to the values in a layout. Expand the "layout" directory and you should see "main.xml", open it. These layouts are defined in an XML format. But if you open it in Eclipse, it should show you the Graphical view of it.
Eclipse makes it easy for you because you can easily drag and drop buttons, widgets, images, etc. into the layout. Click the "main.xml" tab to see the XML version of it.
Before we can run this app, we need to create our emulator! In Eclipse, click the "Android Virtual Device Manager" icon:
It should open the "Android Virtual Device Manager" window. On that window, Click "New" and make up a name. The target of course should be "Android 4.0.3 (API 15)". You may also set the size of the SD Card. After that, click "Create AVD".
Now, let's compile and run this app in the emulator! Do the following:
- In Eclipse, click "Run", then click "Run Configurations..."
- Double click the "Android Application" at the left-hand side and it should create a new run configuration for you
- Click "Browse" and choose the Test project
- Click the "Target" tab, then choose the AVD you created.
- Click "Apply", then click "Run"!
NOTE: Please wait for a while to load. The app should be installed to it once it's finished!
Now that we've created and run our first app, is it possible to add more activities to it? The answer is yes! Let's go back to your Test project and create a new Activity class:
- Click "File", then under "New", click "Class"
- Make up whatever name for your second activity
- In "Superclass:", click "Browse..." and search for "android.activity" and select the first result
- Make sure that you have "android.app.Activity" as your Superclass, since that's the class we want to extend. Click "Finish"
The new class should roughly contain the following:
package test.part1;
import android.app.Activity;
public class SecondActivity extends Activity {
}
The next step is to create a new layout for this new activity. Do the following:
- Under res/layout, right click the directory "layout", then under "New", click "Other"
- Expand "Android", choose "Android XML Layout File", then click "Next"
- Make up a name, then click "Finish"
This should create a new XML file for layout and open the graphical interface. Try to play around with it, make sure you save it after. If you wish to add a string, you must define it in "strings.xml" file under "values" directory, open it. Go to the XML version of "strings.xml" because it is easier. You should roughly see the following:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, TestActivity!</string>
<string name="app_name">Test</string>
</resources>
Just add another string element, with a unique name for it. If you want to refer to this string in your newly created layout, first of all, you must drag and drop a TextView from widget to the layout. After that, go to the XML version of the layout. You should see the "TextView" element. It should have an attribute called "android:text". Erase all everything in it and add the following: @string/whatever_name_of_your_string - this should refer to the string you created in "strings.xml"
Now that we've customized our newly created layout, we must tell our new activity to use it! We do that by doing the same way as our first activity, we override onCreate() and call the setContentView() method:
public class SecondActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.the_name_of_your_second_layout);
}
}
We set the content view to our new layout. If the name of our new layout is "second.xml", we pass "R.layout.second" to setContentView() method.
So how do we run this new activity alongside with the existing one? We have two activities! Which activity runs first? This setting is defined under "AndroidManifest.xml" file. In order to use this new activity we created, we must define it first in the "AndroidManifest.xml" file:
- Open "AndroidManifest.xml" file
- Go to "Application" tab"
- Under "Application Nodes", right click and click "Add..."
- Double click "Activity", then browse for the new activity you created, choose it
- Save and close the "AndroidManifest.xml" file
Now let's open our very first Activity and add the following method inside it:
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == event.KEYCODE_DPAD_CENTER) {
Intent newActivity = new Intent(this, SecondActivity.class);
startActivity(newActivity);
}
return super.onKeyDown(keyCode, event);
}
Let me explain what this do line by line. The first line checks for the key that is being pressed. If the center pad is pressed, if statement should be true and therefore run whatever's under it. The next line creates an Intent object. We need to create an Intent object to tell the Android OS that we intend to do something. On this case, we intend to start a new activity when the center key pad is pressed. We use the startActivity() method to run the intent that we have created. This should open the new activity that we have created!
All in all, this is a pretty straight forward tutorial with Android Activity. There are a couple of things I haven't touched such as saving instance state (how to keep the state of your current activity if you decided to open a new one) or creating fragments and use them alongside with Activities. But I leave it up to you if you want to learn more about those two.