Android Quick Start Programming Tutorial

android-programmingThis tutorial will demonstrate how to create your first Android application, setup Android development environment and it is based on Eclipse 4.2 (Juno), Java 1.6 and Android 4.2 (Jelly Bean).

All examples run on Ubuntu 12.04 but the steps are the same on any environment. In case that some of them are different I’ll note that with detailed explanation what to do if so.

Android development environment setup

We’ll install and configure the following software in order to start Android programming:

– Java JDK

– Android SDK

– Eclipse

– Android Development Tools (ADT)

– Android Virtual Device (AVD)

The great thing with Android programming is that all tools that we need are free and can be used on Linux, Windows or Mac machine.

Java JDK

The Android SDK required the Java SE Development Kit (JDK), so if you do not have it installed get it from www.oracle.com/technetwork/java/javase/downloads/index.html

Android SDK

The main tools is Android SDK (software development kit). It contains libraries, debugger, documentation and tutorials, Android emulator and you can get it on http://developer.android.com/sdk/index.html

After downloading unzip the content into desired folder (e.g. /opt/android-sdk)

Note: If you are a Windows user, Google recommends getting the installer (the latest version is installer_r20.0.3-windows.exe) that will configure the tools automatically. Download, run the installer file and follow the steps.

When you start the Android SDK Manager you will see a list of items and whether or not they are currently installed on your machine.

After you select the items to be installed, click the Install button. The download process could take a time depending on your selection, so we advise to get only what you need at that time and install other items later. For the purpose of this tutorial install the following items:

Note: You should at least choose the latest Android SDK platform and the Extras. At the time of writing, the latest SDK platform is SDK Platform Android 4.2, API 17.

Each version of the Android OS is identified by an API level number. For example, Android 4.1 is level 16 (API 16). For each level, two platforms are available, e.g.:

– SDK Platform

– Google APIs by Google Inc.

The main difference is that Google API contains some additional APIs provided by Google (e.g. Google Maps). So, if you need these additional features you need to create an AVD with Google APIs platform.

You will be asked to choose the packages to install, select the Accept All option and click the Install button.

The installation process could take some time, so be patient. After the installation, you will be asked to restart the ADB (Android Debug Bridge); choose Yes.

Eclipse

The recommended IDE (integrated development environment) for Android programming is Eclipse.

For Android development, you should download the Eclipse IDE for Java EE Developers. Visit www.eclipse.org/downloads/ and download proper edition for your environment(Linux, Windows, Mac, 32- or 64-bit…)

After download, uncompress the content into e.g. /opt/eclipse on Ubuntu or C:\Program Files\eclipse on Windows. Start Eclipse by double-clicking the file, choose default workspace folder and click OK button.

Android Development Tools (ADT)

The next step is to install ADT (Android Development Tools) plug-in for Eclipse. The ADT is an extension to the Eclipse IDE that supports the creating and debugging of Android applications.

Run Eclipse and select Help > Install New Software.

Enter https://dl-ssl.google.com/android/eclipse/ and hit Enter. You’ll see the Developer Tools item and you need to expand it to see its content (e.g. Android Development Tools, Android DDMS, Android Hierarchy View, Android Traceview). Select all items and click Next button twice.

02-android-adt-install-ubuntu

Note: If you have any problems downloading the ADT, consult Google’s help at http://developer.android.com/sdk/eclipse-adt.html#installing.

Accept the offered licenses – select “I accept the terms of the license agreements” and click Finish button. Restart Eclipse after the installation is finished. When Eclipse is restarted, you will be asked to configure your Android SDK. Because the Android SDK has already been downloaded earlier in the previous section, check the “Use existing SDKs” option and specify the directory where you have installed the Android SDK. Click Next. After this step, you will be asked to send your usage statistics to Google. Once you have selected your choice, click Finish.

Note: The installation steps could be slightly different in different SDK versions, so if you don’t experience the same steps as we described, just follow the instructios on screen.

AVD (Android Virtual Device)

Creating Android Virtual Device (AVD)

The next task to do is to create an Android Virtual Device (AVD) to be used for running and testing your Android applications. An AVD is an emulator instance that enables you to model an actual device. Each AVD consists of a hardware profile; a mapping to a system image; as well as emulated storage, such as a secure digital (SD) card. You can create as many AVDs as you want in order to test your applications with several different configurations. This testing is important to confirm the behavior of your application when it is run on different devices with varying capabilities.

To create an AVD, select Window > AVD Manager

In the “Android Virtual Device Manager” dialog, click the “New…” button to create a new AVD.

In the “Create new Android Virtual Device (AVD)” dialog, enter the items as displayed below.

Click the “Create AVD” after you select desired items.

In this case, you have created an AVD (simply saying, an Android emulator) that emulates an Android device running version 4.1 of the OS with a built-in 10-MB SD card. In addition to what you have created, you also have the option to emulate the device with different screen densities and resolutions.

It is preferable to create a few AVDs with different API levels and hardware configurations so that your application can be tested on different versions of the Android OS. After ADV has been created, it is time to test it. Select the AVD that you want to test and click the “Start…” button. The Launch Options dialog will appear.

Tip: If you have a small monitor, it is recommended that you check the “Scale display to real size” option so that you can set the emulator to a smaller size.

Click the Launch button to start the emulator.

04-android-avd-launch-options-ubuntu

The Android emulator will start, and after a while it will be ready for use. It will behave just like a real Android device so go ahead and try it out.

05-android-avd-ubuntu

Now, you’re ready for Android programming and we continue with our first Android application named, of course, Hello World.

Creating the first Android application

This part of the the tutorial is focused to creating our first HelloWorld Android application.

1) Start Eclipse

2) Create a new Android project by selecting File > New > Project…

Tip: After you create your first Android application, subsequent Android projects can be created by selecting File > New > Android Project.

2. Expand the Android folder, choose Android Project and click the Next.

06-android-new-project

3. Enter HelloWorld for the Android project and click Next.

4. Select the target platform (e.g. Android 4.2) and click Next.

5. Enter details about the application in the fields

07-android-project-info

Note: You have to enter at least a period (.) in the name of package. The recommended package name is to use your domain name in reverse order, followed by the project name, e.g. my package name would be com.loneshooter.HelloWorld

6. Click Finish.

7. Expand the project in the Package Explorer on the left section of Eclipse and double-click on res/layout/main.xml file to open it.

8. The main.xml file contains configuration of the user inteface for your application. The default view is the Layout view and it layouts the activity graphically. In order to make changes UI manually, select the main.xml tab at the botton.

9. Make changes in the main.xml file to be as follows:

<?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" />

	<TextView
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:text="HelloWorld Android Application!" />

	<Button
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:text="This is a button!" />
</LinearLayout>

10. Save the changes you’ve made.

11. The next step is to test our first application on the Android emulator we installed in one of our previous steps. Right-click the project name in Eclipse > select Run As > Android Application.

12. The application will be started on the emulator

13. Click the Home button and the Home screen will appear

14. Click the application launcher icon in order to show the list of all apps installed on the device. Our first HelloWorld application will be listed in the launcher, too.

How It Works

To create an Android project using Eclipse, you need to supply the information shown below

Project name – The name of the project

Application name – A user-friendly name for your application

Package name – The name of the package. You should use a reverse domain name for this.

Create Activity – The name of the fi rst activity in your application

Min SDK Version – The minimum version of the SDK that your project is targeting

An activity in Android represents a window that contains the UI of an application. There can be none or more activities, e.g. our HelloWorld application has HelloWorldActivity activity.

The HelloWorldActivity is the entry point of the application and it’s displayed when the application is started. We made changes in the main.xml file to display “HelloWorld Android Application!” and a button with text “This is a button”.

The main.xml file has the UI of the activity so when you run your application on the emulator, it’s automatically installed.

Structure of an Android application

Let’s explain structure of an Android application.

-src

It has source files (.java) of a project. Our example has only one file – HelloWorldActivity.java, which is the source file of the activity. This is the place where we write the code for the application.

- get

This folder contains the R.java file. This file is automatically crated file that keeps references to all resources from a project. This file should not be changed manually.

- library

There is only one file in this folder, android.jar. The file has libraries needed for an application.

- assets

Contains files used by an Android application, e.g. text files, databases, HTML files…

- bin

Contains files generated by the ADT during the build process. The .apk file or Android package represents binary file contains everything needed to run an application.

- res

All the resources used in an Android application are located here. Other subfolders are also in this folder, e.g. drawable-%resolution%, layout and values.

- AndroidManifest.xml

AndroidManifest.xml is the manifest file for an application. It contains information such as permissions needed by an application, receivers, intent filters, min SDK version, package name etc.)

- main.xml

It defines the user interface for an activity.

In the following example the @string represents an refference to the strings.xml file in the res/values folder, and hello refers to the hello string defined in the strings.xml file (“Hello World, HelloWorldActivity” in this case):

<TextView
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:text="@string/hello" />

The recommendation and the best practice is to keep all the string constants in the strings.xml. In case that we need to translate and localize appplication to varous languages, the only thing we need to do is to copy the values folder and make changes in strings.xml in new language.

The next important file in an Android project is the manifest file. Note the content of the AndroidManifest.xml file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.loneshooter.HelloWorld"
android:versionCode="1"
android:versionName="1.0" >
	<uses-sdk android:minSdkVersion="14" />
	<application
	android:icon="@drawable/ic_launcher"
	android:label="@string/app_name" >
		<activity
		android:label="@string/app_name"
		android:name=".HelloWorldActivity" >
			<intent-filter >
				<action android:name="android.intent.action.MAIN" />
				<category android:name="android.intent.category.LAUNCHER" />
			</intent-filter>
		</activity>
	</application>
</manifest>

Note: android.intent.category.LAUNCHER means that the application can be launched from the device’s launcher icon.

Eclipse automatically generates the content in R.java file, as I’ve said previously, and if we delete the R.java file it will be recreated again.

Note: If the file is not generated automatically, this indicates that a project contains errors.

At the end, the file HelloWorldActivity.java contains the setConentView method that connects the activity to the Ui (i.e. main.xml):

package com.loneshooter.HelloWorld;
import android.app.Activity;
import android.os.Bundle;

public class HelloWorldActivity extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
	}
}

In the code R.layout.main  refers to the main.xml file located in the res/layout folder. As you add additional XML files to the res/layout folder, the filenames will automatically be generated in the R.java file. The onCreate() method is one of many methods that are fired when an activity is loaded.

This should be all for Android programming for beginners for now from my side. Stay tuned because I’ll write about advanced techniques and Android tips and tricks.

Share This Post

Related Articles

Leave a Reply

© 2024 LoneShooter.com. All rights reserved. Site Admin · Entries RSS · Comments RSS