Introduction to the Android development

Introduction to the Android development

963
0

What does android mean?

Android is an open source mobile OS for smartphones, tablets, TVs and gaming consoles. Originally designed by Android Inc then acquired by Google in 2005.

Android development 840x420

To start Android programmation, you must first install the Android development kit and understand the basics of this programming language. Then we will make our first app for Android which is the well-known “Hello World” to comprehend these basics.

Basics of an Android application

An Android application contains 4 basic elements:

Activities

An activity is the main component for an Android application. It represents the implementation and interactions of your interfaces.

Taking the example of an application that lists all the images in your phone, the project could be decomposed as below:

  • A layout to show the list of images
  • An activity to manage the filling and the display of the list
  • If we want to add more or delete images, we need to add other activities

Services

A service, unlike an activity, isn’t related to the layout but it allows the execution of an algorithm for an indefinite period of time. It will only stop when the task is finished or its execution is stopped.

It can be executed at different timings:

  • When starting the smartphone
  • At the moment of an event (incoming call, SMS, email, etc …)
  • The launch of an application
  • A specific action in your application

Broadcast and Intent Receivers

A Broadcast Receiver like its name suggests allows you to identify what is happening on the system or on your application and trigger an action that you already preset. It is often through this mechanism that the services are launched.

Content providers

The “content providers” are used to access data from your application. They can access:

  • Contacts stored in the phone
  • The agenda
  • Pictures
  • Other data from your applications

Life Cycle of an Android application

Android activity lifecycle

onCreate

This action is called for the creation of your activity. It is used to initialize your activity as well as all the necessary data to the latter.

When the action onCreate is called, it receives a Bundle as argument. This Bundle contains a backup status recorded during the last execution of your activity.

onStart

This action is meant for the beginning of the execution of your activity (beginning of the passage in the foreground).
If your activity can not go in the foreground for whatever reason, it will be transferred to onStop.

onResume

This action is called after onStart (when your application returns to the foreground). At the end of the call of the onResume action your application is in the foreground and it receives user interactions.

onPause

If another activity is implanted in the foreground, the onPause action is called in order to save the state of the activity and the various treatments performed by the user.

At this stage, your activity no longer has access to the screen, you will be prohibited from performing any activities connected with the user interaction (unsubscribing listeners). However you can continue to execute the necessary algorithms but only the ones who do not consume too much CPU.

onStop

Called when the activity is no longer visible whatever the reason. Through this action you can stop all treatments and services performed by your application.

onDestroy

Called when your application is fully closed (Process Complete). Any unsaved data is lost.

Install your Android development kit

Installing the Android SDK

  1. To get started go to the following link:
  2. http://developer.android.com/sdk/index.html and download the SDK version that suits your OS. (We will be on a Mac).
  3. Once the SDK is downloaded, go to the folder of the zip file and extract it to a folder of your choice.
  4. Install Eclipse: http://www.eclipse.org/downloads/, (Download a version including Java, either the version for Java developers or the one for J2EE developers), Our version of Android Eclipse for this tutorial: 3.6.1 Helios.
  5. You should also install JDK (Java Development Kit) and JRE (Java Runtime Environment), link: http://www.oracle.com/technetwork/java/javase/downloads/index.html.
  6. Now, install the ADT plugin (Android Development Tools) to integrate the Android development softwares in Eclipse.
  7. Launch your Eclipse Go to the “Help and Install New Software” menu.
  8. In the “Available Software”, click “Add”.
  9. Add the name of the site (“ADT plugin” for example). In the tab add: https://dl-ssl.google.com/android/eclipse/, then click “OK”.
  10. Come back to “Available Software”, you should see “Developer Tools”. Select it and press “Next”.
  11. Click “Next” to read and accept the license then click “Finish”.
  12. To complete the installation restart Eclipse.

Your Android development kit is now ready to use.

Setting up your development environment

You may have noticed that six small icons have appeared in the top menu of the Eclipse:

Eclipse icons

  1. Will launch the SDK Manager to update the different SDK tools or install new versions.
  2. Allows launching a tool to manage the various Android emulators.
  3. Allows you to use the Android Lint tool.
  4. Allows you to create a new Android project.
  5. Creates a new Android test project.
  6. Create an Android XML file.

Click the first icon (SDK Manager), a new window will appear:

Android SDK

This screen will allow you to:

  • Install new version of the SDK or new tools.
  • Update versions or tools.

To begin, install the contents of Android 4.0.3 and 4.1.

The second icon allows you to create and manage emulators:

Android Virtual Device Manager

To create a new emulator, click the “New”

  • Name: The name of your emulator (no spaces).
  • Target: Version of the Android SDK emulator.
  • SD Card (Optional): Configure the SD Card (Size … etc).
  • Skins: Choose the size and resolution of your emulator. Preconfigured emulators are in the Built-in part.
  • Hardware: This section allows you to add to your specific hardware emulator. For example you can add a GPS, keyboard configuration, accelerometer, etc …

The emulator will now appear in the list of available emulators.

Now we will move on to the most interesting part 😉

My first Android application

Creating the Project “Hello World”

In Eclipse, click the icon to create a new project (presented in the previous section).

The project creation is carried out by following these three steps:

  • The first specifies the name of the application (Application Name), project name (Project Name – displayed only in Eclipse), the name of the package (Package Name – used as an identifier of the application on Google Play).
  • Chose the version of the SDK used to develop the application and the minimum version to run the application properly.
  • You can also create the icon of your application in the next screen by leaving the box “Create Custom Icon Launcher” checked.

New Android project

Then click “Next”, the next screen allows you to create the application icon:

Android icon creation

Then click “Next” to arrive on the screen allowing you to choose a starting template for your application. Choose “Blank activity” to create a new, empty activity, then click “Next”.

The last screen lets you configure the specific characteristics of the activity that will be created:

  • The activity name
  • Name of the layout representing the activity
  • Type of the navigation chosen in the application (Tabulation, Tabulation + Scroll, Scroll with a title on each page – Example of Google Play, Dropdown on the action bar – Example of the Gmail application)
  • The parent class of the activity to create (default Activity)
  • Title of thr activity

Android New Activity

Then click “Finish” to complete. The “Hello World” will now appear in the eclipse tree.

Explaining the project tree

This is the result of the created project and the tree of the latter:

Android Project tree

  • src: This directory contains the sources of your application (Java code) and packages.
    • com.tuto.android.helloworld: a web application package. Of course you can have multiple packages in your application.
    • HelloWorldActivity.java: Our main activity.
  • gen: Folder that will contain the R.java (This file is automatically generated from the software resources) and BuildConfig.java (to manage the logs display only during the phases of development and testing, not in the final version. This reduces the interaction between your application and the device) files.
    • R.java: This file is automatically generated by the Android SDK each pre-compilation.
    • BuildConfig.java: Will be updated automatically when creating the final version of the .apk to deactivates the logs.
  • assets: Contains non-internationalized data to be used in your application (images, videos, license … etc).
  • res: This is the folder to contain your application resources (images, videos, styles).
    • drawable-xxx: Contains your images in various resolutions (low, medium, high and very high).
    • ic_launcher.png: The icon for your application.
    • layout-xxx: The Android SDK provides a technique to create graphical interfaces using XML files. It is in this folder where you include all the files describing your interfaces. You can create other folders to menus for example. You can create different folders depending on the orientation, size of the screen and the version of Android … etc.
    • activity_hello_world.xml: The main file of your interface.
    • values: This folder contains a set of files that describe the values used by your application. We can, for example, put the strings (strings.xml), tables (arrays.xml), colors, … You can create different folders depending on the orientation, size of the screen (values-large) and the version of Android (values-v11 and values-v14) … etc.
    • strings.xml: A file that contains your declarations of strings.
    • dimens.xml: Contains all the dimensions used in your application. These dimensions will be overloaded for widescreens (values-large) to take into account the available space.
    • styles.xml: Represents the default theme used by your application. The theme will be skipped for devices running with Android 3.0 (values-v11) and 4.0 (values-v14) in order to take into account the new Holo theme.
  • AndroidManifest.xml: Defines the behavior of your application toward the Android system. This file defines for example the name, the icon, the min version of the SDK, activities, services, etc …

“Hello World!”

The sample project created by eclipse will show a “Hello World!”. You can run it as an Android application (For that, you simply right-click on the project and select “Run As -> Android Application”) and the emulator should start. The emulator will take some time to launch the first time (do not close it between your various modifications).

The project launch will give the following results:

On Android 2.3 devices:

Hello world 2.3

On Android 4.02 devices (and up):

Hello world 4.03

You may notice that the style of the application is different between the two devices (no action bar) this is due to the overloading of the theme for the versions 11 and 14.

Our “Hello World” is functional but we still need to understand it. Let’s see the code to understand what is happening.

  • AndroidManifest.xml

AndroidManifest

  • The “manifest” tag on line two contains multiple arguments, the most important is “package”, which gives the name of the package of your main activity. The other two attributes represent the version of the application code (versionCode – not visible to the user) and the version of the application (versionName – visible on Google Play).
  • The tag “application” is used for the declaration of various properties used by your application:
    • android: icon: The location where stored the icon of your application.
    • android: label: The name of your application (located in strings.xml).
    • android:theme=”@style/AppTheme”: Specify the theme used by your application. This theme points to the AppTheme style declared in the file styles.xml
  • The “activity” tag is used to declare an activity, each new activity we must put that tag.
    • android:name: the name of the Java class that represents the activity. The name must begin with a “.” and we do not put the .java in the end.
    • android:label: the label of the activity in question.
    • intent-filter: to specify an action.
  • strings.xml

strings

  • dimens.xml

dimens

  • Represents the size of the different internal margins used in the application.
  • These margins are used for large screens in the values-large folder.

resources

  • styles.xml

styles

  • Indicates the theme applied by the application.
  • By default, the applied theme corresponds to the theme “Light” of Android.
  • The default theme is skipped for versions 3.0 and 4.0 of android in order to use different “Holo” themes introduced in these versions (you may notice the presence of an action bar in those themes which explains its presence at the execution on a device running under Android 4).

DarkAction

  • activity_hello_world.xml

activity_hello_world.xml_

  • We dispose of two display modes.
    • Graphical Layout Tab: Visualisation mode and editing interface.
    • activity_hello_world.xml tab: source code mode.
  • It begins with a tag pointing the layout: here RelativeLayout. Here a link for the different layouts: http://developer.android.com/reference/android/view/ViewGroup.html
  • We declare a TextView component to display text and indixate it the string to display: @string/hello_world, so the variable hello_world is declared in strings.xml.
  • The text is centered vertically and horizontally.
  • We use the internal declared paddings in the file dimens.xml.
  • Each element must have a width corresponding to the size of its contents (wrap_content).

 

  • HelloWorldActivity.java

helloWorldActivity

  • Our main activity must inherit the class “Activity” or a subclass of it. See this link for more: http://developer.android.com/reference/android/app/Activity.html
  • We simply call the OnCreate of the superclass and then we initialize the layout using the setContentView method. This method takes in consideration the interface to load (here R.layout.activity_hello_word).
  • Whenever you see “R”, it means that you use a code that was generated by the various xml files.
  • “R.layout”: We will seek the layout expressed in the file called activity_hello_word.xml

 

  • R.java

R.java

You must not touch this file, it is automatically generated from your files located in the resources file (res).

Notice That all the variables declared in strings.xml are present, the interface is also declared in main.xml (which explains the use of the line R.layout.main in HelloWorldActivity.java) as well as the app icon.

Conclusion

It’s enough for this first tutorial, the Android development world is vast and needs a lot of practice and patience to be mastered. So better try small apps before jumping into complex ones.
If you are a new developer making his first Android app, what is the most difficult part in your project?