Android Tutorial

Android Workshop

Android Details

Android is a framework that provides java programmers the ability to control different aspects of smart devices. This interaction happens through the Android SDK (Software Development Kit).

Model View Controller (MVC)

This is a programming paradigm that is used to help programmers organize large projects in common ways. As a result, Larger projects become more maintanable and incoming develpers can quickly learn about the project, as they will have an idea about how everything is generally organized. Models represent the raw information or access points to the raw information. Views represent the visual representation of the models. More generally they represent the layout of the information.

Your model, for example, can be a collection of menu items and prices (Strings and doubles). The view may be a scrollable list that has the menu item names on the left of each cell (list item) and the prices on the right. The view may offer other extra information such as dollar signs in front of each price.

Controllers connect the Views to the Models, they contain all the logic. Every event that is intercepted, such as a touch, is sent to its respective controller. The controller then decides, based on the input, which models to query. The information collected is then sent to the view which renders the visual representation of the models and the associated actions. The users actions are completed by the view being rendered

Consider the following example: A user taps on one of the menu items, the device collects the information and sends it to the controller, the controller now retrieves the Model for the information for that menu item. In this case the information contains the ingredients, the wine pairing and an image of the item (these may be several Models that have been queried to collect these results). The controller then packs the information up nicely for the view that will show this information to the user in an organized fashion.

Main idea (Guidelines)

The view is expensive to manage, so minimize the amount of work it does. The Model is expensive to retrieve so minimize the work it does. The controller does the majority of the work and is also responsible for minimizing the amount of work the model or view will have to do. The controller knows everything about the Models and very little about the Views. The views, in turn, know a lot about their controllers.

MVC with Android

Android attempts to use this by providing the user with resources and layouts (res folder) that are either raw data files, such as images, or layouts (in XML format). These are the views, they dictate exactly what the program will look like, how everything is laid out. Android follows the “Main idea” from above by doing absolutely no work in the views(ie. No loops, no procedural programming). The controllers are called activities, they interact with the views to create a user experience. The SDK triggers events in the activities. The activity collects the information from the models, builds the view and sets up the event triggers needed to accomplish the tasks. Models are left to the user's discretion, usually they are implemented as classes, it is recommended to build as many classes that have no ties to the activities as possible to provide modularity.

Threads

A thread is a process that is run on the CPU. When a program is started it is a thread/process. A program/thread/process can spawn and control other threads and processes to do tasks simultaneously.

For example: A thread can collect keyboard information, while another thread can allow the cursor to flicker. This can obviously be done in one thread if the programmer so wishes, but it is easier to let the operating system deal with running multiple threads at the same time and let the programmer code simpler programs. If threads were unavailable, and the programmer wanted to keep his program as simple, the cursor would stop blinking while we wait for the user to input information into the keyboard (this may cause the user to think the computer crashed).

The difficulty behind threads is that they are very hard to control and can cause many issues related to accessing variables simultaneously.

User Interface (UI) Thread

Because threads are so difficult to control accurately, Android (and most smart phones) have a UI thread that controls all user input and all drawing of objects. Android will not allow you to do these outside of the UI Thread. So DO NOT LOOP ENDLESSLY in the UI thread as it will cause the phone to stop responding.

Layouts and Views

Layouts are xml files that describe how everything will be laid out on the screen. The simplest layout tool is the LinearLayout this lets the items inside it be places horizontally or vertically, one after the other in the order in which they appear in the XML file. The simpler Views: An ImageView holds the images and a TextView holds the text. Note that everything must have layout_height and layout_width value in its opening XML tag. See the example below.

<?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">
	<ImageView  
		android:layout_width="wrap_content" 
		android:layout_height="wrap_content" 
		android:src="@drawable/kitty"/>
	<TextView  
		android:layout_width="wrap_content" 
		android:layout_height="wrap_content" 
		android:text="Kitten"/>
</LinearLayout>

Activity Life Cycle

The activity life cycle is important to know as it tells you what state your app is in, ie. Whether or not you have focus or if you are on the screen.

Tutorial

Instructions

Download, install and open eclipse

Select file/new

Choose Android Project

Enter a project name

Select a “Build Target” (android 2.2)

Fill in properties: “Package name” is something along the lines of com.android.myself, whatever you think is relevant.

Select create Activity and Give your Activity class a name, remember to use one word that starts with a Capitol letter

Click finish

You should see your new project in the navigator

If you do not see your navigator, click on window then show view then navigator.

Inside your project you will see:

bin – Binary files created during the compilation stage, these are not of relevance to you

gen – These files are generated so that your program can reference your resource files correctly from your code

src – This is where your code goes, notice that everything is found inside the package you named earlier.

res/layout – layouts that will describe your view

res/drawable-_dpi – images that will go into your views

In your package you will see the activity you created earlier, if you double click on it you will be able to edit it.

Extra Android Information:

The AVD manager is what runs the android environment. It holds all the emulators and SDKs. There is an AVD manager installed in eclipse that you can play with to try different emulators and sdks.

The devices provide a log of current activities, to view that log, simply connect your device to the computer via usb and run the following command:

adb -d logcat

adb: is the android debugger

-d: means that you are accessing a device, (-e means emulator)

logcat: says that you are want to see the device log

Full Tutorial (Source code)

The two downloads below were a part of a workshop that I conducted at the University of Toronto for Highschool teachers. They are set up in such a way that they show full circle how to build a simple app that collects models from the cloud and displays them using controllers and views.

Day 1 | Day 2 | Twitter Reader