Monday, January 2, 2012

Creating your first android application – Hello Android



It’s easy and simple to create android application using eclipse. To begin with,

1.       Open eclipse.

2.       Click on file - > new project, you should see options something similar to the image.



If not don’t panic, it doesn’t mean you can’t create android project. Select Other… from the list or just press Ctrl+N, eclipse opens a new project creation wizard.
New Project Wizard
Expand Android , select Android Project option and click Next                                                                                                         
3.       Enter name of the project say, HelloAndroid  and leave the remaining options untouched. Once you are done with giving name to project click Next.                                          

wizard - project name


4.       Now in the list of android SDKs available, select the android sdk you want to target your project and click Next.

wizard - target android sdk


5.      In this final stage of project creation, eclipse asks you to give a package name for your project. Give your  package name, for ex, to my project am gonna use “com.coffeewithtechie.helloandroid” as package name. Leave the default launcher activity name and Minimum SDK, which by default is your target build version  (you can always change by selecting one from the dropdown), untouched.Once you are done with your changes click on Finish. Eclipse creates a project for you in its workspace.

Note  : Do not create any test project at this time.

wizard - final screen, package name
  

Click expand your project in project explorer, each project you create will have default folders and files necessary for the android project.

android project - folder structure

Src –  all your source code goes into src folder.

Gen – contains system generated R.java file, which holds id’s of all your project resources.

Assests – contains files and folders which are part of your project but not used as embedded resources. Android doesn’t generate any id for the asset folder contents. It behaves like a file system, they can be iterated over, listed and discovered just like files. You have to use path to access files in assets folder or AssetManager is used to get list of files and folders in assets folder.

Bin – contains class file, dex file, compressed resource file and generated apk file. Your application will be compiled into apk file and it is uploaded in market place for distribution so that users can download and install your application.

Res – all your application resources will be dumped in this folder, this is further divided to different type of resources. All image resources will go into drawable folders, applications UI will go into layout folder (layout defines how your app should look to user), color values, text values goes into values folder. An id will be generated for whatever included in resources folder in R.java file, so that it can be referred in code using id.

AndroidManifest.xml – it is a configuration file for the project, all project wide settings are done in this file, features and permissions required are specified in this file. It also specifies min and max sdk version to support, targeting sdk, supporting device types, version name, version code etc.. an entire post will be dedicated for configuring manifest file.

Proguard.cfg – if your application uses obfuscation, android provides code obfuscator proguard to use for obfuscation. Any configuration specific to obfuscation goes into this file, and this file will be referred from default.properties file

Project.properties – it’s a system generated file and will be modified whenever you do changes to your project in eclipse say, adding a project reference, jar reference etc.

Whenever a project is created, eclipse creates launcher activity, if specified, and a layout file used by launcher activity.

HelloAndroidActivity.java
package com.coffeewithtechie.helloandroid;
import android.app.Activity;
import android.os.Bundle;

public class HelloAndroidActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main); // main layout set as default for launcher activity
    }
}

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

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

LinearLayout>



All the String resources goes into String.xml file. Double click Open strings.xml file in values folder under res. Select the resource element hello(String). Edit its value in the right side of the designer to “Hello Android!” and save (Ctrl+s). you can also edit the xml file directly (if you are comfortable editing xml file), to do so, click on tab strings.xml next to Resources tab and edit xml values. Refer fig below to find string.xml tab.
String.xml 
Now, it’s time to run our project.
Right click on project -> point to Rus as and select Android application



If there are no emulators running, eclipse starts a new emulator which is compatible with the project if any exists, else you need to create an emulator before running the project. check out this link To know how to create emulator, http://coffeewithtechie.blogspot.com/2011/12/creating-and-running-android-emulator.html .
If everything goes right, project gets deployed into emulator successfully and you can see the output similar to the image.

Emulator - Project output

Wait for the series of posts on using different types of views and layouts in android.





No comments:

Post a Comment