0% found this document useful (0 votes)
8 views83 pages

L01b-Foundation and Tools

Uploaded by

siemroyal99
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views83 pages

L01b-Foundation and Tools

Uploaded by

siemroyal99
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 83

Lecture 01b

Foundation and Tools

1
Lecture Overview
•Programs and Operating Systems
• What is Android (for a programmer)?
• Android - Device and Interaction Overview

• Development Tools

• Configuring the IDE for Android Development

• Using the Logger

• Building a Simple App.

• Building an Interactive App.


Programs and Operating Systems
• All programs eventually execute on Hardware

• They all make use of services from O/S

Program

Operating Systems

Hardware

3
We program to an API

•Programs use services from O/S (via API)

Program

API
Operating Systems

Hardware

4
API is good - but frameworks are better

• Android and iOS are more than O/S

• They are platforms and hence include,

• Operating System (with primitive API)

• Frameworks (incl. Components/Libraries)

Program

Android Frameworks
iOS
API
Operating Systems
Android
Programming languages:
Java,C/C++,Kotlin

Image Source: http://developer.android.com


7
We write to a framework/library

• We are developing programs to use a


framework

• Knowledge of framework is very important


• Android / iOS have very different frameworks

• Different data structures & algorithms

• Different culture

• But ... broad concepts are similar

• You can learn iOS faster if you know Android (vice-


versa)
Lecture Overview - Where are we?
•Programs and Operating Systems
•What is Android (for a programmer)?
• Android - Device and Interaction Overview
• Development Tools

• Configuring the IDE for Android Development

• Using the Logger

• Building a Simple App.

• Building an Interactive App.


Android is a bi-lingual platform
C/C++ Java

Image Source: http://www.tbray.org


10
What is Dalvik?
• Dalvik is a Virtual Machine

• Dalvik executes the compiled Java code

• Dalvik uses a ‘register-based architecture’

• It is NOT a stack machine like the JVM

• Stack machine Vs Register machines

• Data is loaded and manipulated differently

• Register based -> quicker load and execution, but


slightly larger program size
What is in the Android Framework?

•All of these are available via Java (or C/C++)

Image Source: http://www.tbray.org


12
Android Standard Libraries

•Java & C/C++ code can access these libraries

Background Image Source: http://www.tbray.org


13
Where do we start with Android?
Simple Graphical Applications

14
Some Assumptions (from now on...)
• You know how to build Java programs

• You also know,

• Package organization system of Java

• Aware of commonly used standard libraries

• Can use a command interface (Terminal in OSX,


MS Dos console in Windows)

• Can read Java code and understand logic/flow


• Interface inheritance
If your Java is rusty

•You have some catching up to do....

Free e-Book at http://www.mindview.net/Books/TIJ/


16
If you know these, you will learn faster

• Event handling

• By implementing listener interfaces

• By use of anonymous inner classes

• Hash Tables / Maps (concept + code)

• Singleton, Factory, Visitor design patterns

• Have built user interfaces in another


language/framework (incl. Swing/AWT)

• Call-backs
Lecture Overview - Where are we?
•Programs and Operating Systems
•What is Android (for a programmer)?
•Android - Device and Interaction Overview
• Development Tools
• Configuring the IDE for Android Development

• Using the Logger

• Building a Simple App.

• Building an Interactive App.


Android Device User Interaction
•Android has special “buttons”

Apart from these


buttons, vendors can
add more buttons (if
they wish)
Menu
Back
Home
19
Home Button...

• Typically, this will take you back to the Home Screen


of the phone

• Same (default) behavior as the iPhone / iPad button

Home
20
Back Button
• This will take you back to the previous screen

• If app. has only one screen, this will exit app.

• R.when
Vasa Personal Note: I miss this button the most
I use the iPhone / iPad)

Back
Menu Button (mainly older models)

• This will show a contextual menu for a screen (if one is


available)

• Developers can write their own menu for any screen


in their app.

• Quite handy (iOS should borrow this idea)

Menu (as open)


Android (Nexus Devices)

Back Home Recent Apps


23
Lecture Overview - Where are we?
•Programs and Operating Systems
•What is Android (for a programmer)?
•Android - Device and Interaction Overview
•Development Tools
•Configuring the IDE for Android Development
•Using the Logger
•Building a Simple App.
24
Development Tools
• iOS - XCode IDE + iOS SDK (from Apple)

• Offers a robust/powerful Visual UI Builder

• Android

• Android SDK (Compiler and Emulator)

• Android studio

• Debugger is powerful

• Visual UI Builder (is getting better)

25
IDE, SDK and Emulator
• IDE - Editor where we write the code

• SDK - Tools that will compile our code

• Emulator - Runs the code

• Emulator runs as a separate application


• IDEthe codecommunicates to this emulator to deploy

• Debugger (in IDE) also works with emulator


26
Android IDE
Android IDE Graphical User
Android Emulator

Permits checking Portrait


and Landscape views

29
Emulator is nice .. but phone is better

• The emulator runs the Android O/S (you can run any
version from 1.6 up)

• Emulates the phone hardware (like VMWare)


• Emulator does not have sensors (e.g. GPS, Gyro,
Accelerometer, Cell phone etc.)

• Offers ways to simulate sensor data


Emulators Vs Simulator
• iOS offers a simulator

• Android has gone down the emulator path

• Trade-offs (pros and cons),

• Simulators start-up faster, good enough

• Emulators allow checking against rel. 1.6 of the


O/S easily + closer to phone hardware

• Emulators and Simulators cannot mimic real


CPU speed, disk speed, network speed etc.
(yet!)
You need a phone (for serious work)
• Emulator + Simulators are great to learn with

• Commercial (or serious work) - Need Phone

• Why?

• Mobile apps. are context based (& mobile)

• Best way to test them is in their natural


environment

• Sticky message: “Cannot get users


around with an emulator to their head”
to walk
Short Problem 1 (True Story)
• An app. to learn Japanese

• Built around Flash Card metaphor

• Database of 145k words

• During development,

• Search was super-quick

• When tested on phone,

• Search was taking too long

• Why?

33
Lets get our feet wet

34
Demo 1
• Run IDE + Create Simple Hello World

• Send it to Emulator

• Watch what happens on Console View

• Send an SMS to the Emulator

35
Lecture Overview - Where are we?
• Programs and Operating Systems

• What is Android (for a programmer)?

• Android - Device and Interaction Overview

• Development Tools

• Building a Simple App.

• Configuring IDE for Additional Insight

• Using the Logger


Building a Simple App.

37
What is involved?
• Place UI controls (Text and Image)

• Layout the controls

• Centre text

• Make text large font

• Display Image to take up all space

38
Knowledge Needed to Build it...
• What UI Controls available?

• How do we place/layout the UI controls?

• How do we set the start screen?

• How is the UI Constructed & Destroyed

• How exactly do we start/exit the app.?

39
Android Offers a number of UI Controls

•Offers all standard controls and lot more...

Progress
These are
a small
subset of
available
controls

40
Each Screen is an Activity

•Android app. is made up of Activities

Activity

41
Views are Android’s Key UI Building Block

• We need two views on the Activity

TextView

ImageView

42
Views are Placed inside a View Group
• Different types of View Groups are provided

• Each View Group offers a different method to layout


the Views

View Group
(Linear Layout)

43
View Groups and Layouts
•Android offers the following View Groups,
•Linear Layout (Horizontal or Vertical)
•Absolute Layout (You set X, Y for each View)
•Table Layout (Rows and Columns)
•Relative Layout (Position relative to other Views)
•Frame Layout (Show only one view at a time)
•Scroll View (Permits scrolling of the screen)
•View Groups can be nested
44
Linear Layout
•Two Linear Layouts with 4 Views each
•Nested View Groups
•Activity contains,
•Linear Layout (Vertical)
•Linear Layout (Horizontal)
•Linear Layout (Vertical)
•8 Text Views
Source code:
http://developer.android.com/resources/tutorials/views/hello-linearlayout.html

45
How do we program the Layout?
• Layout can be written as an XML file

• Layout can be also be written in Java

• XML file is easier to start off with


Layout Definition XML File
Concept Recap

• We use a View Group to place and control where


Views are shown

• An App. can have one or more Activities


Activity View Group Views
(Layout)

47
Demo 3 - Linear Layout Control

• The layout manager offers control on how much a


View will stretch

• You can set width / height of each View to,


• Match Parent (same width/height as parent)

• Fill Parent (cover up parent)

• Wrap Content (use only what is needed)

• You can use Gravity to position View

• You can combine using bit masking

• You can also assign weight to Views


Common Layouts
•Recommended Reading:
• http://developer.android.com/guide/topics/ui/
layout-objects.html
•Layo
uts
take some time to learn

•Try different layouts and adjust options


•Change the background colour of the views to see
how the layout works

•Remember -- gravity and weight adjustment

49
Generated Code, Layout & Resources
• How did Australia image get into the App.?

• How did we set the text to “Australia”?

• Recall: Convention Over Configuration


• Layout Defined in /layout/main.xml

• Resources Placed in /res/drawable-

• String values defined in /values/strings.xml

50
Resource Definition

• All images (and media) are placed in “res”


directory -- within subfolders

• Android SDK tools will generate R.java


contains a reference to these resources
which

51
Resources and Generated Code

A reference to these
gets automatically
generated by the
Android SDK tools

52
Resources and Generated Code

Generated Code

static final int australia=0x7f020000;

53
A Reference to Layout also Generated

All References are Integers

54
Concept Recap

• We use a View Group to place and control where


Views are shown

• An App. can have one or more Activities


Activity View Group Views
(Layout)

55
How does Activity know about layout?
This is the Activity Class

Reference to the layout


(Android Runtime will Render this Layout)
56
Who writes the Activity code?

This block of code is created by IDE/SDK


when we create new Android project
You can also write you own -- more on that later in semester

57
Activity Creation

Method called (by the Application


Launcher) when Activity is first created

58
Activity Creation - Layout Rendeing

Method call will pass the reference to the layout


that needs to be rendered on the screen

(We will discuss the “rending model” later in the semester)


59
What is the “root” Activity?

• How does Android know which Activity to create


first?

Answer: Application Manifest File

60
Application Manifest File
Application Icon Reference

Activity Name Category


61
Concept Recap
Activity Activity Class (Java)

View Group
(Layout)

Layout Definition
(main.xml)

62
Layouts and Resources

•How did the Australia image get into layout?

Layout can refer to resources


@drawable is a special tag
63
Layouts and String Information

•How did we set the text to “Australia”

String constant

64
Layout and String Information

• String information can come from constant values


defined in values.xml

Context Help shows


recommendations

65
String Constant Values
Defined as a
resource

Refer using an @string tag


66
What is it with the @ tag?

• Constant Resources in Android can be referred


using the “@” tag in XML layout

• Example: @drawable, @string ...


@drawable/australia

@drawable/bots

67
Significance of hdpi/ldpi/mdpi

Different resolution images

High-Res, 240 dpi screen

Low-Res, 120 dpi screen

Med-Res, 160 dpi screen

68
@ Tag and Multiple Screen
Resolutions
• Dealing with multiple screen resolutions

@drawable/icon

Android Runtime decides best resource to use based on hardware capabilities


Drawable at Different Resolutions

High

Low

Medium

70
Drawable at Multiple Resolutions
• You should ideally provide these images

• Runtime will try to

• Scale down Highest resolution available, (or)

• But, Low may need scaling up (yuk!)

• Images at different resolution must be called the


“same name”

• SDK convention based tools will select best option

71
Lecture Overview - Where are we?
• Programs and Operating Systems

• What is Android (for a programmer)?

• Android - Device and Interaction Overview

• Development Tools

• Building a Simple App.

• Configuring IDE for Additional Insight

• Using the Logger


Developers need more insight...

• You need a lot of information when


developing apps:

Views

Need these to see program behaviour


(there is no console or MS-DOS prompt)
73
What do developers need?
• Ability to control the Emulator

• Ability to watch the messages between IDE and


Emulator (things are not always perfect)

• Ability to inspect the processes running on the


emulator (or phone)

• Ability to watch the threads and resources used


by various apps.

• Ability to inspect “LOG” messages


Lecture Overview - Where are we?
•Programs and Operating Systems
•What is Android (for a programmer)?
•Android - Device and Interaction Overview
•Development Tools
•Building a Simple App.
•Configuring IDE for Additional Insight
•Using the Logger
75
Can you program without println?
•Guess what? We cannot do ...
• System.out.println(“I am here!”);
•Same issue in iOS and Android
•These devices do not have a console
• But, we can log messages and watch them
• In Android, we can do,

• Log.d(“TAG”, “I am here!”);

76
Log Messages in Android

77
Demo 2
• Logging a message in Android

• Multiple levels of logging

• Verbose (Log.v(“TAGTAG”, “MethodX Called”);

• Debug (Log.d(“BLAH”, “x should be 34”);

• Information (Log.i(“TAGBLAH”, “Socket open”);)

• We also have Warning, Error, Fatal, Silent

• You can stop log messages from showing

• http://developer.android.com/reference/android/util/Log.html (See last


few methods .. you may say @#$)
Logging
• Android has Log class, iOS has NSLog

• When shipping production/final


should remove these Log methods
code -- we

• iOS -- Use a #define in the make script


• Android -- Adjust configuration of ProGuard

• What is ProGuard?

• Code optimisation/obfuscation
discuss later...)
tool (we will

• Bundled as part of Android SDK


Concept Recap
• Key Buttons -- Home, Back, Menu

• IDE, SDK, Emulator

• Customizing IDE to show additional info.

• Logging, Emulator Control, Console

• Sending messages to emulator

• Use of the Log class


Lecture Overview - Where are we?
• Programs and Operating Systems

• What is Android (for a programmer)?

• Android - Device and Interaction Overview

• Development Tools

• Building a Simple App.

• Configuring IDE for Additional Insight

• Using the Logger


Recommended Reading
• Android Developers Guide
• http://developer.android.com

• Common Layouts
• http://developer.android.com/guide/topics/ui/layout-objects.html

You might also like