Now that it has been explained how to install Eclipse and Android, we will begin to deal with the creation of applications on Android.
The creation of a application on Android can be divided into several steps.
Step 1 - Create the Android project on Eclipse
To create a application on Android, we first need to create a programming project for this application. The image below demonstrates this task:
Then, choose the option "Android Application Project", as shown below.
Then a new window will appear where you will type the project name. AndroidExample will be the name for this project. Notice that as we fill the project name, the text box "Package Name" is filled in with the name of the default package of the project.
NOTE: A package is a folder structure where the project files will be separated. This will be more detailed below.
Note also that the text box "Build SDK" has the value "Android 2.3.3 (API 10)", and the text box "Minimum SDK Required" has the value "API 8: Android 2.2 (Froyo)". This means that the minimum supported version for this application is the 2.2, and the recommended is the 2.3.3. Thus, this program WON'T RUN on devices with lower versions than the 2.2 (as version 1.5 - Cupcake, 1.6 - Donut, and 2.1 - Eclair).
The next screen in the creation of the project is shown below. Here, we have the layout option for the screens of the program we're creating. In this case, we will use the "BlankActivity".
At the end of this process, our project will be created:
The contents of each folder of our project will be explained below, in the next step.
Step 2 - Understanding the structure of our project in Android
An application projetc for Android has a rigidly defined structure. It is divided into:
- AndroidManifest.xml → This is the main file of our project.
In this XML file we have the general settings of our program, as the version number of our program (android:versionCode="1") and the minimum and maximum supported SDK versions. In this case, the minimum version is the SDK 8, generally known as the the 2.2 version (called Froyo), and the maximum version is the SDK 10, generally known as the 2.3.3 version (called Gingerbread).
We also have other information, such as the icon of our program (android:icon="@drawable/ic_launcher"), it's theme layout (android:theme="@style/AppTheme"), the name of the program (android:label="@string/app_name"), it's main screen (android:name=".MainActivity"), and other information that are not so important now.
- src/ → In this folder we have all the source code in Java.
When we create our project, only the file MainActivity.java s created. This is the file that contains the home screen of our program. In Android, a screen is known as an Activity. So, if we want to create a screen, we have to extend the class from an Activity.
A Activity has a "life cycle", which is best explained here, more specifically in the "Activity Lifecycle" section. In short, an Activity (which from now on we will call "screen") has events that are activated when the focus of the application goes for the first time to the screen (onCreate), when the focus goes to the application (onResume), when we minimize the application (onPause), and when we finish the application (onDestroy), among others.
Each screen must MANDATORILY have a graphical user interface associated with it. This is done through the method setContentView(), which must be implemented in onCreate event. In our application, we set the layout of MainActivity with activity_main layout, which is set as an argument of the method setContentView(). So we have our screen and it's layout correctly done.
Other things about MainActivity source code, like what is the event onCreateOptionsMenu and why the argument of the method setContentView() is R.layout.activity_main and not just activity_main) will be explained below. - res/ → In this folder we have other configuration files, such as strings, menus, colors, layout styles, and also all the media files (images, audio and video) of our program.
Notice that in the figure below we have 4 folders whose prefix is drawable. It's in this folder we store images and animations of our project. But why are 4 folders and not just one?
Unlike the iOS mobile operating system, which has only one type of mobile phone (iPhone) and one type of tablet (iPad), in Android we have hundreds of phones and tablets. So we'll have also a great multitude of screen possible sizes in which an Android application can run. Foreseeing this feature, Google chose to create 4 different screen sizes: ldpi, mdpi, hdpi and xhdpi.
The acronym ldpi means "low density of pixels"; mdpi means "medium density of pixels"; hdpi means "high density of pixels"; and xhdpi means "extremely high density of pixels". In short, respectively, we can interpret these acronyms like "small screen size" (like Sony Ericsson Xperia Mini), "medium screen size" (Motorola Atrix), "large screen size" (Samsung Galaxy S 5) and "very large screen size" (usually tablets). Further details here.
Following, we have the folder layout/ (where there are all the graphical interfaces to be used in our project), the folder menu/ (where there are all the menus of our project), and folder values/ (where we can have different types of structures common to our project, such as strings, layout themes, arrays, colors, among others).
The layout of each screen of Android will be better explained in another topic, as well as the menus. About strings, a common question is to understand why we need to declare all the strings in an XML file, and not in the actual source code in Java.
This is another thoughtful practice of Google. How the Android mobile operating system is the most used in the world, being used in hundreds of different countries, it's necessary to have a support for internationalization. In other words, there must be a easy way to an application in a language be changed to another language. The solution found by Google was create the folder res/, a specific folder to store certain structures of our program. In this case, strings.
So, suffice only modify the text in the file strings.xml to our application be supported in various languages. In short, it's much simpler to modify the contents of the file strings.xml than changing out, one by one, all Java files of our project.
- R → This is the most important class of any program in Android. Here are stored all memory locations of the components used in our application (Java classes, XML files, images, sound files, audio, etc).
These memory locations are basically integers. If, for example, we want to reference an image file as the file ic_launcher.png (the icon of our application), we simply need to call the memory location where this image is stored. Although this sounds somewhat complicated, it is simpler than it seems. We just need to call the image as follows: R.drawable.ic_launcher. In other words, R.<image folder>.<image name> (R dot <image folder> dot <image name>).
Therefore, in the method setContentView() of class MainActivity.java, the argument that we have is R.layout.activity_main, not activity_main.
NOTE: This class is self-generated. Even if we modify it manually, it'll be automatically generated again.
Now that we understand a little bit about the structure of our project, we need to run it to see how a program runs in Android, through Eclipse. This is explained in the following step.
To run our project, first we need to create an emulator of Android. This is pretty simple to do. Just click the menu "Window" from the Eclipse menu bar, then "AVD Manager" (Android Virtual Devices Manager). Then, in the new opened window, simply click "New". Then, the window below will be opened.
Now simply choose the name of our emulator, it's "Target" (version of the Android SDK), it's screen size into the check button "Built-in", and then click "Create AVD". And it's done! We have an Android emulator ready to be used.
To run our program, simply right-click on the project name in the "Package Explorer" (or even within any files of our project), click on "Run As", and then in "Android Application". Thus, the emulator will be opened, and our application will run.
Below we can check the execution of our application.
-----
In the following topics will be shown more interesting applications.
If someone has a question, suggestion or criticism, feel at ease.
Nenhum comentário:
Postar um comentário