Android Studio

From Bill's Tutorials at btutor.com

This is an extract from the second chapter of “Start Programming Mobile Apps with Android Java” by Bill Tait. The book is available in Kindle or paperback format from the Amazon bookstore.

 

Installing Android Studio

This is probably the most difficult part for beginner programmers but it is an essential part. Android Studio does not (yet, at least) simply install itself on your computer like some other applications. There may be as many as four stages to the installation of Android Studio, as follows:-

Installing Studio

First you need a recent version of the Java SDK installed on your computer to use Android Studio. This is the Software Development Kit, or SDK, not the runtime engine, the JRE or JVM. You can find out if you have Java installed on a Windows system by visiting the Control Panel then choosing the Programs option which should include Java. You can install the Java SDK from the oracle site.

Now download Android Studio from the android website at developer.android.com

As usual, accept the license agreement then run the installer. Accept all the default options to complete the process. Now run the application. If there is no Java installed or the application cannot find it, you will get an error message to the effect that no JVM is found. You may have to find the environment variables on your system and add a JAVA_HOME variable that points to the JDK directory, such as

C:\Program Files\Java\jdk1.8.0_31

Then the Setup Wizard downloads a few components and we have the welcome screen.

To test the installation you should choose the option to start a new Android Studio project.

You may be offered a selection of app templates. If so it is best to start with a blank template at this stage. These templates are for different types of Android device and each will provide you with some starting code. The exception is the No Activity template which gives you nothing. So start with the Blank Activity – second from the left.

This will open a second screen to select a form factor. You will be asked to choose what types of device you are developing for. Here the selection has been made for you by Studio. It is for a phone or tablet device with an Android API level of at least 15. If you intend to develop for devices with a lower API then you should select an appropriate value from the drop down list.

Creating a New Android App

An app is always developed as a project since it consists of several separate files packaged together. So we start the development by creating a new project in the next screen.

The first option is the name of your app. We will call our app Starter so that should be entered into the name box. Then you need to enter a company domain. This is the domain of an assumed developer company. It is added in reverse to the app name to form a package name which is displayed below these two entries. This package name is the full name of your app in the Play Store or on a mobile device, so it has to be unique, that is, different from all the other apps. There may be other apps called Starter but there is no conflict unless the domains are the same. It is like placing files with the same names in different folders of your computer. The company name defines the namespace in which the app exists, 

The key to defining a unique package name is to declare a company domain that is uniquely yours.
So, in this Starter app the domain is yourdomainname.com and the package name is

com.yourdomainname.starter

You can now click on the Next button to go through a series of screens that allow you to select options. It is best to just accept the defaults but make a note of the file folders used since you have to find them outside of Studio. Finally you will be able to click on the Finish button to open the development screen.

The Android Studio Development Screen

The screen in which all the development will take place is illustrated in Figure 1. A project consists of a number of files and two of these are listed in the tabs near the top of the screen. These are MainActivity.java and activity_main.xml. The latter is already selected and its contents are displayed in the workspace below.

The activity_main.xml file is an xml file that defines the user interface for your app. It will probably start in the mode shown in the figure, which is the Design mode. This is a graphic representation of the user interface. It shows a phone displaying the present contents of the user interface which is just the simple text message, “Hello world”. This has become something of a tradition in computer programming when learning a new language.

The Android Studio user interface in design mode
Figure 1. The user interface xml file displayed in Design mode.
At the bottom of this display there are two tabs. The Design tab is presently selected. If you click on the adjacent Text tab the screen will display the same file but in its underlying text mode, which is in the form of an xml file as illustrated in Figure 2.
The Android Studio user interface file
Figure 2. The user interface file displayed in text mode.

 

The Android Studio Project Files

An Android Studio project comprises several files. You can view them on your computer file system or in Studio and if you delete them from the computer they are deleted also from Android Studio. In the computer they are in whatever folder you selected when you installed and ran Studio.

The Starter folder corresponds to this particular project and opens up to show a large number of subfolders and some files. We are interested only in one of these subfolders, namely, app. The others, including build and gradle are used by Studio to compile the app and we will not be exploring them. The app folder contains all the files that we are interested in.

These files are imported into the Android Studio workspace and displayed in the left hand pane of the development environment. This project pane shows the files for a single project. If you open another project you have the choice of replacing the current one or using a separate window. So there is only ever one project in each window.

The app folder corresponds to the app folder in the computer file system. Opening this reveals three subfolders, called manifests, java and res. The first contains only one file, AndroidManifest.xml. The java folder contains two subfolders both named after the app. The first one contains MainActivity and any other java files we write. The last of the three java subfolders, res, contains the resources used by the app.

Then double clicking on a file name opens it in the central workspace area alongside any other files already there. Each has a tab at the top of the workspace so it can be selected for display and editing by clicking on the tab. Or it can be removed from the workspace by clicking on the tab close icon.

The Android Starter Code

We have just created an app called Starter and this is currently showing in the development screen. The main Java file, MainActivity.java is already open in the workspace and all we have to do to view it is to click on its tab. However if it is not already open then it can be found in the projects pane at

app/java/com.androidjavaapps.starter, or

app/src/main/java/com.androidjavaapps.starter

Double clicking on this name opens the file in the workspace pane where we can edit it and develop our app.

Default code is generated by the IDE for whatever form factor you selected. If that was the blank activity the code is as follows:-

package com.yourdomainname.starter;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends ActionBarActivity {

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

    @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar
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.
// if 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);
}
}

Compiling and Running the App

So what we have now is actually a fully functioning app. It does not do much but it can be compiled and executed on our Android device. We can do this by tapping the Run button at the top of the display. This is a right pointing green triangle.

This will compile the source code into an apk app file and run it either on an emulator or on an external phone or tablet device connected to your computer. Normally you will be given a choice between the emulator, which is called an Android Virtual Device, or AVD, and the real device that is connected to the PC.

If the dialog window does not appear you can click on the Run menu option at the top of the screen then select Edit configuration. This will open another dialog window that lets you force the IDE to always let you choose its Target.

Running on a Real Device

It is not recommended for beginners to use the built-in AVD since it is very slow and quite uncertain. It is best to attach a real device and use that for testing.

To use a real Android device you have to connect it to a USB socket on your computer. Then when you run your app you select the top option to choose a running device. However, if this is the first time you have run your own app on your phone or tablet then it will probably fail with a warning that the device is set to block installation of apps obtained from unknown sources. You are offered the choice of Cancel or Settings. Tap Settings to go to your device settings then find the Unknown Sources option and tap the checkbox to select it. You will be given a warning that this may be harmful to your device. Ignore this and tap OK. It is not malicious software since you have just written it. Now click Install. The app will be installed in your device and run.

So now you have created your own Android app and it is running on your phone or tablet. It should simply display the message Hello world in the top left corner of the screen. Also a new Starter icon will appear on the app screen of your device. You can tap this at any time to run the app like any other app in your device. It will be the default Android icon in the form of a green Android figure but we can change that later to our own design.

If you are wondering where the text came from, it came from the other files which were also created by Android Studio. These created a resource called R.layout.main which defines the user layout. This is used in the Java line

setContentView(R.layout.activity_main);

It refers to the default layout which is defined in the activity_main.xml file. This is found in the Starter/app/main/res/layout folder. This, in turn uses a text string defined in the Starter/app/main/res/values/string.xml file. And this has a line containing the text Hello world. If you feel like a little adventure you can try changing this.

If you want to learn how to develop yur own apps in Android Studio you can purchase "Start Programming Mobile Apps with Android Java" by Bill Tait. This is available in Kindle and paperback format at the Amazon online bookstore.

Further Reading

You can learn more about Android Java programming in my book "Start Programming Mobile Apps with Android Java" published in Kindle and paperback format by Amazon.

You can aso read about some example apps at debden.com