Tutorial 1
Tutorial 1
Contents
1 Introduction 1
2 Introduction to Android 1
3 Preparatory actions 2
3.1 Download and installation of Android Studio . . . . . . . . . . . . . . . . . 2
3.2 Installation of Android development libraries (SDKs) . . . . . . . . . . . . 3
3.3 Checking for Updates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
3.4 Creating Android Virtual Devices (VDs/AVDs) . . . . . . . . . . . . . . . 6
3.5 Prepare your phone for your Apps . . . . . . . . . . . . . . . . . . . . . . . 8
4 Laboratory Work 9
4.1 Create a “Hello Mobile World” Application . . . . . . . . . . . . . . . . . . 9
4.2 Improve your application - Hello World v2 . . . . . . . . . . . . . . . . . . 12
List of Figures
1 SETUP: Start Setup Wizard. . . . . . . . . . . . . . . . . . . . . . . . . . 3
2 SETUP: Option “Standard”. . . . . . . . . . . . . . . . . . . . . . . . . . . 3
3 SETUP: Verify settings and note that some PC’s may present an option
to speed up the emulator. . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
4 SETUP: Verify the license agreement and finish installation. . . . . . . . . 3
5 Configure in Welcome window. . . . . . . . . . . . . . . . . . . . . . . . . . 4
6 SDK manager icon. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
7 SDK Manager: SDK Platforms . . . . . . . . . . . . . . . . . . . . . . . . 5
8 Android Studio: Menu and “Check for Updates”. . . . . . . . . . . . . . . 5
9 Virtual Device: choosing hardware profile. . . . . . . . . . . . . . . . . . . 6
10 Virtual Device: choosing system image. . . . . . . . . . . . . . . . . . . . . 7
11 Device Manager: list of existing Virtual Devices. . . . . . . . . . . . . . . . 8
12 Choose your project - Basic Activity . . . . . . . . . . . . . . . . . . . . . 9
13 Configure you project. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
14 Main files. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
15 Resource files. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
16 Res. img. files. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
17 Hello World app. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
18 Hello World app v2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
19 AS with dynamic string and breakpoint. . . . . . . . . . . . . . . . . . . . 14
20 Hello World Optional, showing information about the current build. . . . . 15
Tutorial 1 - Hello Mobile Android World!!
1 Introduction
This tutorial starts with an introduction to Android, then it proceeds with the down-
load and installation of the official Android Integrated Development Environment (IDE)
that is the Android Studio. The tutorial proceeds with the configuration and update of
the necessary tools. Thereupon, it requires the development of a simple “Hello World”
application.
Follows a link that should be consulted during the development of Android applica-
tions.
All tutorials are expected to be finished and presented to the teacher during class hours.
The resulting projects must be completed, presented, evaluated and uploaded to the
course page on the Moodle platform in class, until the deadline date shown on the cover
page. After each deadline date the final grade, for the project, will have a penalty value
of 5/20 for each passing week/Monday until the project is presented and evaluated
by the teacher in class. This condition applies regardless if the project was submitted or
not on the Moodle platform.
2 Introduction to Android
Android OS was originally created inside Android Inc. by Andy Rubin in the early 21st
century as a mobile operating system. In 2005, Google acquired Android Inc. and made
Andy Rubin the director of Google’s mobile platforms.
Android OS is an operating system supported by the Linux Kernel. That’s why
Android devices are essentially Linux computers. The project responsible for developing
the Android system is called “Android Open Source Project (AOSP)” and is led by
Google.
The Dalvik Virtual Machine (DVM) is the executor of Android applications.
The compilation of Android code generates Dalvik Executable format files with extension
.dex. These files, plus resources and the manifest files are grouped in .apk files. One
.apk file is one Android application ready to be installed.
There are four (plus one - Layouts) main components in Android applications (see
https://developer.android.com/guide/components/fundamentals):
1
Tutorial 1 - Hello Mobile Android World!!
These elements will be introduced in more detail when they are used. For now, we
will focus on Android Activities and Layouts.
An activity is a screen, in an application, providing the context for one user inter-
action. An application may have multiple activities, providing multiple contexts for the
user. This is the way used to spread all the information, of the application, in the re-
duced size screens of mobile devices. An activity has one Layout Container to join the
user interface elements. One activity can also change its Layout Container and provide a
mutable user interface.
A Layout Container groups design elements, called Widgets, to build a screen.
The design elements can be user interface elements for text, graphics, 3D content, digital
video, menus, animation, ...
Layouts Containers are built based on ViewGroup class and Widgets based on
View class.
For Android graphics such as images or animations the term Drawables is used.
The Event Handling capabilities of Android User Interface elements offers a way to
applications react to user actions or other source of events.
3 Preparatory actions
To enable the development and execution of Android applications this tutorial presents
the installation of IDE, the development libraries (Software Development Kit (SDK)) and
emulation capability.
The Android Studio package includes all the tools necessary for the development of
android applications:
2
Tutorial 1 - Hello Mobile Android World!!
Note:If you were programming in Java, you would also need to install the Java
Development Kit (JDK) and the Java Runtime Environment (JRE).
4. You will go through several configuration steps in order to install Android Studio
(see Figures 1 to 4).
Figure 3: SETUP: Verify settings and note Figure 4: SETUP: Verify the license agree-
that some PC’s may present an option to ment and finish installation.
speed up the emulator.
3
Tutorial 1 - Hello Mobile Android World!!
Android 10 Q (API 29). To develop code for one platform, you need the corresponding
libraries, called Software Development kit (SDK). So, to develop code for Android 8.0,
you need its Android SDK platform and tools. For the management of the SDKs installed,
or to install new ones, there is the Android SDK Manager. It is recommended to have
installed the Android 10 Q (API 29) and another one like Android 6.0 Marshmallow (API
23) or the highest one that is supported by the student’s phone.
1. Open the Android SDK Manager: it can be done in the “Welcome to Android
Studio” window (when there is no Project opened), by selecting the “More Actions
(3 dots at the top right)” drop down list and choosing SDK Manager (see
Figure 5) or when we are working on a project by opening the menu Tools > SDK
Manager or by selecting the SDK Manager icon in the toolbar (see Figure 6).
2. In the SKD Manager panel (see Figure 7) select the Android version you wish to
develop and are not installed by checking them and then press the “OK” button.
Select “Show Package Details” check-box to see in detail the components that are
part of each version of the Android API.
In SDK Manager the “SDK Platforms” tab is used to install Android API versions
and the “SDK Tools” tab is used to install development tools.
4
Tutorial 1 - Hello Mobile Android World!!
1. Check for updates: in the “Welcome to Android Studio” window, when there is no
Project opened, by selecting the “Options Menu”(placed on the bottom left) drop
down list and choosing Check for updates (see Figure 5) or when we are working
on a project by opening the menu Help > Check for updates... (see Figure 8)
(do not install Beta versions).
5
Tutorial 1 - Hello Mobile Android World!!
1. Open the AVD Manager either in “Welcome to Android Studio” window (when
there is no Project opened) by selecting the “More Actions” drop down list and
choosing Virtual Device Manager (see Figure 5) or when we are working on a
project by opening the menu Tools > Decice Manager or by selecting the Device
Manager icon in the toolbar (the one at the left of SDK Manager in Figure 6).
2. Create two VDs, one for a Pixel 3 smartphone and another for a Pixel C tablet.
For each one, start by clicking in the “Create Device” button. In the Virtual Device
Configuration panel (Select Hardware), see Figure 9, select the hardware profile for
the desired VD and then click the “Next” button. In the VD system image panel,
see Figure 10, select the Android platform version, hardware architecture (x86, ...)
6
Tutorial 1 - Hello Mobile Android World!!
and the supported Google services (Google Play, ...), download it if necessary and
then click the “Next” button. Use the Recomended configuration and leave the
remaining parameters, for now, set to “default” and finish.
3. Start the Pixel 3 AVD. After you create the emulators, you can see them in the
Device Manager panel (see Figure 11). On that list click the “play” button on th
Pixel 3 smartphone emulator to launch it.
Note 2: The VD after being started should not be stopped/closed during develop-
ment. If you make changes to the code, just do the “re-deploy” of the application
in the VD.
Note 3: During the creation of an emulator you can choose between the options:
“Snapshot” or “Host GPU”. If you choose the first option, the emulator, when
stopped, is like hibernated (saved a copy of its memory state), and a subsequent
start will be fast. If you choose the second option, rendering is faster because the
PC graphics card is used.
Note 4: When choosing the Android API version, when creating the emulator, you
can select a version where the VD image is based on an ARM CPU architecture or
an Intel CPU architecture. An VD that is based on an Intel system is much faster
if it runs on Intel hardware, because in that case it is not necessary to convert the
7
Tutorial 1 - Hello Mobile Android World!!
8
Tutorial 1 - Hello Mobile Android World!!
Bridge (ADB). To do it, go to Settings > Developer options and enable USB
debugging.
See https://developer.android.com/studio/command-line/adb.
4 Laboratory Work
4.1 Create a “Hello Mobile World” Application
1. Create an Android project selecting File > New > New Project to open a dia-
logue box with several options (see Figure 12). Choose the Empty Views Activity
option and click Next. This option will create a basic application with one activity.
2. Type the name of the application: Hello World. Write the name of the pack-
age: “dam aXXXXX.helloworld” (see Figure 13) (aXXXXX should be replaced
with your student number). You should use that base package name for
all your projects. Set the path where the project should be saved, as: “dam-
path\code\HelloWorld”. Where dam-path should be the directory of DAM. Select
the Kotlin language and the API 24: Android 7.0 (Nougat) version for the
Minimum API level. Click Finish to generate the project.
Note 5: In the selection of Minimum API level the configuration wizard will show,
for each API version, the estimated percentage of devices that supports the version
and the new features introduced by the version. Inspect the new characteristics of
each version.
Note 6: After pressing the Finish button, some installations if the IDE have been
reporting an error with the gradle JDK:
9
Tutorial 1 - Hello Mobile Android World!!
Figure 14: Main files. Figure 15: Resource files. Figure 16: Res. img. files.
3. Inspect generated code. As you can see in Figure 14 the Android Studio creates
three sections for files: manifests, java and res.
An app should contain an manifest file containing: app package name, components
(activities, services, broadcast receivers, and content providers) and their properties,
permissions needed and hardware and software features required.
The java section contains Java(.java)/Kotlin(.kt) files. It should contain Activity
files and all other Java/Kotlin files. The generated files are set with the defined
10
Tutorial 1 - Hello Mobile Android World!!
4. Execute the application in the Pixel 3 AVD. Press Run > Run “app”, or by using
Shift + F10 or by clicking the green play symbol in toolbar (on right of device drop-
down box; right of “No Devices” in Figure 8). In the window choose your Pixel 3
AVD smartphone and click the “OK” button. The AVD will present the contents
of the Figure 17. Check the created elements in the emulator screen.
Note 7: This procedure may take some time. Be patient. If the execution blocks,
try closing the AVD and restarting the execution.
5. Put strings in strings.xml file. String should be defined in string.xml file, to en-
able internationalization (we will see it latter). In strings.xml each string is identified
by a name. To get the value of the string we must use ”@string/strname (if named
strname). Change the text in the TextView to strings.xml: in activity main.xml
select the existing string “Hello World!” and press ALT + ENTER; select “Ex-
tract string resource”; write hello string in the name and press Ok. Check that the
11
Tutorial 1 - Hello Mobile Android World!!
6. Change the string hello string (in strings.xml) to “Hello Android World”. Run
again.
7. Change the app name (in strings.xml) to “Hello World V1”. Run again.
8. Run and debug the application in your real device. Use ADB by USB cable and
by WiFi.
See https://developer.android.com/studio/command-line/adb.
2. Change the TextView to the top and change some properties. You should have
the window in Split mode (select in top right corner), with the Palette view at
the left, the design editor in the middle and the Attributes view at the right.
In the design editor, select the TextView and in its bottom constraint give a left
mouse click with the CTRL pressed to remove that constraint. To add a new
constraint, if necessary, just stretch a new one from a source point (one of the
circles) to an anchor point such as, e.g., another TextView, the edge of the screen
or a horizontal/vertical barrier. Set its attributes: textColor (use colors placed in
colors.xml, e.g., purple 500), textSize and textStyle, to look the same as in 18.
3. Add a second TextView with “My First App” text (strings to strings.xml), by
dragging one from the palette to the middle of the graphic editor. Connect the top
graphical constraint to the bottom of the other TextView. Change the attributes
of the TextViews to change: layout width (set to 0dp to use all width), textSize,
textAlignment and background to look the same as in 18.
4. Add a image to the screen, as shown in Figure 18. Download one small image
from the web and add it to the “drawable” folder. The image name should only
have lower-case letters. From the palette view drag an ImageView element to the
layout of activity main.xml. In “Pick a resource view” select the image. It will add
to the ImageView: app:srcCompat=“@drawable/smileygood” (if smile is the name
of the image file). You will see an yellow warning because the image do not have
an description. Add a content Description (alternative text) to the ImageView:
android:contentDescription=”Just smile” (strings to strings.xml).
12
Tutorial 1 - Hello Mobile Android World!!
2. Format code: this action enables to format the code automatically. To activate
this action go to: Menu → Code → Reformat Code, or press Ctrl + Alt + L.
13
Tutorial 1 - Hello Mobile Android World!!
that appears in the second left drop-down box (the first one is to select the device
and the third one is to select the level of visualization).
3. Setting a dynamic string: now, we will first send that message to strings.xml
file by giving a double click over the Activity string to select it and doing ALT
+ ENTER, select Extract string Resource, in the Extract Resource dialog box
we should insert in Resource name: activity oncreate msg, and in Resource value:
“Activity %1$s onCreate”. Strings with dynamic contents, like the one in onCre-
ate, use the String.format() way, where, in our example, the %1$s says to use
the first argument treated as a string in the place where %1$s is. Hence, we are
now ready to complete the call to getString by completing it with the addition
of a second argument with the value that we want to appear. This should re-
sult in: getString(R.string.activity_oncreate, this@MainActivity.localClassName). Run
again the app to see the result. See the results also in Figure 19.
4. Capturing the screen: the LogCat view has two icons that enables to do a screen
capture (icon with a photo camera) and screen record (green icon with a play
symbol) of the device screen.
14
Tutorial 1 - Hello Mobile Android World!!
5.3 Optional
Create a new application that show, on a MultiLine Text Widget, information about the
current build, extracted from system properties as shown in Figure 20. Note 8: For this
application, use the android.os.Build object to extract the necessary information.
Figure 20: Hello World Optional, showing information about the current build.
15