Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Introduction to Android Studio IDE

See in this article a whole introduction to Android Studio IDE and its environment, as well as how to start developing Android Apps in this new platform.

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.

Listing 1. Default XML code.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="app.mrbool.com.appmrbool.MainActivity"
    tools:showIn="@layout/activity_main">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />

</RelativeLayout>

It is also generated a standard Java code that makes the reference to the XML and can display the device's screen, noting that the Activity will always control the screen of an Android application.

Listing 2 shows the Java code generated by default when you create any project using Blanktemplate by Android Studio.

Listing 2. Standard Java code.

package app.mrbool.com.appmrbool;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

}

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.

Listing 3. Manifest.xml generated by default.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="app.mrbool.com.appmrbool">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>


</manifest>

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.

Listing 4. Modified XML code

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:text="Click Me" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

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.

Listing 5. Java code modified

package app.mrbool.com.appmrbool;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button = (Button) findViewById(R.id.button);
        final TextView text = (TextView) findViewById(R.id.textView);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                text.setText("The button was clicked!!!!");
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}

Observe the code contained in Listing 6.

Listing 6. Java code snippet

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button button = (Button) findViewById(R.id.button);
    final TextView text = (TextView) findViewById(R.id.textView);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            text.setText("The button was clicked!!!!");
        }
    });

}

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

Official Gradle website: http://gradle.org/

Official Android Studio download page: http://developer.android.com/sdk/index.html



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...

What did you think of this post?
Services
[Close]
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,00
This 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,00
This 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!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login