My First Android Application

Leave a Comment
Once Eclipse has started, go to File –> New –> Other (or type Ctrl + N / Command + N).  From there you can either find Android or type it in the filter text box at the top. Choose Android Project and click Next.  Next, you’ll come to a Create Android Project window.  The only thing you need to specify here is the Project Name.
You will then be asked to select a Build Target.  If you installed all of the Android SDK versions as detailed in our first post, you should see quite a few.  If not, you should at least see the most recent, 4.0.  Go ahead and choose API Level 14 for Platform 4.0 with target name Google APIs.  We’re not going to do anything 4.0 specific; however, it’s a good idea to make your applications able to run on the latest version of Android (we’ll get into this a bit more in a moment).  Once selected, click Next
When all is done your window should look just like this:


For now we’ll skip it.  Go ahead and click Finish.  Your application should now be created.  Before we run the project, let’s start the emulator.  Go to Window –> AVD Manager.  Pick the hvga emulator you created earlier and click Start.  From there you should just be able to click Launch.  If this is the first time you’ve started the emulator, it’s going to take a little while.  Once the emulator is running, go back into Eclipse.  Right click on your project go to Run As –> Android Application.  You should see some messages coming up in the Console about uploading and installing FirstApp and then starting an activity.  If you go back to your emulator you should now see the boiler plate app in action:


Take a look at some of the files that made this happen.  First open the AndroidManifest.xml file in the root directory.  The manifest file contains app specific settings such as app version and minimum SDK version, the different permissions required, activities that can be run, services, broadcast receivers and more. The XML view of the file:
   1:  <?xml version="1.0" encoding="utf-8"?>
   2:  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
   3:      package="com.test.firstapp"
   4:      android:versionCode="1"
   5:      android:versionName="1.0" >
   6:      <uses-sdk android:minSdkVersion="8" />
   7:      <application
   8:          android:icon="@drawable/ic_launcher"
   9:          android:label="@string/app_name" >
  10:          <activity
  11:              android:label="@string/app_name"
  12:              android:name="FirstAppActivity" >
  13:              <intent-filter >
  14:                  <action android:name="android.intent.action.MAIN" />
  15:                  <category android:name="android.intent.category.LAUNCHER" />
  16:              </intent-filter>
  17:          </activity>
  18:      </application>
  19:  </manifest>

At the top of our manifest you’re specifying your unique package name (com.test.firstapp), your app version code and version name, and the minimum SDK Version.  After that, you have your application settings.  At the top level you’re specifying our icon and label (don’t worry we’ll look at drawables and strings soon).  Then, you have your activity. The intent-filter stuff essentially specifies that this activity is what should launch when the app runs. 
Now let’s look at those @string things.  Back in Package Explorer, expand the res folder and then the values folder.  You should see a strings.xml file.  The XML view of the file: 
   1:  <?xml version="1.0" encoding="utf-8"?>
   2:  <resources>
   3:      <string name="hello">Hello World, FirstAppActivity!</string>
   4:      <string name="app_name">FirstApp</string>
   5:  </resources>

You should see two strings that were already created.  If you look up above at the manifest where it shows@string/app_name, it’s referring to the app_name string seen here.  This strings.xml file is basically a resource file.  These are heavily used in Android to make it easier to localize your applications .  As an example, if you wanted to release your app in the US and in France.  The default strings.xml file would contain English text.  Inside of a res/values-fr/ folder we would have another strings.xml that would contain the French equivalent text.  So going forward, if you want to add text to your UI or use it in your code behind, make sure you’re making use of these files. 
Next expand the res/layout folder and open main.xml.  Once again, you get a graphical view.The XML view of the file:
   1:  <?xml version="1.0" encoding="utf-8"?>
   2:  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   3:      android:layout_width="fill_parent"
   4:      android:layout_height="fill_parent"
   5:      android:orientation="vertical" >
   6:      <TextView
   7:          android:layout_width="fill_parent"
   8:          android:layout_height="wrap_content"
   9:          android:text="@string/hello" />
  10:  </LinearLayout>

The LinearLayout is a way of containing and organizing elements inside of it.  The TextView is used to display a block of text.  You’ll see again that we’re using a string resource to set our TextView’s text property. 
Return to the package explorer and expand src –> com.test.firstapp and open the FirstAppActivity.java file.  Once there you should see this Java code:
   1:  public class FirstAppActivity extends Activity {
   2:      /** Called when the activity is first created. */
   3:      @Override
   4:      public void onCreate(Bundle savedInstanceState) {
   5:          super.onCreate(savedInstanceState);
   6:          setContentView(R.layout.main);
   7:      }
   8:  }

You can see here that the FirstAppActivity class is extending the Activity class (which is part of the Android SDK).  We’re only overriding one method from the Activity subclass, the onCreate method.  This method is called when the activity is created and also when the screen is rotated.  We call the super.onCreate method so the Activity subclass can handle it’s plumbing and then we call setContentView.  This method is passed R.layout.main.  R is a way to refer to the res folder and is how we programmatically access the items in that folder (and it’s sub folders).

0 comments:

Post a Comment