Monday, August 19, 2013

Getting Started with Mobility, Part 3: Native Android

In this series, we're introducing mobile development by looking at a variety of platforms. In Part 1, we provided an overview of the mobile landscape, and in Part 2 we explored Native iOS development. Here in Part 3, we'll introduce you to native development for Android phones and tablets.

Android, the Market Leader
Android from Google currently has the largest portion of the mobility market, which automatically makes it an important platform to develop for; Google bills it as "the most popular mobile platform in the world." Unlike Apple, which controls both the hardware and software, Google provides the Android platform and works with multiple manufacturers and carriers. This is why there is a multitude of phones and tablets available for Android.

Android: Variety, Variety, Variety

The Computer You'll Develop on: A Mac or a PC
One of the nice things about Android development is how approachable it is: whereas other mobile platforms force you to develop on a Mac (iOS) or a (Windows Phone, Windows 8), you can develop Android apps using either a Mac or a PC.

Developer Tools: Eclipse, IntelliJ, or Android Studio
The longtime-favorite for Android development has been Eclipse, and most of the tutorials you'll find online assume Eclipse. However, IntelliJ has been steadily displacing Eclipse as a favored IDE--and IntelliJ Community Edition is free. Moreover, the future of Android development is "Android Studio", based on IntelliJ CE and available now in an "Early Access Preview". Android Studio is what I use.

Whatever IDE you choose, you'll need to install it--and that's going to involve obtaining a number of software bits, installing them, and getting them to reference each other. Because Android development is based on Java, you'll need to install Java from Oracle. Then you'll need the Android Developer Kit (ADK) from developer.android.com. Lastly, you'll need your IDE.

Android Studio IDE

Programming: XML, Java, and Android APIs
To develop for Android, you will work in XML, Java, and the Android APIs.

In Android projects you define many things in XML, including your application manifest, layouts, menus, and resources. Below is a sample Android screen layout, containing a text field and a button.

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"&gt;
     <EditText

        android:id="@+id/edit_message"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:hint="@string/edit_message" />
    <Button

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_send" />


You will also write code in Java. The choice of Java is one of the things that makes Android easy to learn: many developers already know Java, or a similar language like C#. Below is an example of the Java code for an Android activity (view).

package davidpallmann.tutorial01;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.content.Intent;
import android.widget.EditText;

public class DisplayMessageActivity extends Activity {

   
@SuppressLint("NewApi")
   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView
(R.layout.activity_display_message);

       
// Make sure we're running on Honeycomb or higher to use ActionBar APIs
       
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
           
// Show the Up button in the action bar.
            getActionBar
().setDisplayHomeAsUpEnabled(true);
       
}
   
}

   
@Override
   
public boolean onOptionsItemSelected(MenuItem item) {
       
switch (item.getItemId()) {
       
case android.R.id.home:
           
NavUtils.navigateUpFromSameTask(this);
           
return true;
       
}
       
return super.onOptionsItemSelected(item);
   
}
}

Some things you'll deal with in Android development include the following:
  • Namespace: having to know the right namespaces to reference in your Java code.
  • Choice of tools: there's more than one IDE out there, so tutorials and guidance don't always match the tool you're using.
  • Immense variety: dealing with a large variety of devices and dimensions.
  • Versioning: deciding how much to support in terms of older Android devices.

Testing Your Apps: the Android Emulator
You can test your apps on the Android Emulator and/or connect an actual device to your computer. The Android emulator is a bit slow, so you'll need patience to use it. The emulator supports a variety of devices, and you can set up multiple configurations for a mix of devices (for example, several different phones and tablets).

Android Emulator

To connect an actual device to your computer, you'll need to set up your device for development, which involves setting it to enable USB debugging.

Online Resources
The Android developer center at http://develop.android.com is nicely set up, with design and develop resources such as guidance, training, tools downloads, and documentation.

Android Developer Center
 
Key areas to visit:
Design Principles
Developer Training
API Guides
Developer Tools
Samples

Tutorials
The Training section on developer.android.com has good, attractive materials. However, you'll find the "tutorials" here to be a mixed bag: some of them give you step-by-step instructions and have downloadable code, while others are more like "how to guides" that don't specify exact steps and lack complete code listings or downloads. Accordingly, you'll find them to be useful but will almost certainly want to supplement with additional learning materials. If you prefer video tutorials to written tutorials, you might check out the Android learning materials from Treehouse.

1. Building Your First App

This first of the tutorials on developer.android.com will walk you through creating a simple project with a text box and button, that passes on the text you enter to a second screen. It's a simple but effective introduction to Android development that will give you exposure to XML layout, Java code-behind classes, Intents (action messages), and string resources. Time: 1-2 hours.

Building Your First App Tutorial

The follow-on training topics at developer.android.com discuss how to augment this simple app in a number of ways.


2. Android 4.2 App Development

This book, available in hardcopy and Kindle form, is a great way to learn Android development. One of it's many useful tutorials is on the Master-Detail flow project template, which is also available online at at Techotopia. It will acquaint you with the Master-Detail Android project template and have you customize it to track web sites. One of the nice things about this project template is that it uses an alternate layout for phones vs. tablets to take advantage of available screen real-estate intelligently.

Master-Detail Project Template
 
By the time you're finished with the tutorial, you'll have modified the item data model and the detail view will display web content.
 
Master-Detail Flow Tutorial
 
3. Capturing Photos Tutorial

In this tutorial you develop the ability to take pictures (and video) using the camera. It includes downloadable source code. You'll first learn how to request camera permissions, take a photo, view the photo, save the photo, add the photo to a gallery, and decode a scaled image. Then you'll move on to recording and viewing video. Lastly you'll learn how to directly control the camera.

Tutorials and samples are great for learning and getting started, but you'll ultimately want to move past looking at someone else's work and develop your own apps.

Summary: Native Android Development
Android is one of the key mobile platforms you will want to target due to its strong position in the market. It is also one of the easier platforms to get started with, because of the availability of tools for computers (Mac, PC) and its use of the well-known Java programming language. Like any mobile platform, however, it will take an ongoing investment to learn its conventions and APIs well.

Next: Getting Started with Mobility, Part 4: Native Windows Phone

1 comment: