CHAPTER 3
Android Project
Course Outline Chapter 3
• Android Project
• Starting a project
• Setting up emulator
• Creating Launch Configurations
• Running the Android App
• Understanding project structure
Setting up your Project
Afterinstalling Android Studio, need to set up your
project
Main steps to set up Android Project
Start a new Android Project
Configure new Android Project (select app name, domain name, etc)
Set platform and minimum SDK
Add an Activity
Start a new Android Project
Company domain: important because it is used to name
the Java package to which the code will belong.
Set Minimum
SDK
• How to check your smart phone
android version.
(depend on device type)
• Samsung galaxy Note3
• Settings-> General-> About device->
Android version
Add an Activity
(Empty/Blank Activity is Simplest)
• To add an Activity to
Mobile Screen.
• Example,
For Google Map Activity
option, Android Studio
will create a project with
a basic activity that
contains a Google Map in
it already.
• Choose Empty Activity
to create a basic
activity with no code in
it.
• It is Accepted practice in
Android development to name
main activity as MainActivity,
to make it easier to locate the
startup code.
• All other activity can be named
by its function. Example,
InputFormActivity,
DeleteRecordActivity.
• Layout file follows the ‘name’
naming convention. Example,
activity_main, activity_input,
activity_delete.
Running a Simple App
Main steps to run Android Project
Run Android Studio
Fix any remaining issues
Run (Android Virtual Device) AVD, select virtual device
Run App on selected virtual device
How to Run the App?
Click here to run the app
Where to Run Android App
Android app can run on:
Real phone (or device)
Change to developer mode https://
[Link]/root-tutorials/[Link]
Emulated phone in
Emulator (software version of phone) Android Studio Need USB cord to copy app over from
development PC to phone
Emulator Pros and Cons
Pros:
Conveniently test app on basic hardware by clicking in software
Easy to test app on various devices (phones, tablets, TVs, etc), various screen
sizes
Cons:
Some hardware missing, especially hardware for sensing environment
E.g. GPS, camera, video recording, etc
Emulator Limitations
No support for placing or receiving actual phone calls
Simulate phone calls (placed and received) through the emulator console
No support for USB connections
No support for camera/video capture (input)
No support for device‐attached headphones
No support for determining connected state
No support for determining battery charge level and AC charging state
No support for determining SD card insert/eject
No support for Bluetooth
No support for simulating the accelerometer
Use OpenIntents’s Sensor Simulator
Slow!!!
Run On Emulator
• Run Open AVD Manager
• Create Virtual Device / Choose
Virtual Device
• Run App on Virtual Device
Run App on Virtual Device (Phone)
Creating a Launcher Icon for
the Application
• When app is installed, its icon helps users identify its presence in the
application launcher.
• Android Studio automatically includes a default launcher icon
• To change the launcher logo of an app in Android Studio?
• Expand the project root folder in the Project View
• Right Click on the app folder
• In the Context Menu go to New->Image Asset
• In the pop up that appears select the the new logo you would like to
have(image/clip art/text).
• Can design launcher icon using android asset studio
[Link]
Android Apps: Big Picture
UI Design using XML
UI design code (XML) separate from the program
(Java)
Why? Can modify UI without changing Java
program
Example: Shapes, colors can be changed in XML file
without changing Java program
UI designed using either:
Drag-and drop graphical (WYSIWYG) tool or
Programming Extensible Markup Language (XML)
XML: Markup language, both human-readable and
machine-readable''
Android App Compilation
Android Studio compiles code, data and
resource files into Android PacKage
([Link]).
.apk is similar to .exe on Windows
Apps download from Google Play, or copied to
device as [Link]
Installation = installing apk file
Activities
Activity? 1 Android screen or dialog box
Apps
Have at least 1 activity that deals with UI
Entry point, similar to main( ) in C
Typically have multiple activities
Example: A camera app
Activity 1: to focus, take photo, launch activity 2
Activity 2: to view photo, save it
Activities
independent of each other
E.g. Activity 1 can write data, read by activity 2
App Activities derived from Android’s Activity class
Our First Android App
3 Files in “Hello World” Android Project
Activity_my.xml: XML file specifying screen layout
[Link]: Java code to define behavior,
actions taken when button clicked (intelligence)
[Link]:
Lists all screens, components of app
Analogous to a table of contents for a book
E.g. Hello world program has 1 screen, so
[Link] has 1 item listed
App starts running here (like main( ) in C)
Note: Android Studio creates these 3 files for
you
Execution Order Next: Samples of [Link]
Hello World program
Start in [Link]
Read list of activities
(screens) Start execution
from Activity tagged Launcher
Create/execute activities
(declared in java files)
E.g. [Link]
Format each activity using layout
In XML file (e.g. Activity_my.xml)
Inside “Hello World” [Link]
This file is written using xml namespace and tags and rules for android
Your
package
name
Android
version
List of
activities
(screens) One activity (screen)
in your app designated LAUNCHER.
The app starts running here
Execution Order
Start in [Link]
Read list of activities
(screens) Start execution
from Activity tagged Launcher
Create/execute activities
Next (declared in java files)
E.g. [Link]
Format each activity using layout
In XML file (e.g. Activity_my.xml)
Example Activity Java file
(E.g. [Link])
Package declaration
Import needed classes
My class inherits from
Android activity class
Initialize by calling
onCreate( ) method
of base Activity class
Note: Android calls your Activity’s onCreate Use screen layout (design)
method once it is created declared in file [Link]
Execution Order
Start in [Link]
Read list of activities
(screens) Start execution
from Activity tagged Launcher
Create/execute activities
(declared in java files)
E.g. [Link]
Format each activity using layout
Next In XML file (e.g. Activity_my.xml)
Simple XML file Designing UI
After choosing the layout, then widgets added to design UI
XML Layout files consist of:
UI components (boxes) called Views
Different types of views. E.g
TextView: contains text,
ImageView: picture,
WebView: web page
Views arranged into layouts or ViewGroups
Declare Layout
Add widgets
Widget properties
(e.g. center contents
horizontally and
Android Files
Android
Project
File
Structur
e
3 Main Files to
Write Android app
Files in an Android Project
res/ (resources) folder contains static resources
you can embed in Android screen (e.g. pictures,
string declarations, etc)
res/menu/: XML files for menu specs
res/drawable-xyz/: images (PNG, JPEG, etc) at
various resolutions
res/raw: general-purpose files (e.g. audio clips,
mpeg, video files, CSV files
res/values/: strings, dimensions, etc
Concrete Example: Files in an Android Project
res/layout: layout, dimensions (width, height) of
screen cells are specified in XML file here
res/drawable-xyz/: The images stored in jpg or
other format here
java/: App’s response when user clicks on a
selection is specified in java file here
[Link]: Contains app name
(Pinterest), list of app screens, etc
Editting in Android Studio
Editting Android
Can edit apps in:
Text View: edit XML directly
Design View: or drag and drop widgets unto emulated phone
Resources
Declaring Strings in [Link]
Can declare all strings in [Link]
String declaration in [Link]
Then reference in any of your app’s xml files
Strings in [Link]
Strings declared in [Link] can be referenced by all other XML files
(activity_my.xml, [Link])
String declaration in [Link]
String usage in [Link]
Where is [Link] in Android Studio?
Editting any string in [Link] changes it
wherever it is displayed
Styled Text
In HTML, tags can be used for italics, bold, etc
E.g. <i> Hello </i> makes text Hello
<b> Hello <b> makes text Hello
Can use the same HTML tags to add style (italics, bold, etc) to
Android strings
References
• Slides from Prof. Emmanuel Agu,
Worcester Polytechnic Institute, CS 528,
Mobile and Ubiquitous Computing
Class, Fall 2017
• 2017 J.F. DiMarzio, Beginning Android
Programming with Android Studio, 4th
Edition, Indianapolis, Indiana