53
UI Concepts Unlocking Android Chapter 4

Unlocking Android Chapter 4. Understanding activities and views Exploring the Activity lifecycle Working with resources Defining the AndroidManifest.xml

Embed Size (px)

Citation preview

Page 1: Unlocking Android Chapter 4.  Understanding activities and views  Exploring the Activity lifecycle  Working with resources  Defining the AndroidManifest.xml

UI ConceptsUnlocking Android

Chapter 4

Page 2: Unlocking Android Chapter 4.  Understanding activities and views  Exploring the Activity lifecycle  Working with resources  Defining the AndroidManifest.xml

Understanding activities and views Exploring the Activity lifecycle Working with resources Defining the AndroidManifest.xml

Goal

Page 3: Unlocking Android Chapter 4.  Understanding activities and views  Exploring the Activity lifecycle  Working with resources  Defining the AndroidManifest.xml

Understanding activities

Page 4: Unlocking Android Chapter 4.  Understanding activities and views  Exploring the Activity lifecycle  Working with resources  Defining the AndroidManifest.xml

Views and other Android components◦ compiled into a binary form

R.java◦ available to applications as

resources◦ make use of strings, colors,

styles, and graphics R.java class

◦ automatically generated◦ provides a reference to

individual resources ◦ is the bridge between binary

references and source.◦ to grab a string or a color and add

it to a View. AndroidManifest. Xml

◦ where your application begins◦ what its permissions are◦ Activities (services and

receivers ) it includes

Page 5: Unlocking Android Chapter 4.  Understanding activities and views  Exploring the Activity lifecycle  Working with resources  Defining the AndroidManifest.xml

Requirements◦ Allows the user to search for restaurant reviews based

on location and cuisine. ◦ Allow the user to call, visit the website of, or map

directions to a selected restaurant. Need three basic screens to begin with:

◦ A criteria screen where a user enters parameters to search for restaurant reviews

◦ A list-of-reviews screen that shows paged results that match the specified criteria

◦ A detail page that shows the review details for a selected review item

A screen =an Activity

RestaurantFinder

Page 6: Unlocking Android Chapter 4.  Understanding activities and views  Exploring the Activity lifecycle  Working with resources  Defining the AndroidManifest.xml
Page 7: Unlocking Android Chapter 4.  Understanding activities and views  Exploring the Activity lifecycle  Working with resources  Defining the AndroidManifest.xml
Page 8: Unlocking Android Chapter 4.  Understanding activities and views  Exploring the Activity lifecycle  Working with resources  Defining the AndroidManifest.xml

1 it gives our application a context,

◦ because Activity itself extends android.app.ApplicationContext;

it brings the Android lifecycle methods into play;

it gives the framework a hook to start and run your application; and

it provides a container into which View elements can be placed.

Page 9: Unlocking Android Chapter 4.  Understanding activities and views  Exploring the Activity lifecycle  Working with resources  Defining the AndroidManifest.xml

Needs three Views ◦ because an Activity represents an interaction with the

user, it needs to provide components on the screen. ◦ location, cuisine, and grabReviews

Location◦ is a type of View ◦ known as an EditText

cuisine ◦ is a fancy select list component, ◦ known in Android terms as a Spinner, and

grabReviews ◦ is a Button.

2

Page 10: Unlocking Android Chapter 4.  Understanding activities and views  Exploring the Activity lifecycle  Working with resources  Defining the AndroidManifest.xml

This is one of a series of important lifecycle methods the Activity class provides.

Every Activity will override onCreate(), ◦ component initialization steps are invoked, ◦ not every Activity will need to override other

lifecycle methods.

3

Page 11: Unlocking Android Chapter 4.  Understanding activities and views  Exploring the Activity lifecycle  Working with resources  Defining the AndroidManifest.xml

setContentView() ◦ normally associate an XML layout file◦ you do not have to use an XML file at all;

you can instead define all of your layout and View configuration in code, as Java objects.

◦ it is often easier, and better practice use an XML layout resource decoupling, each xml layout file for each Activity.

An XML layout file ◦ defines View objects,

are laid out in a tree, can then be set into the Activity for use.

4

Page 12: Unlocking Android Chapter 4.  Understanding activities and views  Exploring the Activity lifecycle  Working with resources  Defining the AndroidManifest.xml

Dynamic Views ◦ Can bind to data, ◦ can be referenced in code ◦ cast to their respective subtypes

Static Views ◦ don’t need to interact with or update at runtime

like labels, ◦ do not need to be referenced in code

5

Page 13: Unlocking Android Chapter 4.  Understanding activities and views  Exploring the Activity lifecycle  Working with resources  Defining the AndroidManifest.xml

“Adapter” concept◦ link views that contain collections (AdapterView) with data. ◦ a collection handler that returns each item in the collection as a View.

An Adapter ◦ is an interface◦ a bridge between an AdapterView and the underlying data for that view. ◦ provides access to the data items ◦ making a View for each item in the data set◦ many basic adapters:

ListAdapter, ArrayAdapter, GalleryAdapter, CursorAdapter, And more. can create your own Adapter

An AdapterView a subclass of view and viewgroup an abstract class is a view whose children are determined by an Adapter◦ commonly used subclasses of AdapterView◦ ListView, GridView, Spinner, and Gallery 

6

Page 14: Unlocking Android Chapter 4.  Understanding activities and views  Exploring the Activity lifecycle  Working with resources  Defining the AndroidManifest.xml

using an ArrayAdapter that is populated with◦ Context (this), ◦ a View element defined in an XML resource file

used for the element shown in the Spinner before it is selected;

◦ an array representing the data After the spinner selected

◦ Set a different View (R.layout.spinner_view_dropdown) for the drop-down

7- ArrayAdapter

Page 15: Unlocking Android Chapter 4.  Understanding activities and views  Exploring the Activity lifecycle  Working with resources  Defining the AndroidManifest.xml

Once our Adapter and its View elements are defined, we set it into the Spinner object

Handle event◦ OnClickListener with our Button, in order to

respond when the button is clicked

8-9

Page 16: Unlocking Android Chapter 4.  Understanding activities and views  Exploring the Activity lifecycle  Working with resources  Defining the AndroidManifest.xml

After the onCreate() method is complete, with the binding of data to our Spinner views, we have menu buttons and associated actions. ◦ different than on-screen Button views

Menu Button vs on-screen Button◦ is invoked by pressing the Menu button on the

device and tapping a selection (button and a tap)

◦ an on-screen button (single tap)

menu buttons

Page 17: Unlocking Android Chapter 4.  Understanding activities and views  Exploring the Activity lifecycle  Working with resources  Defining the AndroidManifest.xml
Page 18: Unlocking Android Chapter 4.  Understanding activities and views  Exploring the Activity lifecycle  Working with resources  Defining the AndroidManifest.xml

Menu class add() method to create a single MenuItem element B. ◦ passing a group ID, ◦ An ID, ◦ an order, and ◦ a text resource reference to create the menu item.

Assigning to the menu item an icon with the setIcon method. ◦ The text and the image are externalized from the code, again

using Android’s concept of resources. The MenuItem

◦ duplicates the on-screen Button with the same label for the “Get reviews” purpose.

Page 19: Unlocking Android Chapter 4.  Understanding activities and views  Exploring the Activity lifecycle  Working with resources  Defining the AndroidManifest.xml

onMenuItemSelected() event method C,where we parse the ID of the multiple possible menu items with a case/switch statement.

Page 20: Unlocking Android Chapter 4.  Understanding activities and views  Exploring the Activity lifecycle  Working with resources  Defining the AndroidManifest.xml

HandleGetReviews• puts the user’s selection state in the Application object • sets up to call the next screen. • used in multiple places,

1. from our on-screen Button2. from our MenuItem.

Page 21: Unlocking Android Chapter 4.  Understanding activities and views  Exploring the Activity lifecycle  Working with resources  Defining the AndroidManifest.xml

Application object Used internally by Android for many purposes, Can be extended,

◦ RestaurantFinderApplication ◦ includes a few member variables in JavaBean style

Store global state information◦ Can be retrieve later

One way to pass objects back and forth between activities;◦ You can also use public static members and Intent extras

with Bundle objects. ◦ You can use the provided SQLite database, or◦ You can implement your own ContentProvider and store

data there.

Page 22: Unlocking Android Chapter 4.  Understanding activities and views  Exploring the Activity lifecycle  Working with resources  Defining the AndroidManifest.xml

asking another Activity to respond to the user’s selection of a menu item by calling startActivity (Intent intent)

Page 23: Unlocking Android Chapter 4.  Understanding activities and views  Exploring the Activity lifecycle  Working with resources  Defining the AndroidManifest.xml

•validate()• Called before we allow the next

Activity to be invoked, • display a pop-up-style alert dialog

to the user if the location has not been specified.

•OnClickListener() • button can be made to respond to a

click event

AlertDialog.Builder(Context context)Constructor using a context for this builder and the AlertDialog it creates.

Page 24: Unlocking Android Chapter 4.  Understanding activities and views  Exploring the Activity lifecycle  Working with resources  Defining the AndroidManifest.xml

Exploring the Activity lifecycle

Page 25: Unlocking Android Chapter 4.  Understanding activities and views  Exploring the Activity lifecycle  Working with resources  Defining the AndroidManifest.xml

Every process running on the Android platform is placed on a stack.

Android can’t keep every process running forever◦ system resources are finite.

How to use the limit resources?◦ When you use an Activity in the foreground, the

system process that hosts that Activity is placed at the top of the stack,

◦ previous process (the one hosting whatever Activity was previously in the foreground) is moved down one notch

Activity lifecycle

Page 26: Unlocking Android Chapter 4.  Understanding activities and views  Exploring the Activity lifecycle  Working with resources  Defining the AndroidManifest.xml

Foreground activity◦ at the top of the screen that the user is currently

interacting with  Visible activity

◦ an activity that is visible to the user but not in the foreground,

◦ such as one sitting behind a foreground dialog Background activity 

◦ Any process hosting a background Activity is next in line.

Empty process ◦ Any process not hosting any Activity (or Service or

BroadcastReceiver)is last in line.

Process priorities

Page 27: Unlocking Android Chapter 4.  Understanding activities and views  Exploring the Activity lifecycle  Working with resources  Defining the AndroidManifest.xml
Page 28: Unlocking Android Chapter 4.  Understanding activities and views  Exploring the Activity lifecycle  Working with resources  Defining the AndroidManifest.xml
Page 29: Unlocking Android Chapter 4.  Understanding activities and views  Exploring the Activity lifecycle  Working with resources  Defining the AndroidManifest.xml
Page 30: Unlocking Android Chapter 4.  Understanding activities and views  Exploring the Activity lifecycle  Working with resources  Defining the AndroidManifest.xml
Page 31: Unlocking Android Chapter 4.  Understanding activities and views  Exploring the Activity lifecycle  Working with resources  Defining the AndroidManifest.xml

Working with Views

Page 32: Unlocking Android Chapter 4.  Understanding activities and views  Exploring the Activity lifecycle  Working with resources  Defining the AndroidManifest.xml

View- building blocks of the UI

Related to layout

Page 33: Unlocking Android Chapter 4.  Understanding activities and views  Exploring the Activity lifecycle  Working with resources  Defining the AndroidManifest.xml
Page 34: Unlocking Android Chapter 4.  Understanding activities and views  Exploring the Activity lifecycle  Working with resources  Defining the AndroidManifest.xml
Page 35: Unlocking Android Chapter 4.  Understanding activities and views  Exploring the Activity lifecycle  Working with resources  Defining the AndroidManifest.xml

Understanding layout Layout is defined in terms of

◦ ViewGroup ◦ LayoutParams

ViewGroup ◦ is a container

is a View that contains other views (has children) Child View elements must fit into the layout specified by their

parents. a child View has to lay itself out based on its parents’ LayoutParams

a child View specify a different layout for its own children LayoutParams

◦ public static class- ViewGroup.LayoutParams◦ used by views to tell their parents how they want to be laid out◦ dimensions —width and height◦ relative or absolute placement◦ Margins

Page 36: Unlocking Android Chapter 4.  Understanding activities and views  Exploring the Activity lifecycle  Working with resources  Defining the AndroidManifest.xml
Page 37: Unlocking Android Chapter 4.  Understanding activities and views  Exploring the Activity lifecycle  Working with resources  Defining the AndroidManifest.xml

Grasping events

Page 38: Unlocking Android Chapter 4.  Understanding activities and views  Exploring the Activity lifecycle  Working with resources  Defining the AndroidManifest.xml

Allow other components to attach and listen for events. ◦ View class methods:

onFocusChanged(), onSizeChanged(), onLayout(), onTouchEvent()

Similarly, other items◦ Activity lifecycle methods

onCreate(), onFreeze()

View items are Observable

Page 39: Unlocking Android Chapter 4.  Understanding activities and views  Exploring the Activity lifecycle  Working with resources  Defining the AndroidManifest.xml

Using resources

Page 40: Unlocking Android Chapter 4.  Understanding activities and views  Exploring the Activity lifecycle  Working with resources  Defining the AndroidManifest.xml

Supported resource types

Page 41: Unlocking Android Chapter 4.  Understanding activities and views  Exploring the Activity lifecycle  Working with resources  Defining the AndroidManifest.xml

Referencing resources in Java

Page 42: Unlocking Android Chapter 4.  Understanding activities and views  Exploring the Activity lifecycle  Working with resources  Defining the AndroidManifest.xml

This maps to an XML layout file at src/res/layout/review_detail.xml

Page 43: Unlocking Android Chapter 4.  Understanding activities and views  Exploring the Activity lifecycle  Working with resources  Defining the AndroidManifest.xml
Page 44: Unlocking Android Chapter 4.  Understanding activities and views  Exploring the Activity lifecycle  Working with resources  Defining the AndroidManifest.xml

Defining views and layouts through XML resources Views and layout are defined in XML

◦ rather than in Java code◦ as resources

Benefits◦ easier to work with ◦ decoupled from the code◦ Reusable

Resource files ◦ are placed in the res/layout source directory◦ many XML files allowed ◦ the root of these XML files is

subclasses ofViewGroup RelativeLayout, LinearLayout, FrameLayout

◦ body of XML represent the view/layout tree.

Page 45: Unlocking Android Chapter 4.  Understanding activities and views  Exploring the Activity lifecycle  Working with resources  Defining the AndroidManifest.xml

•layout_[attribute]•Android: padding = setPadding()•android:id="@+id/[name]•android: layout_below="@id/[name]•style="@style/[stylename]

Page 46: Unlocking Android Chapter 4.  Understanding activities and views  Exploring the Activity lifecycle  Working with resources  Defining the AndroidManifest.xml

Externalizing values

Page 47: Unlocking Android Chapter 4.  Understanding activities and views  Exploring the Activity lifecycle  Working with resources  Defining the AndroidManifest.xml
Page 48: Unlocking Android Chapter 4.  Understanding activities and views  Exploring the Activity lifecycle  Working with resources  Defining the AndroidManifest.xml
Page 49: Unlocking Android Chapter 4.  Understanding activities and views  Exploring the Activity lifecycle  Working with resources  Defining the AndroidManifest.xml

Providing animations

Page 50: Unlocking Android Chapter 4.  Understanding activities and views  Exploring the Activity lifecycle  Working with resources  Defining the AndroidManifest.xml

Understanding the AndroidManifest file

Page 51: Unlocking Android Chapter 4.  Understanding activities and views  Exploring the Activity lifecycle  Working with resources  Defining the AndroidManifest.xml

Android requires a manifest file for every application

Is placed in the root directory of the project source

Describes ◦ application context◦ supported activities◦ services◦ intent receivers ◦ content providers ◦ permissions

AndroidManifest.xml

Page 52: Unlocking Android Chapter 4.  Understanding activities and views  Exploring the Activity lifecycle  Working with resources  Defining the AndroidManifest.xml

•To inform the system which implicit intents they can handle•Each filter describes a capability of the component, a set of intents that the component is willing to receive.

Page 53: Unlocking Android Chapter 4.  Understanding activities and views  Exploring the Activity lifecycle  Working with resources  Defining the AndroidManifest.xml