logo

1. Create a Project

Select File -> New -> Other -> Android -> Android Project and create the Android project “com.mobisys.android.first_project”. Enter the following.

The details here are somewhat important. The Project name can be whatever you like it to
be and will only be referenced in Eclipse. It should be Create a new project in the workspace

The Build Target is the next important bit. It should target the platform you intend to
develop for.
Android is backwards compatible in the running of applications (meaning you can run
something targeted for 1.5 on a handset running 2.1) but you should develop for the
lowest target you intend to support. Generally that will be 1.5 as that has the greatest
number of users at the moment and those with later versions will still be able to run
the application.

Next up is the Application name. This will be the name of the application as seen on the
device.

Package name is a Java convention and generally needs to have at least two words
(with periods between) and be unique (globally). The convention is that you use your
domain name in reverse followed by something unique to the application itself.

Last is the Activity. This is the name of the initial class that will be run or the
default “Activity” that will be launched.

2. Directory Structure

Press Finish. This should create the following directory structure.



Within the project you should see a “src” directory and inside there a “.java” file
for the particular Activity you created. Double click on the “.java” file(HelloActivity.java)
to bring up the code editor.

3. Activity Class

It should contain something like the following:


package com.mobisys.android.first_project;

import android.app.Activity;
import android.os.Bundle;

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

The top line “package” should be the package name you specified previously.
The two “import” statements bring in packages from the Android SDK that are used in this Activity.

The work begins with the public class HelloWorld extends Activity line. That is saying that this is a class named HelloWorld and it is derived from Activity (the base class). This means that this class has all of the properties and methods of the Activity class as well as what you might add to it.

You’ll notice that in HelloWorldclass, we are “Overriding” the onCreate function. This is generally our starting point for any Activity based class. Generally you always want to call super.onCreate(savedInstanceState) first as this tells the derived classes (the “super”) to
run their onCreate methods.

4. Layout

Next is the setContentView method call. This actually determines what will be displayed in the application. In this case, it is specifying R.layout.main.
R.layout.main can be found within the “gen” directory in our Eclipse project. It is auto-generated by Eclipse and not meant to be edited. We can look at it though to see what is going on.
You should see a portion something like this:

public static final class layout {
public static final int main=0x7f030000;
}

Seeing that main is an “int” within the “layout” class.
Behind the scenes Android is referencing our “layout” XML and making that be what is displayed.
In Android, in most cases, the UI (user interface) of

an application is written in XML.
To edit this XML, in Eclipse, go to res -> layout -> main.xml
You should see something that looks like this:

5. Graphical Layout & XML Code

The tabs at the bottom determine your view.You can switch between UI and XML by clicking on the tab on the lower part of the screen. As shown below:



If you click on “main.xml” you’ll see the actual XML as follows:


<?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">
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"/>
</LinearLayout>


6) Add UI Components

Select res/layout/main.xml and open the Android editor via a double-click. This editor allows you to create the UI via drag and drop or via the XML source code. You can switch between both representations via the tabs at the bottom of the editor. For changing the position and grouping elements you can use the outline view.


The default value is what you see on the button: “@+id/Button01” which is simply there to
help you know what it’s “id” is. You can edit the value directly and change it to whatever
you like. You can change the Button Text.For which, the Corresponding XMl is:

<Button android:text="Hello World" 
        android:id="@+id/Button01" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content">
</Button> 
 


7. Run Project

To start the Android Application, select your project, right click on it, Run-As-> Android Application Be patient, the emulator starts up very slow. You should get the following result.


8)Summary:

So we created our first android project i.e.Hello World & We learned the following things:
1. Create a Project
2. Directory Structure
3. Activity Class
4. Layout
5. Graphical Layout & XML Code
6. Add UI Components
7. Run Project

Tags:
AUTHOR: Vikas Hiran
No Comments

Sorry, the comment form is closed at this time.