Sunday, July 06, 2014

Designing Programming the Android SDK - Part 1

Finding your way around the Android Software Development Kit

Create a basic application for Android devices include setting-up the required software on your computer, creating a new Android project, designing a simple layout, coding a simple app, testing the app on an emulator or physical device and signing the app to create a finished .apk file that can be run on any compatible Android device.

Setting-up the Development Software

In order to create our Android app, we are going to need some specific software installed on a computer. The items we need to install are:

1) the Android SDK (Software Development Kit) that has all of the Android-specific specifications
2) the Android Packages and Platforms that we will use when developing our apps
3) the Eclipse IDE (Integrated Development Environment) that will help us to create our app.

Google has made all of this software available in a bundle that can be downloaded here:
http://developer.android.com/sdk/index.html.

We will go through a quick walk-through of the process.
Go to the website referenced above at developer. android.com.
Click the link to Download the SDK ADT Bundle for your operating system. After you accept the license agreement, the download should start automatically. You will want to make sure that the computer you are using meets the requirements: Windows XP (32-bit), Vista (32- or 64-bit), or Windows 7 (32- or 64-bit); Mac OS X 10.5.8 or later (x86 only); Linux (tested on Ubuntu Linux, Lucid Lynx) GNU C Library (glibc) 2.7 or later is required. On Ubuntu Linux, version 8.04 or later is required.

After the Android SDK bundle finishes downloading, decompress the file and make sure that you save the SDK in a safe place on your computer – you will need to keep the decompressed folder on your computer for as long as you want to program Android apps. Remember where you put the folder because you’ll need that information soon.

Then you should be ready to open Eclipse, the program that we are going to use to develop Android apps. Eclipse should be located in the “adt-bundle-<os_platform>/eclipse/” directory. Once you have Eclipse setup and running, you are ready to begin creating Android apps.

Basic App Creation

Now that we have our development environment set-up, we can start developing our Android App. We are going to create a very basic app that consists of a text field and a button that updates that text field. So let’s get started.

The first thing we need to do is to open up Eclipse. Under the File Menu choose New, then select Android Application Project. This opens up a New Android Project Wizard.

Next, type a name for our Android App in the Application Name Field. The Project Name and the Package name fields will be automatically updated to reflect the Application Name.

The Package Name will usually take the form of: com.your_company_website.your_project_name. If you are going to upload this project to the Google Play store, this name has to be an absolutely unique package name that will never be used for a different project by anyone. That’s why the form com.your_company_website.your_project_name is used, because there’s a pretty good likelihood that no one else has used that name.

The Build SDK and Minimum Required SDK fields can be kept at the default settings. The Build SDK is the highest version of Android that we want to use in our programming. The Minimum Required SDK is the minimum version that we want our app to run on. By going to the android website here:
http://developer.android.com/resources/dashboard/platform-versions.html we can see what percentage of users use which Android OS version.

You may note that there are blue, red and yellow icons that you can hover over to get more information about a specific item in the New Android App wizard. The red icon with an “X” will tell you there is an error for that item. The yellow icon with the “!” gives you a warning for that item. The blue icon with the “i” will give you more information about that item.

The next settings that we will look at on the first wizard page are the three check boxes below the SDK settings. For now, we want the Create custom launcher icon box unchecked. The Mark this project as a library icon should also remain unchecked. The Create Project in Workspace icon should be checked.

The Workspace is a folder on your computer that contains a set of project files. For now all we really need to know is where the Workspace folder is located on our computer. You should have created this folder during the Eclipse set-up process. If you need to, you can create a new folder on your computer to use as a Workspace. If you want to save your Android Project to a different location than the Workspace folder that you previously set-up, you can unselect the Create Project in Workspace checkbox and manually find the folder where you would like your project saved.

Clicking next in the Wizard bring up the Create Activity screen. Here we want to make sure that Create Activity is checked and that BlankActivity is selected.

Clicking next will bring up the New Blank Activity screen. An Activity in an Android application can be thought of as a discrete screen inside of your app that can display whatever you create. You can give whatever name you choose for the Activity Name, Layout Name and Title. For the Navigation Type choose None. Leave the Hierarchical Parent section blank.

Now we can click the Finish button and Eclipse has just created our Android Project. Let’s take a look at some of the main files in our app.

The AndroidManifest.xml file

We can open our project in Eclipse by clicking the arrow to the left of our project’s name in the Package Explorer window. After opening the project folder we see the files and folders in our project. Double click on the AndroidManifest.xml file to open it. This file is the main file for the app that has basic app information. On the bottom of the manifest window that just opened-up we should see different tabs: Manifest, Application, Permissions, Instrumentation, AndroidManifest.xml. Click on the AndroidManifest.xml tab. This is the xml code for the Android Manifest file. All of the other tabs in the AndroidManifest.xml file help you to add code that you can see in this tab.

If you are going to do Android Development, you are going to learn some XML (Extensible Markup Language). But we will just dip our toes into the xml now, to begin our learning process.
<?xml version=”1.0” encoding=”utf-8”?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android” package=”com.mysite.TestProject” android:versionCode=”1” android:versionName=”1.0” >
  <uses-sdk android:minSdkVersion=”7” />
  <application android:icon=”@drawable/ic_launcher” android:label=”@string/app_name” >
    <activity android:name=”.TestProjectActivity” android:label=”@string/app_name”>
      <intent-filter>
        <action android:name=”android.intent.action.MAIN” />
        <category android:name=”android.intent.category.LAUNCHER” />
      </intent-filter>
    </activity>
  </application>
</manifest>
In the AndroidManifest.xml file we can see the name of the package, some version information, and some application information. The first section inside of the application tag includes the main icon location and the location for the name of the app. Beneath that is an activity tag that holds the information for the Activity we created using the Wizard. We can see the activity name and the location for a label with a formatted activity name.

Within the activity tag is an intent filter tag. In that intent filter tag are two lines of code that specify this activity to be the main activity and that tell this activity to start when the app is launched. If we had additional activities we would have to add them to the manifest file. If our app required any user permissions, we would have to add those to the manifest file. There are two ways to edit the manifest file: you can change the code directly on the AndroidManifest.xml tab or you can use the other tabs that are meant to help you to add the code.

The next main file we will look at is the Activity we created.

Activity File

Go to the src folder inside your project. Open the folder with the project package name. In that folder should be the Activity that was created by the Wizard using whatever name you chose – by default this will be ProjectNameActivity.java – with whatever project name you chose when you set-up the project.

You see the .java at the end of the file name?

That specifies this as a java file. If you know java, great, Android programming should come easily for you. If you don’t know java, now is the time to start learning because XML and Java are the computer languages we will use to make our Android app – but don’t worry, you can learn a little at a time. By the way, the java files are mostly for the functionality part of the app and the XML files are mostly for the layout features of the app. But as we will see shortly, the two work together.

Now we should have the main Activity file for our app opened. The file should look something like this:
package com.mysite.TestProject;
import android.app.Activity;
import android.os.Bundle;
public class TestProjectActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}
The onCreate(Bundle) method here is what gets called when the app is started. So far the only thing this app does is to set the view to whatever it says at R.layout.main. So let’s see what’s there.

Main Layout file

The third file we are going to look at now is the main layout file. You can find this by opening the res folder, then the layout folder in the Package Manager window. At the bottom of the window that opens when you double click the main.xml file are two tabs: Graphical Layout and main.xml. Like with the AndroidManifest.xml file, you can edit the file by using any of the tabs; you can edit the xml directly or using the Graphical Layout. Check that the xml looks like this:
<?xml version=”1.0” encoding=”utf-8”?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
       android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:orientation=”vertical” >
    <TextView android:layout_width=”fill_parent” android:layout_height=”wrap_content”
       android:text=”@string/hello” />
</LinearLayout>
Here the LinearLayout is acting as our wrapper layout. The only element in the LinearLayout is a TextView. The TextView is set to display whatever is at @string/hello. So let’s take a look at the strings.xml file.

The strings.xml file

The strings.xml file is located in the res folder in the values folder. Like the other xml files, there are multiple tabs at the bottom of the window and you can edit the file using either tab. My strings.xml file looks like this:
<?xml version=”1.0” encoding=”utf-8”?>
<resources>
    <string name=”hello”>Hello World, TestProjectActivity!</string>
    <string name=”app_name”>Test Project</string>
</resources>
Yours should be similar (the names can be different).

So now we have everything ready to test run our app. Except that if we do not have a physical device hooked up to our computer, we will need to first create an emulator.

Read Next - Designing Programming the Android SDK - Part 2
Post a Comment