Monday, January 7, 2013

My First Android Application (Hello World)


In the previous post I had written about “install android development environment on windows 7”. Today I’m going to create my first android application.

First launch the eclipse by double clicking on the eclipse.exe file located in the eclipse folder.

Then we have to choose the Workspace,this is the folder where we store our projects, I choose D:\Example as my Workspace.


Then click “OK” to launch eclipse.


To create new android project  
 
File ---> New ---> Android Project


Type the Project Name as “Hello World” and click Next.

In the next window some platform versions will appear, which were installed early. We need to choose a platform version from list to this project. I choose 2.3.3


Click “Next” to go next window.

In this window we need to give a package name. We need to have at least a period (.) in the package name. I type the package name as “niroshan.HelloWorld”.


Click “Finish” to close this window.

Now the Eclipse IDE is look like this.


In the Package Explorer (located on the left of the Eclipse IDE), Expand the Hello World project.

Double click “main.xml” file which is in the res/layout folder. This defines the User Interface of our project.

The default view is the layout view. To modify the UI, click the main.xml tab located at the bottom.


From here I’m going to start code writing. In this first application I’m going to add a Text view and a Button.  So I add the following code in bold to the  main.xml file.

<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns: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"/>

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello Niroshan, This is my first Android Application"/>

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This is a clickable Button"/>

</LinearLayout>


Press ctrl+s to save the changes.

Now it is the time to test our application. There are many ways to run android application in Eclipse IDE.

1. Select the project name in Eclipse and press F11

2. Select the project name in Eclipse ---> click “Run” in menu bar ---> Run As
     ---> Android Application

3. Right click on the project name in Eclipse ---> Run As ---> 
    Android Application

When I tried to run my project, an error message was appeared. 


That is, there is no compatible target. Because, This application’s (Hello World) target platform is 2.3.3, but we have not create Virtual Device for this version yet. We have only created Virtual Device for 2.1 in the previous post. So we need to create an Android Virtual Device for 2.3.3 version.

Click “Yes” to create Virtual Device.




Click “New” and fill as follows.



Click “Create AVD”


Now we have created another Virtual Device name Emulator_2.3.3

Then select Emulator_2.3.3 , click “Start” and click “Launch” to launch the Virtual Device.



Our application will run on the Emulator.


When we click the Home button, Home Screen will appear.



When we click the application launcher icon, it will display the installed applications on the device.  “Hello World” application also in here.


This is my first Android Application.





No comments:

Post a Comment