Let's play a bit with Android and create our first application, in this tutorial for beginners.
We can call this tutorial: Tuto 0.
You could find it on the Android Market.
Open Eclipse.
On the Package Explorer, right click and select:
New > Other > Android > Android Project > Next.
Write the Project Name: HelloWorld.
On the Build Target area, select the version of Android you would like to use.
We are selecting the Android 3.2 one for this example.
In the Package name, write: com.badprog.android.helloworld
Click Finish.
Open the your project just created in your Package Explorer.
HelloWorld > src > com.badprog.android.helloworld >HelloWorldActivity.java.
Inside you have this code generated automatically:
package com.badprog.android.helloworld; import android.app.Activity; import android.os.Bundle; public class HelloWorldActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } }
OK, let's try it on the Android Emulator!
On Eclipse, click:
Window > Android SDK and AVD Manager.
On the left, select Virtual devices.
Then click New on the right, a new window appears.
As a Name, write android_emulator_1 (spaces are not allowed!).
As a Target, select the mobile version you want to emulate, select the 3.2 for example to emulate a mobile phone with the Android version 3.2.
In the Skin area, select either Built-in and select HVGA or Resolution and write: 320 x 480.
Click Create AVD. (AVD means Android Virtual Device.)
Now on Eclipse, right click on your HelloWorld project, and Run As > Android Application.
A new window appears. This is your new Android emulator, with its name in the top bar of the window.
The logo of Android is displayed with a shiny reflect.
Be patient, it may take several minutes to load. This action has to be done only one time, so let's wait.
OK, the Android logo has disappeared and you can see the desktop of your Android mobile.
At bottom, there is a square composed with 16 tiny squares. Click it.
All applications available on your device are displayed, even ours, the famous HelloWorld.
Click it.
The logo of our application (the default one) appears with its name.
And of course, the sentence: Hello World, HelloWorldActivity!
Let's change this sentence.
Close your Android emulator.
In your HelloWorldActivity.java class, rewrite the code like this:
package com.badprog.android.helloworld; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class HelloWorldActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView myText = new TextView(this); myText.setText("Hello World, welcome on BadproG.com! :D"); setContentView(myText); } }
Now we already ran the application, Eclipse saved this configuration.
So, instead of right click on your project then click Run As > Android application, we will create a new configuration.
There is a Run icon on the Eclipse top bar (this is the green circle with a white triangle).
On its right, there is a tiny black triangle, click it and select Run Configurations.
On the Android Application area, select HelloWorld (or create a new one, if you do not have it).
On the right, click the Android tab and write the Name HelloWorld and the Project as well.
Click the Target tab, and select the emulator you just created before, named android_emulator_1.
Click Apply and Run.
The Android emulator will be launched.
Delock the device, by left clicking the cadenace icon from the left to the right.
If you do not see your HelloWorld application, open the application menu on the device, and select it.
Now the sentence is: Hello World, welcome on BadproG.com! :D
To finish, let's change again the sentence, without closing the emulator, by:
Hello World, it is a great day today, I made my first Android application!
Save your HelloAndroidActivity.java file and reclick on the Run icon, on the Eclipse top bar.
This time, the device emulator is still running and your application has been updated!
If you want to come back to the main menu, press ESC key.
Great job you made it!
Add new comment