HELLO WORLD ON ANDROID
Pattern
Hello readers! Today we will create our first project for Android . How could it not be, let's make the famous Hello World . So, without further ado, let's put our hands in the dough.
The first step is to create a new project. Open Eclipse and go to the File -> New -> Other ...menu and look for the Android section , where you should select Android Project .
Select Next> and fill in the HelloWorld project name (no spaces) and click Next> again. In the next screen, select the version of Android that you intend to use. In the last post we created a device running version 2.2, so I will select it to use (use a version that you have created a device with the corresponding version). Click Next> again.
In the next screen, we will define the name of the application and the package that we will use. The name of the application will put Hello World and the package I will put as net.rafaeltoledo.helloworld (but you can put what you want). To finish, click Finish .
Ready! We have already created our project. The two most important files for us at the moment are HelloWorldActivity.java which is inside the src and main.xml paster which is inside res / layout . Basically, the main.xml file is responsible for defining the layout (objects such as buttons, fields, selectors, etc.) and their layout. Meanwhile, the HelloWorldActivity.java file is where we put the logic of the application itself (with selections, loopings , etc). Let's take a look at the content generated by the two files:
1
2
3
4
5
6
7
8
9
10
11
12
| <? xml version = "1.0" encoding = "utf-8" ?> 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 > |
1
2
3
4
5
6
7
8
9
10
11
12
13
| package net.rafaeltoledo.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); } } |
If the editor is displaying the graphic editor when opening the main.xml file , simply change its view by clicking on the editor's footer, as shown in the following figure.
Let's start by looking at the main.xml file . In it we have the first line defining the XML file (this line will always be present). Next, we have a layout definition , in this case the LinearLayout , which indicates that the items will be inserted one ahead of the other. Within this definition (nested internally), we have a TextView element , with a string with the hello content (defined in the android: text attribute ). Let's modify your content for Hello World, Android! www.rafaeltoledo.net . That way, the line looks like this:
1
| android:text="Hello World, Android! www.rafaeltoledo.net" /> |
In the HelloWorldActivity.java file , we have the assignment of the layout defined in the main.xml file to the current instance, within the onCreate () method calling the setContentView () method .
With this, we can run the application. To do this, just right click on the project and select Run As -> Android Application .
With this, the emulator will open, starting the virtual device that has the system compatible with the version of the application (in our case, Android 2.2). Do not worry if the emulator is slow to open (it really is kind of slow). If the screen becomes too large (larger than the monitor screen itself), return to the AVD Manager and edit the resolution.
Nenhum comentário:
Postar um comentário