Introduction to Android Studio IDE

Previously, the Android development was spread using the Eclipse platform with the Android Development Kit (ADK), provided by Google, which launched the Android Studio platform. This is based on IntelliJ IDEA, which is an IDE that also supports Android, but it has a high cost.

The Android Studio IDE has some advantages like, for example, the Gradle dependency manager (see Links section), also based on IntelliJ, widely used all over the world. This is one of the great strengths of the platform editor as it offers more options to the developer at build time, once Eclipse used the classic compilation way.

Android Studio can be installed on Windows operating systems, OSX and Linux and is recommended by Google itself that the hardware must have at least 4 GB of memory and 1GB of free hard disk space, but we recommend that you have more memory because it was noted that Android Studio is still a little slow. You must have Java installed on the machine via the JDK (Java Development Kit), not the JRE, as it is usually installed, once to develop on Android is necessary for all Java development classes to be present on the machine.

Installation

To install Android Studio just get on its official website (Links section) and download the latest version available. Choose your platform, as shown in Figure 1.

Figure 1. Downloading Android Studio IDE.

After installation you need to install the Android SDK, because without it you can not develop for the platform. To do this, click on "Configure", as shown in Figure 2.

Figure 2. Welcome Screen

After entering the next screen, click "SDK Manager" and see that the Android SDKs manager will be opened. Check the options shown in Figure 3. See that I have already downloaded the Android SDK 6.0, regarding Marshmallow version (last one available), and you must do the same.

Figure 3. SDK Manager

Remember that to start any development on Android is necessary to have installed:

Android SDK Tools, in which are contained the tools to the SDK and the emulator;

Android SDK Build-Tools, in which are contained the tools used to compile your code. Depending on the version you have downloaded, it will be used to compile your code.

Android SDK Platform-tools, in which are contained all the Android tools.

Other items can be installed to have more components, such as documentation, Android TV emulators, Android Wear and other elements that can be used by the developer during the project. With everything installed you can start creating the first project using the Android Studio.

First Application

To start a new project we must click in "Start a New Android Studio Project" option. Note in Figure 4 that we put the name of the application, the domain and where to save to create the main package.

Figure 4. Configuration of Project I

Once complete, click the "Next" button to move to the next screen (Figure 5) which will make the configuration of the SDKs we need to simulate, ie which device (Smartphones/Tablets, Android TV, Android Wear or Android Glass) we will execute the application.

For our example we will select only smartphones and Tablets.

Figure 5. Minimum SDK.

Something very interesting to note in this screen is that when selecting a SDK is shown the approximate number of devices that can use the application to be created in accordance with active devices in the Google Play Store.

In the next screen select one of the Activity templates that are available. The template selected for our first example will be Blank Activity, as shown in Figure 6.

Figure 6. Template Selection

The next screen, shown in Figure 7, is where we will set the name of the Activity and its template. Remember, the template is an XML file where the screen elements are defined and Activity is the Java code that accesses these elements of Template.

Figure 7. Defining Names

After clicking "Finish" the project is built and Figure 8 shows the main screen of the Android Studio development with XML template and rendered screen elements. By changing the code, this screen is changed automatically, and if any element changes this is rendered with no major trouble into the code.

Figure 8. Android Studio screen

The XML code generated automatically when you create a project using the Blank template can be seen in Listing 1.

It is also created the Manifest.xml file, which is a mandatory file for any Android application, it is that where we find other informations about Activities, permissions and other kinds to the application that is being written. Listing 3 shows the manifest file generated by default.

To run an application you need to create a virtual device, which emulates the operating system Android, which the developed application will be tested and validated before other tests, such as on real devices. To create a virtual device, click the button highlighted in Figure 9.

Figure 9. Button to create Emulator

The screen that appears after clicking this button, displays the list of all the emulators ever created. In the first run the list will be empty because the first emulator must be created now. By clicking the option "Create Virtual Device" as shown in Figure 10, it will direct us for the screen of emulators.

Figure 10. Listing of emulators.

To create an emulator, the first thing that should be done is to choose what kind of device will be emulated, in this case will be a phone like the Nexus 5. Figure 11 shows how it is the screen layout.

Figure 11. Hardware Selection

After selecting the Hardware, you must create the image to be used, ie what API, that the developer wants to create the application as shown in Figure 12.

Figure 12. Picture Selection

With the image selected properly, Android Virtual Device will be created, here we should put informations such as name, screen orientation, if the camera is enabled or not, or if it will emulate SD card and the type of performance that will be used. Figure 13 shows the screen described.

Figure 13. Establishment of the AVD

So, click Finish to end the creation process. With all this done, the emulator can be started for testing, it is important to browse the emulated system to make sure that everything is working properly. Figure 14 shows the emulated system.

Figure 14. System emulated

Now the question that most developers who are starting on the Android ask: How my application will be installed in the emulator? The answer is quite simple: Android Studio is responsible for executing commands necessary to install the APK file within the system emulated by the Virtual Device.

For this to be completed, just click the button highlighted in Figure 15. First, Android Studio will check the code to see if there is any syntax error that prevents the build, and if no error is found, the system will generate the APK, and install it within the previously emulated system.

Figure 15. Button to run the application.

Now as a demonstration of Android Studio, a small application will be developed for teaching purposes and consolidate the knowledge discussed above. What would be developed is no secret, the app will contain a screen, that will have a Button and TextView, clicking this button, a message appears on the TextView, so you must change the activity_mail.xml XML file, it is something very simple, but when you click the button shown in Figure 15, all the application installation process within the virtual device starts. Listing 4 shows the template XML code that has been modified to meet the criteria described above.

With the build XML file changed, the compilation can already be done, and the modified layout would be shown on the screen. However, it will not have any functionality because it was not modified, only the template structure was, thus the code that will control and add functionality is not contained in the XML but in the Java class that extends a super Activity type class, it is worth remembering that every Activity in Android is responsible for controlling a graphical interface, ie will be associated to an XML file layout. The Java code responsible for making this junction and give proper functionality for XML is very simple, and can be seen in Listing 5.

Note that an object of type Button is declared but does not start using the new but using the findViewById method, this method is responsible for calling the screen element that is in XML with the object stated in the Java code. It is also worth remembering that the findViewById returns an object of type View so it is necessary to make the cast to the data type that was declared at the beginning. The same is true for the TextView, the findViewById is called, the cast is done and the XML element is placed on the method parameters, then the TextView is declared final for use within an internal method, which is nothing more than the onClick method, one listener for the button Events be captured and displayed on screen.

Figure 16 shows the result of the above described code.

Figure 16. Final Result

Conclusion

In this article you could learn how to create and run a simple application on Android and use the powerful Android Studio platform, only basic features have been addressed, but with this it is possible for the developer to create their codes and test them in the virtual environment emulating the Android system.

Hope you enjoyed, until next time.

Links

Julio is a System analyst and enthusiast of Information Technology. He is currently a developer at iFactory Solutions company, working in the development of strategic systems, is also a JAVA instructor. He has knowledge and experi...

To have full access to this post (or download the associated files) you must have MrBool Credits.

See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video.Subscribe for this package!