MAD Page 1 of 17
Lab Exercise 1
Topic: Getting Started with Android Studio
1. Download and install Android Studio
2. Create and run the HelloWorld example from the Java Quick
Start lecture (you could follow the instructions in the video on
Moodle - "Lab 1 - How to create the Android Studio project").
a. Start Android Studio
b. Create a new project called HelloWorld
Markus A. Wolf
MAD Page 2 of 17
Now add a new window (class AppCompatActivity) to your
project
Markus A. Wolf
MAD Page 3 of 17
Markus A. Wolf
MAD Page 4 of 17
Explanation:
-AppCompatActivity class represents a window in this app
3. Select a device to run the app
Option 1: To run your app on a virtual device
Markus A. Wolf
MAD Page 5 of 17
Add an AVD (Android Virtual Device), which is an emulator
through the AVD Manager in Android Studio (found in the
Tools menu) – I chose a Google Pixel 3 with a recent version
of Android, but you can choose whichever you want)
Click the play button to open the virtual device
Markus A. Wolf
MAD Page 6 of 17
Click the run button to run the app on the device
Option 2: To run your app on a real mobile phone
Use your USB cable to connect your mobile phone to your
laptop
Open Settings, Select About Phone (Thong Tin Dien Thoai)
Markus A. Wolf
MAD Page 7 of 17
Select Software Information
Markus A. Wolf
MAD Page 8 of 17
Select “So hieu ban tao” many times until the phone says that
you are activating developer mode
Markus A. Wolf
MAD Page 9 of 17
After activating developer mode, you will see “Tuy chon nha
phat trien” to appear
Select “Tuy chon nha phat trien” and then enable USB
debugging
Markus A. Wolf
MAD Page 10 of 17
You then see your phone to appear in the device list
Run the app and see the app running on your mobile phone
Markus A. Wolf
MAD Page 11 of 17
4. Change the HelloWorld example to add in an if statement so
that the user gets told if they have passed. Compile and run
the app.
Markus A. Wolf
MAD Page 12 of 17
Switch between Code and design
Write some lines of code
Explanation:
-TextView with id = “@+id/TextView_Main” is now a resource (GUI component)
-@+id : the id is increased automatically and given by Android Studio
Markus A. Wolf
MAD Page 13 of 17
-Now you understand a resource has a name (given by developer) and has an id (given by
Android Studio)
Explanation for source code at line 19:
-TextView is a view. View represents an area, usually a
rectangle which is used to display information of different types
(text, number, name, images, links,…). A text view is a view
which is used to display text information.
-R is a built-in resource object, representing different types of
resources we declare to use in this application (text, images,
videos, other files, GUI components…)
-R.id : get access to the id of the resource
-R.id.TextView_Main : get access to the TextView_Main id
object (declared in the activity_main.xml)
-findViewById : an built-in API which gets access to the id
object and return the view object, in this case a TextView object.
This is a frequently used API in a mobile app which has an
AppCompatActivity (a window)
Markus A. Wolf
MAD Page 14 of 17
Run the app
5. Change the app, so the user gets the following messages:
a. under 50 - "Sorry you failed"
b. 50-69 - "Well done you passed"
c. 70 and over - "Brilliant, you got a distinction"
Markus A. Wolf
MAD Page 15 of 17
6. The text displayed in the TextView is quite small. See if you
can find out how to increase the font size – (Hint: change the
textSize property of the TextView)
- The first way to do this is to adjust textSize in the Attribute
panel:
-The second way to write a piece of code to adjust the textSize
at runtime: main.setTextSize(30);
Markus A. Wolf
MAD Page 16 of 17
7. In Java code one way to specify colours is to use constants of
class Color (e.g. Color.RED, Color.BLUE). These constants
actually represent integers.
The TextView class has a method which allows you to set
the colour of text using these constants
main.setTextColor(Color.BLUE);
Markus A. Wolf
MAD Page 17 of 17
Use the Android Java API documentation (or intelliSence in
Android Studio) to find class TextView and look for the
method that allows you to set the colour of text.
Use the method to change the colour of the text displayed
by the HelloWorld app to green.
Can you make the colour different depending on the user's
mark?
Markus A. Wolf