MY mENU


Saturday 22 September 2012

Creating an Android Project

An Android project contains all the files that comprise the source code for your Android app. The Android SDK tools make it easy to start a new Android project with a set of default project directories and files.

This lesson shows how to create a new project either using Eclipse (with the ADT plugin) or using the SDK tools from a command line.
Note: You should already have the Android SDK installed, and if you're using Eclipse, you should also have the ADT plugininstalled (version 20.0.0 or higher). If you don't have these, follow the guide to Installing the Android SDK before you start this lesson.

Create a Project with Eclipse


  1. In Eclipse, click New Android App Project  in the toolbar. (If you don’t see this button, then you have not installed the ADT plugin—see Installing the Eclipse Plugin.)
  2. Figure 1. The New Android App Project wizard in Eclipse.
  3. Fill in the form that appears:
    • Application Name is the app name that appears to users. For this project, use "My First App."
    • Project Name is the name of your project directory and the name visible in Eclipse.
    • Package Name is the package namespace for your app (following the same rules as packages in the Java programming language). Your package name must be unique across all packages installed on the Android system. For this reason, it's generally best if you use a name that begins with the reverse domain name of your organization or publisher entity. For this project, you can use something like "com.example.myfirstapp." However, you cannot publish your app on Google Play using the "com.example" namespace.
    • Build SDK is the platform version against which you will compile your app. By default, this is set to the latest version of Android available in your SDK. (It should be Android 4.1 or greater; if you don't have such a version available, you must install one using the SDK Manager). You can still build your app to support older versions, but setting the build target to the latest version allows you to enable new features and optimize your app for a great user experience on the latest devices.
    • Minimum Required SDK is the lowest version of Android that your app supports. To support as many devices as possible, you should set this to the lowest version available that allows your app to provide its core feature set. If any feature of your app is possible only on newer versions of Android and it's not critical to the app's core feature set, you can enable the feature only when running on the versions that support it.
      Leave this set to the default value for this project.
    Click Next.
  4. The following screen provides tools to help you create a launcher icon for your app.
    You can customize an icon in several ways and the tool generates an icon for all screen densities. Before you publish your app, you should be sure your icon meets the specifications defined in the Iconography design guide.
    Click Next.
  5. Now you can select an activity template from which to begin building your app.
    For this project, select BlankActivity and click Next.
  6. Leave all the details for the activity in their default state and click Finish.
Your Android project is now set up with some default files and you’re ready to begin building the app. Continue to the next lesson.

Create a Project with Command Line Tools


If you're not using the Eclipse IDE with the ADT plugin, you can instead create your project using the SDK tools from a command line:
  1. Change directories into the Android SDK’s tools/ path.
  2. Execute:
    android list targets
    This prints a list of the available Android platforms that you’ve downloaded for your SDK. Find the platform against which you want to compile your app. Make a note of the target id. We recommend that you select the highest version possible. You can still build your app to support older versions, but setting the build target to the latest version allows you to optimize your app for the latest devices.
    If you don't see any targets listed, you need to install some using the Android SDK Manager tool. See Adding Platforms and Packages.
  3. Execute:
    android create project --target  --name MyFirstApp \
    --path /MyFirstApp --activity MainActivity \
    --package com.example.myfirstapp
    
    Replace  with an id from the list of targets (from the previous step) and replace  with the location in which you want to save your Android projects.
Your Android project is now set up with several default configurations and you’re ready to begin building the app. Continue to the next lesson.
Tip: Add the platform-tools/ as well as the tools/ directory to your PATH environment variable.

A small Intro About Java To Do Android Programmers


What is Java?

Android applications are developed using the Java language. As of now, that’s really your only option for native applications. Java is a very popular programming language developed by Sun Microsystems (now owned by Oracle). Developed long after C and C++, Java incorporates many of the powerful features of those powerful languages while addressing some of their drawbacks. Still, programming languages are only as powerful as their libraries. These libraries exist to help developers build applications.
Some of the Java’s important core features are:
  • It’s easy to learn and understand
  • It’s designed to be platform-independent and secure, using
    virtual machines
  • It’s object-oriented
Android relies heavily on these Java fundamentals. The Android SDK includes many standard Java libraries (data structure libraries, math libraries, graphics libraries, networking libraries and everything else you could want) as well as special Android libraries that will help you develop awesome Android applications.

Why is Java Easy to Learn?

Java is easy to learn for a variety of reasons. There’s certainly no shortage of Java resources out there to help you learn the language, including websites, tutorials, books, and classes. Java is one of the most widely discussed, taught, and used programming languages on the planet. It’s used for many different types of programming projects, no matter their scale, from web applications to desktop applications to mobile applications.
If you’re coming from a traditional programming background like C or C++, you’ll find Java syntax quite similar. If you’re not, then take comfort in knowing that you’ve chosen one of the easiest languages to learn. You’ll be up and running in no time at all.
Finally, Java is one of the most human-readable languages out there, by which we mean that a person who knows nothing about programming can often look at some Java code and have at least an inkling what it’s doing. Consider the following example:
  1. char character = 'a';  
  2. if(character=='a')  
  3. {  
  4.     doSomething();  
  5. else {  
  6.     doSomethingElse();  
  7. }  
If you simply read the code aloud, you can pretty much tell that this snippet of code is doing. There’s a single letter variable called character. If the character variable equals the letter a, then we do something (call the doSomething() method), otherwise we do something else (by calling the doSomethingElse() method).

Why is Platform Independence Important?

With many programming languages, you need to use a compiler to reduce your code down into machine language that the device can understand. While this is well and good, different devices use different machine languages. This means that you might need to compile your applications for each different device or machine language—in other words, your code isn’t very portable. This is not the case with Java. The Java compilers convert your code from human readable Java source files to something called “bytecode” in the Java world. These are interpreted by a Java Virtual Machine, which operates much like a physical CPU might operate on machine code, to actually execute the compiled code. Although it might seem like this is inefficient, much effort has been put into making this process very fast and efficient. These efforts have paid off in that Java performance in generally second only to C/C++ in common language performance comparisons.
Android applications run in a special virtual machine called the Dalvik VM. While the details of this VM are unimportant to the average developer, it can be helpful to think of the Dalvik VM as a bubble in which your Android application runs, allowing you to not have to worry about whether the device is a Motorola Droid, an HTC Evo, or the latest toaster running Android. You don’t care so long as the device is Dalvik VM friendly—and that’s the device manufacturer’s job to implement, not yours.

Why is Java Secure?

Let’s take this bubble idea a bit further. Because Java applications run within the bubble that is a virtual machine, they are isolated from the underlying device hardware. Therefore, a virtual machine can encapsulate, contain, and manage code execution in a safe manner compared to languages that operate in machine code directly. The Android platform takes things a step further. Each Android application runs on the (Linux-based) operating system using a different user account and in its own instance of the Dalvik VM. Android applications are closely monitored by the operating system and shut down if they don’t play nice (e.g. use too much processing power, become unresponsive, waste resources, etc.). Therefore, it’s important to develop applications that are stable and responsive. Applications can communicate with one another using well-defined protocols.

Compiling Your Code

Like many languages, Java is still a compiled language even though it doesn’t compile all the way down to machine code. This means you, the developer, need to compile your Android projects and package them up to deploy onto devices. The Eclipse development environment (used with the Android Development plug-in) makes this pretty painless. In Eclipse, automatic compilation is often turned on by default. This means that every time you save a project file, Eclipse recompiles the changes for your application package. You immediately see compile errors. Eclipse also interprets Java as you type, providing handy code coloring and formatting as well as showing many types of errors as you go. Often, you can click on the error and have Eclipse automatically fix a typo, or add an import statement, or provide a method stub for you, saving lots of typing.
You can still manually compile your code if you so desire. Within Eclipse, you’ll find the Build settings under the project menu. If you have “Build Automatically” turned on, you can still choose the “Clean…” option that will allow you to do full rebuild of all files. If “Build Automatically” is turned off, “Build All” and “Build Project” menu options are enabled. “Build All” means to build all of the projects in the workspace. You can have many projects in an Eclipse workspace.
The build process, for regular Java projects, results in a file with the extension of JAR – Java ARchive. Android applications take JAR files and package them for deployment on devices as Android PacKage files with an extension .apk. These formats not only include your compiled Java code, but also any other resources, such as strings, images, or sound files, that your application requires to run as well as the Application Manifest file, AndroidManifest.xml. The Android Manifest file is a file required by all Android applications, which you use to define configuration details about your app.

Expected Error in Java


. . . expected
The syntax of JAVA is very specific about the required punctuation. This error occurs,
for example, if you forget a semicolon at the end of a statement or don’t balance parentheses:
if (i > j // Error, unbalanced parentheses
max = i // Error, missing semicolon
else
max = j;
Unfortunately, this syntax error is not necessarily caught precisely at the point of the
mistake so you must carefully check the preceding characters in the line or even in a
previous line in order to find the problem.
ECLIPSE:
Syntax error, insert ") Statement" to complete IfStatement.
Syntax error, insert ";" to complete Statement
ECLIPSE is more informative as to the precise syntax error encountered.