Open Book Chapter 1 - 6
Open Book Chapter 1 - 6
NATIVE APP
Since the app is developed within a mature ecosystem following the technical and user
experience guidelines of the OS, it not only has the advantage of faster performance but also
“feels right”.
What feeling right means is that the in-app interaction has a look and feel consistent with
most of the other native apps on the device. The end user is thus more likely to learn how to
navigate and use the app faster.
HYBRID APP
A hybrid app is a web app built using HTML5 and JavaScript, wrapped in a native container
which loads most of the information on the page as the user navigates through the
application (Native apps instead download most of the content when the user first installs
the app).
MVC – MODEL(JAVA), CONTROLLER (ANDROID ACTIVITY), VIEW (XML)
► A layout defines the structure for a user interface in your android app, such as in an Activity.
► All elements in the layout are built using a hierarchy of View and ViewGroup objects. A View
usually draws something the user can see and interact with. Whereas ViewGroup is an
invisible container that defines the layout structure for View and other ViewGroup objects.
► Instantiate layout elements at runtime. Your app can create View and ViewGroup
objects (and manipulate their properties) programmatically.
CREATE PROGRAMMATICALLY XML
► Declaring your UI in XML allows you to separate the presentation of your app from the code
that controls its behavior. Using XML files also makes it easy to provide different layouts for
different screen sizes and orientations
► XML is a set of rules for forming semantic tags that break a document into parts and identify
the different parts of the document.
► XML, is a metamarkup language, in which you make up the tags you need as you go along.
► These tags must be organized according to certain general principles, but they're quite
flexible in their meaning.
► For instance, if you're working on genealogy and need to describe people, births, deaths,
burial sites, families, marriages, divorces, and so forth, you can create tags for each of these
elements.
■ For example, a tag may indicate that words are written in italics or bold type.
■ Although the most common and most widely used markup languages are written for
computers, the concept of a markup language is not limited to computer
programming.
SGML
■ The first drive for XML was, the Standard Generalized Markup Language (SGML).
■ SGML is an international standard for semantic tagging of documents that's been around for
a little over a decade.
■ SGML is intended for semantic markup that assists computer cataloging and indexing, and
can be extended in an infinite variety of ways to handle new data formats.
■ Although SGML has its advantages, it is too complex and expensive for anyone to put their
information on the Internet.
WHAT IS SGML?
■ SGML is not in itself a document language, but a description of how to specify one.
■ It is metadata
HTML
■ HTML, invented by Tim Berners-Lee at CERN in 1990, HTML was designed as a simple
replacement for SGML that could be written without the assistance of expensive authoring
tools.
■ Both SGML and HTML have shortcomings, and a new approach was needed to combine their
strengths without their limitations XML.
WHAT IS HTML?
■ HTML (Hypertext Markup Language) is the set of markup symbols or codes inserted
in a file intended for display on a World Wide Web browser page.
■ The markup tells the Web browser how to display a Web page's words and images
for the user.
■ Each individual markup code is referred to as an element (but many people also
refer to it as a tag).
SGML
XML
HTML
This allows it to be processed by generic processors (XML parser) that traverse the
document and create an internal tree representation.
An element may not have two attributes with the same name.
start with letters (including non-Latin characters) or the” _" character, but not
numbers or other punctuation characters.
After the first character, numbers are allowed, as are the characters "-" and ".".
Names can't start with the letter’s "xml", in uppercase, lowercase, or mixed – you can't start a
name with "xml", "XML", "XmL", or any other combination.
<root>
<child>
<subchild>..</subchild>
<subchild>..</subchild>
</child>
An element may not have two attributes with the same name.
Example: http://www.matuszek.org/ns
Associate a prefix with a namespace, then use the prefix in the XML to refer to the
namespace
NAMESPACE SYNTAX*
In any start tag you can use the reserved attribute name xmlns:
<book xmlns="http://www.matuszek.org/ns">
This namespace will be used as the default for all elements up to the corresponding end
tag
<book xmlns:dave="http://www.matuszek.org/ns">
Use this prefix on every tag and attribute you want to use from this namespace,
including end tags--it is not a default prefix
You can use the prefix in the start tag in which it is defined:
<dave:book xmlns:dave="http://www.matuszek.org/ns">
COMMENTS
CHAPTER 3
WHAT IS ANDROID?
● Android is a Linux-based platform for mobile devices.
● Android is a mobile operating system based on a modified version of the Linux kernel and
other open source software, designed primarily for touchscreen mobile devices such as
smartphones and tablets.
• Documentation (developers.android.com)
• Sample code
ANDROID STUDIO
• Virtual devices
• Project views
Dalvik Virtual
Native Libraries
Machine (VM)
(C/C++ code)
Novel Java Virtual
Graphics (Surface Machine
Manager)
implementation (not
Multimedia (Media
Framework)
using the Oracle
Database DBMS JVM)
(SQLite) Open License (Oracle
Font Management JVM is not open!)
(FreeType) Optimized for
WebKit memory-constrained
C libraries (Bionic) devices
…. Faster than Oracle
JVM
….
Linux Kernel
● Security features
● Drivers
C/C++ libraries
● Core C/C++ Libraries give access to core native Android system components and services.
Android runtime
● Each app runs in its own process with its own instance of the Android Runtime.
DALVIK VM
• All applications written in Java are converted to the dalvik executable .dex
• Every android app runs its own process, with its own instance of the dalvik virtual machine
• Not a traditional JVM, but a custom VM designed to run multiple instances efficiently on a
single device
• VM uses linux kernel to handle low-level functionality incl. security, threading, process and
memory management
Application
Framework
(Core Components of
Android)
Activity Manager
Packet Manager
Telephony Manager
Location Manager
Contents Provider
Notification
Manager
….
Applications
(Written in Java code)
● Notification manager
APPLICATION FRAMEWORK
- apps can include lists, grids, text boxes, buttons, web browser
● Content Providers
● Resource Manager
● Notification Manager
● Activity Manager
APPLICATIONS
• All apps (native and 3rd party) are written using the same APIs and run on the same run time
executable
• All apps have APIs for hardware access, location-based services, support for background
services, map-based activities, 2D and 3D graphics.
CHAPTER 4
APP COMPONENTS
ACTIVITIES
BROADCAST RECEIVER
CONTENT PROVIDERS
• Identified by a URI.
● View is an object that draws something on the screen that the user can interact with
● ViewGroup is a container that holds Views and other ViewGroup objects and defines layout
of all or part of the screen
● ViewGroup is an invisible container that organizes child Views (some of which might
themselves be ViewGroups)
● Child Views inside a ViewGroup are widgets or input controls that draw some part of the UI
○ It takes more time and memory to construct and display a complex View hierarchy
● Declare your layout either
Name of XML element for a View is the same as the class name of the View
(e.g., TextView, LinearLayout)
When XML layout loaded, each node inflated into a runtime object
LAYOUT
● You can declare Layouts either in your Java code or in an XML layout file
● Declaring UI in XML resource lets you separate presentation from logic and behaviour
● Can create XML layouts for different screen orientations, sizes and densities or different
languages
● XML vocabulary for defining layouts follows naming of classes and methods
● Each layout file contains one root element, which must be (i.e., correspond to) a View or a
ViewGroup object
● The root element contains child elements which correspond to Views (often widgets) and
ViewGroups (often Layouts)
● The layout XML file should end with “.xml” and be stored in the res/layout/ directory
LOADING AN XML LAYOUT RESOURCE
● When app is compiled, each XML layout file is compiled into a View resource
● Load the layout by calling setContentView() from the onCreate() callback implementation in
your Activity class
● Pass setContentView() the id of the layout resource, as defined in the R.java file
ATTRIBUTES
● Each View and ViewGroup element in an XML layout file has its own set of attributes (e.g.,
id, text)
• These attributes are inherited by any class that subclasses View or ViewGroup
● Some attributes are layout parameters that describe layout orientations of a View object
(e.g., layout_width, layout_height)
ID
● +: indicates that this is the first time the resource is being defined and that it should be
added to R.java
ANDROID RESOURCES
● Here you do not use the “+” symbol, since you are not defining the ID, just using one that
has already been defined
● You declare the type of the resource to be “@android:id” to indicate that the id is defined in
the android package namespace
● First define the view or widget in the layout file and assign a unique id
● Then create an instance of the View and capture it from the layout (usually in onCreate())
LAYOUT PARAMETERS
● The ViewGroup.LayoutParams class inside a ViewGroup defines size and position of each of
its child Views
● Every LayoutParams subclass has its own syntax for setting values
● Each child element must define LayoutParams appropriate for its parent
● A child ViewGroup may also define different LayoutParams for its own children
○ Every view in the ViewGroup must define these in its XML declaration
● View has left, top, width and height properties, expressed in pixels relative to the parent
● getWidth(), getHeight()
○ actual width and height of View on screen at drawing time after layout
● getMeasuredWidth(), getMeasuredHeight()
COMMON LAYOUTS
LINEARLAYOUT
● LinearLayout is a ViewGroup that aligns all its children in a single row or column
LINEARLAYOUT EXAMPLE
RELATIVELAYOUT
● RelativeLayout can eliminate nested ViewGroups and keep the View hierarchy flat
○ improves performance!
POSITIONING VIEWS
● RelativeLayout lets child views specify their positions relative to each other and the
containing Layout
○ e.g., align two elements by right border, make one below another, centered in the
screen, centered left, etc.
● android:layout_alignParentTop
● android:layout_centerVertical
● android:layout_below
● android:layout_toRightOf
○ Left edge of this View to the right of View specified with resource ID
CHAPTER 5
WHAT IS AN ACTIVITY?
● Typically fills the screen, but can be embedded in other Activity or a appear as floating
window
● Handles user interactions, such as button clicks, text entry, or login verification
● Has a life cycle—is created, started, runs, is paused, resumed, stopped, and destroyed
IMPLEMENTING ACTIVITIES
@Override
super.onCreate(savedInstanceState);
1. Create an Intent
○ Intent intent = new Intent(this, ActivityName.class);
2. Use the Intent to start the Activity
○ startActivity(intent);
To ask Android to find an Activity to handle your request, use an implicit Intent
1. Create an Intent
○ startActivity(intent);
● putExtras(bundle);
⇒ if lots of data, first create a bundle and pass the bundle.
SENDING DATA TO AN ACTIVITY WITH EXTRAS
intent.putExtra(EXTRA_MESSAGE_KEY, message);
startActivity(intent);
getData();
⇒ Uri locationUri = intent.getData();
startActivityForResult(intent, requestCode);
● When done, pop stack, return to previous Activity, and execute onActivityResult() callback to
process returned data
STARTACTIVITYFORRESULT() EXAMPLE
startActivityForResult(intent, CHOOSE_FOOD_REQUEST);
// Create an intent
replyIntent.putExtra(EXTRA_REPLY, reply);
setResult(RESULT_OK, replyIntent);
finish();
IMPLEMENT ONACTIVITYRESULT()
}}}
ACTIVITY STACK
● When a new Activity is started, the previous Activity is stopped and pushed on the Activity
back stack
● Last-in-first-out-stack—when the current Activity ends, or the user presses the Back button,
it is popped from the stack and the previous Activity resumes
Ancestral or up navigation
BACK NAVIGATION
● Back stack contains all the Activity instances that have been launched by the user in reverse
order for the current task
UP NAVIGATION
● Set parentActivityName
<activity
android:name=".ShowDinnerActivity"
android:parentActivityName=".MainActivity" >
</activity>
ACTIVITY STATES AND APP VISIBILITY
● Started (visible)
● Resume (visible)
● Stopped (hidden)
State changes are triggered by user action, configuration changes such as device rotation, or system
action
onStop()—no longer visible, but still exists and all state info preserved
Configuration changes invalidate the current layout or other resources in your activity when the
user:
● onPause()
● onStop()
● onDestroy()
● onCreate()
● onStart()
● onResume()
CHAPTER 6
Input controls such as keyboards send input to the view that has focus
WHICH VIEW GETS FOCUS NEXT?
● After user submits input, focus moves to nearest neighbor—priority is left to right, top to
bottom
● Activity.getCurrentFocus()
● ViewGroup.getFocusedChild()
● datetime: Show a numeric keypad with a slash and colon for entering the date and time
TEXTVIEW FOR TEXT
● Set text:
● Use <b> and <i> HTML tags for bold and italics. All other HTML tags are ignored
● String resources: one unbroken line = one paragraph. \n starts a new a line or paragraph
● Escape apostrophes and quotes with backslash (\", \’). Escape any non-ASCII characters with
backslash (\)
<TextView android:id="@+id/textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/my_story"/>
android:text—text to display
android:textColor—color of text
android:textSize—text size in sp