UI Concepts

53
UI Concepts Unlocking Android Chapter 4

description

UI Concepts. Unlocking Android Chapter 4. Goal. Understanding activities and views Exploring the Activity lifecycle Working with resources Defining the AndroidManifest.xml. Understanding activities. Views and other Android components compiled into a binary form R.java - PowerPoint PPT Presentation

Transcript of UI Concepts

Page 1: UI Concepts

UI ConceptsUnlocking Android

Chapter 4

Page 2: UI Concepts

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

Goal

Page 3: UI Concepts

Understanding activities

Page 4: UI Concepts

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: UI Concepts

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: UI Concepts
Page 7: UI Concepts
Page 8: UI Concepts

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: UI Concepts

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: UI Concepts

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: UI Concepts

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: UI Concepts

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: UI Concepts

“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: UI Concepts

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: UI Concepts

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: UI Concepts

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: UI Concepts
Page 18: UI Concepts

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: UI Concepts

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

Page 20: UI Concepts

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: UI Concepts

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: UI Concepts

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

Page 23: UI Concepts

•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: UI Concepts

Exploring the Activity lifecycle

Page 25: UI Concepts

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: UI Concepts

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: UI Concepts
Page 28: UI Concepts
Page 29: UI Concepts
Page 30: UI Concepts
Page 31: UI Concepts

Working with Views

Page 32: UI Concepts

View- building blocks of the UI

Related to layout

Page 33: UI Concepts
Page 34: UI Concepts
Page 35: UI Concepts

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: UI Concepts
Page 37: UI Concepts

Grasping events

Page 38: UI Concepts

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: UI Concepts

Using resources

Page 40: UI Concepts

Supported resource types

Page 41: UI Concepts

Referencing resources in Java

Page 42: UI Concepts

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

Page 43: UI Concepts
Page 44: UI Concepts

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: UI Concepts

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

Page 46: UI Concepts

Externalizing values

Page 47: UI Concepts
Page 48: UI Concepts
Page 49: UI Concepts

Providing animations

Page 50: UI Concepts

Understanding the AndroidManifest file

Page 51: UI Concepts

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: UI Concepts

•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: UI Concepts