0% found this document useful (0 votes)
37 views

Chapter3 Android Project

Uploaded by

Syamira Property
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Chapter3 Android Project

Uploaded by

Syamira Property
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 41

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://
www.kingoapp.com/root-tutorials/how-to-enable-usb-debugging-mode-on-android.htm
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
https://romannurik.github.io/AndroidAssetStudio/index.html
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
(filename.apk).
 .apk is similar to .exe on Windows

 Apps download from Google Play, or copied to


device as filename.apk

 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

 MainActivity.Java: Java code to define behavior,


actions taken when button clicked (intelligence)

 AndroidManifest.xml:
 Lists all screens, components of app

 Analogous to a table of contents for a book

 E.g. Hello world program has 1 screen, so


AndroidManifest.xml 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 AndroidManifest.xml
Hello World program

Start in AndroidManifest.xml
Read list of activities
(screens) Start execution
from Activity tagged Launcher

Create/execute activities
(declared in java files)
E.g. MainActivity.Java

Format each activity using layout


In XML file (e.g. Activity_my.xml)
Inside “Hello World” AndroidManifest.xml
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 AndroidManifest.xml
Read list of activities
(screens) Start execution
from Activity tagged Launcher

Create/execute activities
Next (declared in java files)
E.g. MainActivity.Java

Format each activity using layout


In XML file (e.g. Activity_my.xml)
Example Activity Java file
(E.g. MainActivity.java)

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 main.xml
Execution Order

Start in AndroidManifest.xml
Read list of activities
(screens) Start execution
from Activity tagged Launcher

Create/execute activities
(declared in java files)
E.g. MainActivity.Java

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

 AndroidManifext.XML: 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 Strings.xml
 Can declare all strings in strings.xml
String declaration in strings.xml

 Then reference in any of your app’s xml files


Strings in AndroidManifest.xml
 Strings declared in strings.xml can be referenced by all other XML files
(activity_my.xml, AndroidManifest.xml)

String declaration in strings.xml

String usage in AndroidManifest.xml


Where is strings.xml in Android Studio?
Editting any string in strings.xml 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

You might also like