Basic Programming for Android

Basic Programming for Android
Page content

Intro to Android

Android is developing applications (software). It was created for mobile phones and other devices (such as smartphones and tablet computers).

- Is often referred to as the Open Handset Alliance

- It includes an operating system, middleware and key applications

- The mobile platform is available, for free, as the Android Open Source Project

The Android Web site is a good place to start.

Version History: 23 September 2008 was the release of version 1.0. Since then, each version has been developed under a code name based on a dessert item. The current version is Honeycomb 3.0 SDK (a tablet-only release of Android). Later this year, October or November 2011, Android will release a newer version called Ice Cream Sandwich (which will include facial recognition and new APIs).

The Android Dev Guide (see references) provides a practical introduction to developing applications for Android. And, for those interested in retrieving more about this open source application, consider reading some of the “Google Android How-To Guides” (which are articles from BH).

Using Android

System Requirements

  • Windows XP (32-bit) or Vista (32- or 64-bit)
  • Mac OS X 10.4.8 or later (x86 only)
  • Linux (tested on Linux Ubuntu Dapper Drake)

Note: users should download Eclipse and Android SDK (1.6).

- Begin here by Installing the Android SDK

- Those using Eclipse, make sure to install the Android Development Tools (ADT) Plugin

Using Android SDK (Note: Google had released Android SDK)

- It provides the tools and APIs needed to begin developing applications.

- It will run on Windows XP or Vista; Mac OS 10.4.8 or later; or Linux Ubuntu Dapper Drake or later.

Using Eclipse

- to edit files and verify that they compile

- used for error checking

- to check directories in search for Java files

  • Users can highlight and execute a piece of source code by pressing CTRL + SHIFT + D

  • Users can highlight or select text by pressing ALT + SHIFT + UP ARROW (or DOWN ARROW)

Programming

Android applications are written in the Java programming language, so learning a bit of Java is necessary.

Also, Android supports XML syntax. It’s used for designing application screens.

Need help getting started? There’s a helpful forum (blog) that users can learn Android Basic Programming. The site: Android Competency Center. It contains Android tutorials and tips.

For users wanting to build graphical user interfaces for the Android cell phone platform, there’s DroidDraw.

Android uses the aapt tool into an Android package, an archive file marked by an .apk suffix.

Using the Android Asset Packaging Tool

  • ./aapt (used by Linux and Mac OS)

  • aapt.exe (used by Windows)

- Is included in the tools directory of the SDK

- It allows you to view, create, and update Zip-compatible archives (zip, jar, apk)

Building the code

Note: To build the files, run make from within your working directory.

$ cd ~/mydroid

$ make

Note: If users are missing the “run-java-tool,” try setting the ANDROID_JAVA_HOME env var to $JAVA_HOME

$ export ANDROID_JAVA_HOME=$JAVA_HOME

To Create an AVD (Android Virtual Devices)

android create avd –target 2 –name my_avd

To Select, Add, Delete, and Modify Data - use the content provider URI (CONTENT_URI)

The CONTENT_URI consist of three parts:

  1. The string “content://”
  2. The segment representing the kind of data to retrieve
  3. The specific item of the specified content provider

Importing (examples):

import android.app.AlertDialog.Builder;

import android.content.Context;

import android.content.DialogInterface;

import android.content.DialogInterface.OnClickListener;

import android.preference.ListPreference;

import android.preference.Preference;

import android.util.AttributeSet;

To Ask for a User’s Name:

androidid=”@+id/nameMessage”

Examples of how to define the activity entry in the AndroidManifest.xml:

android:name=”DialogActivity”

android:label=”@string/activity_dialog”

android:theme=”@android:style/Theme.Dialog”>

To Create a Dialog:

MyDialog dialog = new MyDialog(context); dialog.show();

Using AlertDialog:

AlertDialog dialog = new AlertDialog.Builder(context).create();

dialog.setMessage(”Do you play golf?”); dialog.setButton(”Yes”, myOnClickListener); dialog.setButton2(”No”, myOnClickListener);

dialog.show();

To Add a Record:

private void insertRecords(String name, String phoneNo) {

ContentValues values = new ContentValues();

values.put(People.NAME, name);

values.put(Contacts.Phones.TYPE, People.Phones.TYPE_MOBILE);

values.put(People.NUMBER, phoneNo);

}

To Display a Record:

private void displayRecords() {

String columns[] = new String[] { People.NAME, People.NUMBER };

String name = null;

String phoneNo = null;

name = cur.getString(cur.getColumnIndex(People.NAME));

phoneNo = cur.getString(cur.getColumnIndex(People.NUMBER));

}

To Modify a Record:

private void updateRecord(int recNo, String name) {

ContentValues values = new ContentValues();

values.put(People.NAME, name);

}

To Delete a Record:

private void deleteRecords() {

Uri uri = People.CONTENT_URI;

getContentResolver().delete(uri, null, null);

}

References