Introduction to Android App Development

Welcome to the first Android development articles on Mobile Orchard. We are going to start a series of articles to introduce you to the world of Android development. We are going to start off with the Android platform and the necessary tools to start Android development.

Android Software Stack:

This picture describes the Android software stack that can be described as a Linux Kernel and C/C++ libraries exposed through an application framework that provides services for and management of runtime and applications.

Android Software Stack

The elements of the Android software stack are:

  1. Linux Kernel: provides abstraction between the hardware and the rest of the stack, responsible for device drivers (Camera , Wi Fi, etc…), resources management , power management, security and net working
  2. C/C++ Libraries: such as SQL lite, Graphics libraries OpenGL ES, media framework and webkit layout engine.
  3. The Android Runtime: includes Core libraries and the Dalvik Vitual Machine.
    The Core libraries provide most of java libraries + additional Android libraries.
    The Dalvik VM provides (Just In Time) JIT compilation. the VM is optimized to run multiple instances of VMs. As Java applications access the core libraries each application has its own VM
  4. The Android Application Framework: Provides classes required to develop an Android application and abstraction between hardware access. the Android Java API’s main library include telephony, content providers (data), resources, locations and UI.
  5. Application Layer: all Android applications(native or third party) are built on the application layer using the same API.

So Android applications are written in Java, but remember it is not Java ME (Mobile Edition). It’s just Most of J2SE libraries + Android’s own Java libraries.

The Android Application structure:

Android architecture encourages component reuse allowing you to publish and share activities, services and data between applications with security restrictions defined by you. This enables developers to include out of the box components such as the phone dialer or contact manager to their applications, or add new functionalities to them.

The bases of the Android applications are:

  • Activity Manager: which controls the life cycle of the activities, activities can be compared to the windows or web forms they carry the controls (views) that construct the interface, an activity represents a single screen.
  • Views: the UI components that construct the interface. They can be compared to the swing or windows forms controls
  • Notification Manger: provides a consistent mechanism to notify or alert users.
  • Content Providers: lets applications to share data between them.
  • Resources Manager: much like the ASP.NET resources concept. Enables the developer to store resources such as strings or images.

Android Versions:

Android has been updated many times since its release, these updates were to fix bugs and add new functionalities. Each Android update has a name of a dessert.

  • Android 1.5 (Cup Cake).
  • Android 1.6 (Donut).
  • Android 2.0/2.1 (Eclair).
  • Android 2.2 (Froyo).
  • Android 2.3 (Gingerbread).

Getting started with Android development tools:

Android applications can be developed on Windows, Mac or Linux platforms.

To start developing Android apps you need:

  1. Download and install the Java Development Kit (JDK).
  2. The Android SDK
  3. Eclipse IDE .
  4. Follow the installation links from this link.

After you’re done you will be ready to start developing Android Applications.

Our First Android Application:

Now we’re going to explore our first Android application which will be the famous Hello World application. We are going to explain the structure of an Android application and some of the basic concepts we must understand.

  1. Open Eclipse and select from the menu File>New> Project, you’ll see a window like this from the Android node select Android project and press Next.
  2. Now there are several fields that appear here they are:
    Project Name: The Eclipse project name, the name of the folder that will hold the project files.
    Application Name:The name of the application that will appear on the phone.
    Package Name: The application package name, a good convention to use to avoid applications package name conflict is com.mydomain.myapp.
    Create Activity: the name of the basic activity class of your application. it’s optional to create.
    Min SDK Version: the minimum API level required by the application. You see in the Build Target list there is a list of the available SDK versions to use for the application and in front of each one the corresponding API level. You must ensure that you application API level is supported by the device select it to be 8 (for Android 2.2).
  3. Press next; we won’t create a test project for our application yet.
  4. Press Finish then navigate to the project  press run and choose run as Android Application, the emulator will start, wait until the main screen appears.

The Android Emulator may take some time, so please be patient. After the emulator launches, unlock it and your application will launch automatically. it should be like this:

Android Project Structure:

The project contains the following folders:

  • Src: Contains all the source code (class files) for the project.
  • Gen: Contains the R.java class file. The R.java is an automatically generated class file that holds a reference to each resource in the application
  • Android 2.2: Its name changes according to the sdk version you use (2.2 here). Contains the Android API class files packed in android.jar files
  • Assets: You can place any external resources (text files, images,…) in this folder. It’s much like the res file except it’s more like the file system where you put files that you want to access them as raw bytes
  • Drawable folders: Contains the images required for you application. You can add your own image files to this folder. Notice that there are three folders with hdpi,ldpi and mdpi suffixes. These folders are used to include image resources with different resolutions suitable to the different phone screens. And at runtime Android will pick the suitable image from the suitable folder.
  • Layout: Contains the main.xml file that defines the view that construct the User Interface of the application
  • Values: Contains other resources that can be used for your application such as string resources, styles or colors

Now we’re going to take a deeper look on the Hello Android application and know what each line of code exactly means
The MainActivity.java file contains the following code:

package mina.android.hellowolrd;

import android.app.Activity;

import android.os.Bundle;

public class MainActivity extends Activity {

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

}

}
  1. The first line is the package name of the application.
  2. Lines 3,4 are import statements of two libraries(): android.app.Activity and android.os.Bundle
  3. Line 6 is the declaration of the MainActivity class which extends from Activity , Activity represents the User interface view or window of your application much like windows or web forms.
  4. Our class contains one function which is onCreate(Bundle savedInstanceState). This function is called when the activity is first created, it should contain all the initialization and UI setup.
    This function has a parameter savedInstanceState of type Bundle
    this object holds the activity’s previously saved state if exists (similar to asp.net view state concept)
  5. Line 10 calls the super class’ OnCreateMethod with the activity’s
  6. Line 11 calls setContentView(R.layout.main); method which dsiplays the layout definition in main.xml file in res/layout directory by accessing a reference to it from the R class
    This method is required to display the user interface, if the activity does not include this function then it would display a blank screen.

If we went to main.xml in the layout folder we will find it like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
</LinearLayout>

This xml layout file defines a vertical LinearLayout Container with a TextView inside it.

The TextView has a text property android:text=”@string/hello”. This means that there is a string resource called hello is defined in the values/strings.xml with the value “Hello World, MainActivity!”.

What if we want to reference this TextView from our class, we will add an id property to the TextView to be like this:

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

The id of any view in Android must be in this format “@+id/(id of the view)”

After we added the id property to the view, we reference it from the code like this:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TextView txt=(TextView)findViewById(R.id.txtHello);
        txt.setText("referenced from code");
    }

Run the application it should be like this:

Notice that we reference our TextView with the method findViewById(int Id).

The id of the TextView (txtHello) is strongly typed as it was added automatically to the R.java file and assigned an integer value:

public final class R {
    public static final class attr {
    }
    public static final class drawable {
        public static final int icon=0x7f020000;
    }
    public static final class id {
        public static final int txtHello=0x7f050000;
    }
    public static final class layout {
        public static final int main=0x7f030000;
    }
    public static final class string {
        public static final int app_name=0x7f040001;
        public static final int hello=0x7f040000;
    }
}

Note:

You do not to add views (controls) to your activity in the xml directly. You can switch to the layout view by pressing the layout tab in the main.xml file and choose the views from the toolbox.

There is also a nice UI generator tool called Droid Draw.

Summary:
The basic Android application with only one view consists of a class that extends the Activity class. This class preserves the activity frozen state (like viewstate in asp.net) and it displays the UI defined in xml file layout file or defined explicitly programmatically.

We hope you found this tutorial informative and look forward to bringing you our new series on Android app development.


16 Responses to “Introduction to Android App Development”

  1. Dee 10. Nov, 2010 at 5:02 pm #

    Excellent start to the android development series Mina.. lookin’ forward to the next post :)

    I just got an android phone and was playing with developing some widgets for personal use.

    Dee

  2. Mina Samy 11. Nov, 2010 at 1:26 am #

    Thanks
    I’m glad you liked it, Developing for Android is really interesting.

  3. Frank 13. Nov, 2010 at 6:07 am #

    Really nice intro to Android development. Good work Mina.

    Do you plan to run a series?

  4. Mina Samy 13. Nov, 2010 at 1:09 pm #

    Sure Frank, there gonna be more Android Dev articles

  5. Can 19. Nov, 2010 at 7:37 pm #

    Nice post thank you. Will help me much

  6. Brett Widmann 26. Feb, 2011 at 7:30 pm #

    App development is HUGE! Thanks for this helpful article.

  7. Supriya 06. Apr, 2011 at 1:16 am #

    Very helpful post..thanks a lot….

  8. JerryH 25. Jun, 2011 at 4:34 pm #

    Mina, Is the source code for your Mobile Orchard android tutorials available anywhere?

    I would love to do the tutorials, but I really don’t want to type in every bit of code.

    Thanks,
    Jerry

    • Mina 27. Jun, 2011 at 4:42 am #

      Hi Jerry, you can find some source code on some topics in my personal blog : http://android-pro.blogspot.com
      but sorry for the rest, you’ll have to copy/paste it.
      thanks

  9. Mark Turkel 14. Nov, 2011 at 12:32 pm #

    Good post…looking forward to more. :)

  10. Alex Design 05. Feb, 2012 at 2:26 pm #

    Great post on Android Development…..Looking forward to reading the rest of your blog.

  11. sai madan 04. Feb, 2013 at 12:35 pm #

    awesome kickstart

  12. It’s not my first time to visit this site, i am visiting this site dailly and
    get nice information from here daily.

Trackbacks/Pingbacks

  1. Android Entwicklung | medienproduzent.net - 10. Nov, 2010

    [...] Introduction to Android development [...]

  2. Weekly Design News – Resources, Tutorials and Freebies (N.64) | Android - 20. Nov, 2010

    [...] Introduction to Android development [...]

  3. Introduction to the Android Development World | What Is Android - 16. Oct, 2011

    [...] We are going to start a series of articles to introduce you to the world of Android development.Read MoreIntroduction to Android Development Mobiletuts iPhoneThis article will introduce you to the Android [...]

Leave a Reply

© 2008-2014 • Mobile Orchard and the Bar-Tree Logo are service marks. ContactAdvertise