In order to test the applications, we will need an Android Virtual Device (AVD). This is an Android smartphone emulator that we can install and launch applications we create. Let’s create it.
Lesson updated 04/06/2017
In the last lesson, we installed the development environment and the Android SDK.
Now we can finally create our first application and see how it works.
To create an application, we need to create a project in Android Studio. When creating a project, it creates a module. In this module, we draw application screens and write code. And when we launch this module, we get a ready-made application. Therefore, the module is essentially an add-on. And the project is a container for the module.
That is, in the simplest case, the structure of the project is as follows:
There is a project and it has a module. When a project is started, a module is started and we get an Android application created in this module.
In this case: one project = one Android application (one module).
But there can be several modules in one project. And there are several projects you can create.
Here in the first project two modules are created, and in the second project – three modules.
When starting any project, you will need to specify which module you want to run. And each module is a separate Android application.
That is, in this case: one project = multiple Android applications (multiple modules).
We will not go into detail yet which of the proposed schemes is better and more convenient. You can create one project for each lesson and create modules for each lesson. Or you can create a separate project, for example, every 10 lessons. Generally, you can create a separate project for each lesson.
I think we will start with an option: one project for all lessons. And over time, as you get used to it, decide for yourself which option is most convenient for you.
Let’s create a project. We open Android Studio. The startup shortcut must be in the start menu. Or you can open the folder where Android Studio was installed last lesson. The bin folder must have an EXE file.
The development environment has opened. press Start a new Android Studio project.
A project creation window appears. Let’s fill in.
Application name – the name of the project. It will appear in the project list when you open Android Studio. Let’s write here Android lessons (ie Android lessons).
Company Domain – the name of the site, we write startandroid.ru.
Package name is a Java concept, you can see in detail here. In short, it is a prefix for the name of our application classes. As you can see, the package is automatically made on behalf of the site and the name of the project. You can always edit it manually by clicking the edit link on the right.
Project location – the folder on the computer where all the project files will be located. In the last lesson, we created the android folder where we installed Android Studio and SDK. Create an AndroidStudioProjects folder there as well – this folder will store all Android Studio projects. And in it create the Androidlessons folder for our current project.
Most likely now nothing is clear. That’s okay, don’t worry. In each lesson, I will be prompted to fill in these fields, and eventually understanding will come.
Wizard asks what platform we will code for and the Minimum SDK (the minimum version of Android on which the application can be launched). We leave everything as is.
Wizard can load the components he needs.
then select Empty Activity.
We do not change anything here. For now, we don’t need to know why we need it all.
the project is created
Then the studio opens. When opened, it can show tips.
If you don’t need them, turn off the checkbox.
But below you can see the progress bar. This means that there is still some action to be done and we have to wait.
And as a result, the project opens
We are immediately open to editing some files. We will not change anything in them, while it is not necessary.
On the left we see the word app is a module. By default, an app module is created when creating a project. We are not interested in it because we will create our modules. But let it remain, I do not advise you to delete it. The wizard that we just went through when creating the project mainly touched on this module.
So the project was created. Now let’s create our own module in the project. I will repeat that for almost every lesson we will create a module in this project. Now let’s create a module (app) for this current lesson. This procedure will be somewhat similar to the creation of the project, but with small differences.
To create a module – select from the menu File -> New -> New modules
The type of module we choose Phone and Tablet Application
fill in the fields
Application / Library name – directly the name of the application that will appear in the list of applications in the smartphone. We write here FirstProject.
Module name is the name of the module. That is, this name will appear on the left in the list of modules, where there is now an app. Let’s come up with a template for the name of the modules.
For example: p<номер урока(000)><номер проекта в уроке(0)>.
There are three numbers for the lesson number, one for the project number. Also, we will add the name of the program – FirstProject. And all this will be written in small letters and without spaces. The following module name will appear: p0031firstproject.
Package name – Edit the package name manually by clicking edit on the right. Let’s leave ru.startandroid there and add the point and the name of the module.
Minimum SDK leave unchanged.
then select Empty Activity.
We do not change anything here
press Finish and we are waiting.
After a while, the module will be created and we will see it in the list on the left. This is p0031firstproject – the value we specified in the Module name field.
You can open this module and see its contents.
Let us briefly walk through the elements of interest to us
file AndroidManifest.xml – program manifest or config file
In the folder java and its subfolders will be all of the application code written by us
Teka really used for resource files of different types.
We will continue to use all of this, and it will become clear what and why is needed.
Let’s finally launch our first app! To do this, select the appropriate module in the drop-down list above
And click (just to the right of the list) the green triangle button (or Shift + F10 combination).
To launch the application, you need some thread real Android device or emulator.
We still have nothing to launch the application for. You can plug in a real device with the cord, and it will appear here (unless there is a problem with the drivers or device settings).
Or you can create an emulator. Click Create New Virtual Device
Here you can select the form factor of the device. Leave what is selected by default.
Then go to the x86 Images tab and there should be an image that does not have the word Download. That is, completed downloads and we can use it.
In this case, Android 7.1.1 will be installed on the emulator. If you need a different version, download it and use it.
Then we are asked to specify the name of the emulator and change its settings. We leave everything as is
As a result, the device emulator just created appears in the device list and we can use it to run the application.
After a while (up to a few minutes) an emulator will appear
And it will start running Android
And as a result, our application will launch
The title in the header is FirstProject. This is what we specified when creating the program.
That is, you have created and launched your first application, with which I welcome you) There are hundreds of such applications and launches ahead.
If the emulator did not show your application, then make sure that Android Studio “sees” this emulator. To do this, click the Android Monitor tab at the bottom left
And in the list of devices just above should be visible emulator Nexus_5X_API_25
If the emulator is listed and the application does not appear, try running the application again by clicking the green triangle (Shift + F10).
If the emulator is not listed, close the emulator and try running the application again.
This lesson was updated in June 2017. Many of the following lessons were written in 2011-2012. Therefore, screenshots may differ from yours. This is normal.
Also, I have to warn that I did and tested the first lessons on Android version 2.3.3. This is stated at the beginning of the lessons. But this does not mean that the lessons are already hopelessly outdated and there is no point in reading them. The code under 2.3.3 and 7.1.1 is in the vast majority of cases exactly the same. New versions of Android add new components, and the past ones usually remain unchanged and are rarely modified or declared obsolete.
If you have a project open and you want to see the Android Studio launch window again, select from the menu File > Close Project.
You will see a start window, to the left is a list of your projects.
If you have a username in Russian, there may be problems. How to solve them can be read on the forum in the branch of this lesson.
If the line above didn’t help, then try this solution again.
In the next lesson, we will be adding different elements to our application and changing their properties.