Now that I have set my development environment it’s time to make my very first application. The very well known “Hello World!”.
For this I follow another one of TugaBest tutorials that he shared in AndroidPT Forum and that you can access it here.
The first thing that it is needed is a AVD (Android Virtual Device) to emulate a device. To create one, on window console,go to android-sdks directory and simply type :
>android list targets
A list of possible devices and android versions is presented to you. In my case, I’m going to chose one that is running android 2.2 (id = 1). So I type
>android create avd -t 1 -n my_avd_1 -a
(-t means target, -n means name, -a means to create snapshot)
say no to create a custom hardware profile. Because you are not doing any changes to the default profile.
Now it’s time to create a new project in Eclipse
File > New > Project > Android > Android Application Project.
The New Android Application Window opens and I filled all the fields.
- Application Name
- Project Name
- Package Name
- Minimum Required SDK (I chose API 8: Android 2.2 (Froyo))
- Target SDK (I chose API18: Android 4.3)
- Compile With (API18 Android 4.3)
Then I pressed Next.
On the next window I’ve checked:
- Create custom launcher icon
- Create activity
- Create Project in Workspace
And then Next again.
The next window lets me customize the logo for my application. After customization I pressed Next.
Its now time to create a new Activity.
An Activity is the simplest entity used to execute actions. The same application can have several activities. But the user can only interact with each one of a time.
I chose to start with a Blank Activity.
And then Next again.
Then finally I supplied:
- The Activity Name (HelloWolrd)
- Layout Name (activity_hello_world)
- Navigation Type (None)
And then pressed Finish.
A little more theory…
An Android Interface is a combination of object hierarchies. These objects are called Views and they are of drawable type. Each of these drawable objects/views is used as a layout element (such as, a button, an image or a label). And each type of element it’s a subclass of View. For example, the subclass of View that represents Text is called TextView.
In the image is listed all the steps needed to display the text “Hello, World!”.
First we need to create the subclass of view that represents the Text Element.
TextView tv = new TextView(this);
The input (this) of the constructor its an Android Context. A Context is an handler to the system. It stores services like resources, database access and settings. Each Activity and therefore each subclass of Activity inherits from Context.
After creating the object/view we need to set the actual text to be displayed. So we need to add the line:
In order to be seen in the monitor we need to set a UI for this object. That’s why we need to add the line:
Accordingly to the amazing tutorial posted by TugaBest in AndroidPT Forum. The only thing that it’s missing is to run the created app.
To do it so,
Go to Eclipse Menu Run > Run
On the next window (Run Configurations) select your app under Android Application and run it.