0% found this document useful (0 votes)
63 views33 pages

Android Resources, Animation and Intents

The document discusses Android resources, animations, and intents. It covers how to define and access resources like layouts, drawables and strings. It describes how to create styles and tween animations to modify views. It also explains how to use intents to start activities, pass data between components, and define intent filters to respond to external intents.

Uploaded by

swonera
Copyright
© Attribution Non-Commercial (BY-NC)
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)
63 views33 pages

Android Resources, Animation and Intents

The document discusses Android resources, animations, and intents. It covers how to define and access resources like layouts, drawables and strings. It describes how to create styles and tween animations to modify views. It also explains how to use intents to start activities, pass data between components, and define intent filters to respond to external intents.

Uploaded by

swonera
Copyright
© Attribution Non-Commercial (BY-NC)
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/ 33

Android: Resources, Animation and Intents Tommy MacWilliam Resources Styles Animations Intent Filters

Android: Resources, Animation and Intents


Tommy MacWilliam
Harvard University

February 22, 2011

Announcements
Android: Resources, Animation and Intents Tommy MacWilliam Resources Styles Animations Intent Filters

Lecture videos available at: https://www.cs76.net/Lectures New section schedule: https://www.cs76.net/Sections


section every week here right after lecture ofce hours weeks projects are due Tuesday/Wednesday nights walkthroughs on Thursdays after project released seminars on interesting mobile dev topics on non-walkthrough weeks online labs

Today
Android: Resources, Animation and Intents Tommy MacWilliam Resources Styles Animations Intent Filters

Resources Styles Animation Intent Filters

Section Feedback
Android: Resources, Animation and Intents Tommy MacWilliam Resources Styles Animations Intent Filters

http://tommymacwilliam.com/e76/feedback
let me know how Im doing!

I dont like long surveys either, so give me feedback via an anonymous (I promise) 140-character tweet!

Resources
Android: Resources, Animation and Intents Tommy MacWilliam Resources Styles Animations Intent Filters

remember, resources are anything in our project that isnt code all resources will be placed in a special directory called res
this directory contains subfolders to organize your resources by type

Resources
Android: Resources, Animation and Intents Tommy MacWilliam Resources Styles Animations Intent Filters

anim: animations (which well take a look at later) color: colors used for different states of UI elements drawable: PNG, JPG, GIF, etc. layout: layouts for activities (which we saw last week) menu: app menus (i.e. Options, Context, etc.) raw: any le needed in its raw for (i.e. plaintext) xml: conguration XML les

Accessing Resources
Android: Resources, Animation and Intents Tommy MacWilliam Resources Styles Animations Intent Filters

resources can be accessed from XML and Java


XML: @<type>/<name> Java: R.<type>.<name>

<type>:

anim, drawable, etc.

<name>: lename (without the extension)

Resources
Android: Resources, Animation and Intents Tommy MacWilliam Resources Styles Animations Intent Filters

values: predened strings, integers, colors, etc.


lenames are arbitrary, can contain arbitrary XML elements each element must be a child of <resources> from Java: R.<element>.<name> from XML: @<element>/<name>

Alternative Resources
Android: Resources, Animation and Intents Tommy MacWilliam Resources Styles Animations Intent Filters

unlike the iPhone, there are many different types of Android devices
which all have different hardware specs, screen resolutions, etc.

all of the folders in the res directory can be device-specic


create a new folder called <name>-<qualifier 1>-<qualifier 2>-... can have any number of qualiers, which match devices that match all qualiers

Alternative Resources
Android: Resources, Animation and Intents Tommy MacWilliam Resources Styles Animations Intent Filters

resource qualiers
language: en, en-rUS, es, etc. (any valid ISO 639-1 code)
http://www.loc.gov/standards/iso639-2/php/code_list.php

screen size: small, normal, large, xlarge screen aspect ratio: long, notlong screen orientation: port, land

Alternative Resources
Android: Resources, Animation and Intents Tommy MacWilliam Resources Styles Animations Intent Filters

resource qualiers
pixel density: ldpi, mdpi, hdpi, xhdpi, nodpi text input: nokeys, qwerty, 12key navigation: nonav, dpad, trackball, wheel

Picking the Best Resource


Android: Resources, Animation and Intents Tommy MacWilliam Resources Styles Animations Intent Filters

must be specied in the order they were presented here 4-step process
eliminate any contradictions (device is en, app has cong for es) iterate through qualiers (all possible qualiers in the predened order) check if any folder matches the qualier if match, eliminate directories that do not include the qualier (until only one directory is left)

Alternative Resources
Android: Resources, Animation and Intents Tommy MacWilliam Resources Styles Animations Intent Filters

example time!

Styles
Android: Resources, Animation and Intents Tommy MacWilliam Resources Styles Animations Intent Filters

dene a custom look and feel for UI elements


the Android equivalent of CSS makes View XML much less verbose can apply the same style to multiple Views

stored in XML les located in res/values/

Styles
Android: Resources, Animation and Intents Tommy MacWilliam Resources Styles Animations Intent Filters

<style> denes multiple <item>s <item>s name attribute is equivalent to the View attribute
value equivalent to the value of the View attribute

<Something android:layout_width=fill_parent />


becomes: <item name=android:layout_width> fill_parent </item>

Styles
Android: Resources, Animation and Intents Tommy MacWilliam Resources Styles Animations Intent Filters

style can inherit from platform styles and themes via the parent attribute a style can also extend an already dened style
name=MyStyle.Something will automatically inherit from the style with name=MyStyle

Styles
Android: Resources, Animation and Intents Tommy MacWilliam Resources Styles Animations Intent Filters

example time!

Tween Animations
Android: Resources, Animation and Intents Tommy MacWilliam Resources Styles Animations Intent Filters

despite its name, unrelated to Justin Bieber equivalent to CSS3 transitions, can be used to animate any View object
admittedly a bit more verbose than CSS3, though

saved in res/anim <set> denes properties to be animated concurrently

Tween Animations
Android: Resources, Animation and Intents Tommy MacWilliam Resources Styles Animations Intent Filters

transitions
<alpha>: fade a Views opacity in/out <scale>: resize a View <translate>: vertical/horizontal motion <rotate>: rotate a View

all transitions must be children of a <set>, but a <set> can contain <set>s

Tween Animations
Android: Resources, Animation and Intents Tommy MacWilliam Resources Styles Animations Intent Filters

attributes in the form android:from<Something> and android:to<Something>


<alpha>: fromAlpha, toAlpha <scale>: from{X,Y}Scale, to{X,Y}Scale <translate>: from{X,Y}, to{X,Y}, from{X,Y}Delta, to{X,Y}Delta <rotate>: fromDegrees, toDegrees <all>: duration, startOffset

Applying Animations
Android: Resources, Animation and Intents Tommy MacWilliam Resources Styles Animations Intent Filters

rst, we load a new Animation via the AnimationUtils.loadAnimation


recall we can access the animation via R.anim.<filename>

call startAnimation on the View we want to animate AnimationListener provides callbacks to detect when an animation is complete

Tween Animations
Android: Resources, Animation and Intents Tommy MacWilliam Resources Styles Animations Intent Filters

example time!

Interpolators
Android: Resources, Animation and Intents Tommy MacWilliam Resources Styles Animations Intent Filters

just like in CSS3, the interpolator determines how the animation is applied
built-in interpolators given by @android:anim/<interpolator> accelerate_decelerate_interpolator, bounce_interpolator, linear_interpolator, overshoot_interpolator

custom interpolators can also be dened


<bounceInterpolator>, etc. are XML elements, attributes changeable to customize animation

Interpolators
Android: Resources, Animation and Intents Tommy MacWilliam Resources Styles Animations Intent Filters

example time!

Frame Animation
Android: Resources, Animation and Intents Tommy MacWilliam Resources Styles Animations Intent Filters

used to animate drawable elements animation consists of multiple <item>s, each a child of an <animation-list>
each <item> must have an android:drawable and android:duration remember, we can access drawable resources via @drawable/<name>

Intents
Android: Resources, Animation and Intents Tommy MacWilliam Resources Styles Animations Intent Filters

remember, Activities, Broadcast Receivers, and Services are triggered via Intents
last week: Intent i = new Intent(Context packageContext, Class<?> cls); startActivity(i);

Intent objects simply contain a description of what action is to be performed


has an action, data, and a category

we can use Intents to trigger (private) actions within our own app and (public) actions within other apps

Intents
Android: Resources, Animation and Intents Tommy MacWilliam Resources Styles Animations Intent Filters

Intent i = new Intent(<action>, <URI>); action: the action to be performed


ACTION_MAIN: show the initial Activity ACTION_DIAL: present a dialer for a phone call ACTION_EDIT: display editable data to the user ACTION_VIEW: view content (web page, etc.) ACTION_WEB_SEARCH: search the web for data

manage actions with setAction() and getAction()

Intents
Android: Resources, Animation and Intents Tommy MacWilliam Resources Styles Animations Intent Filters

<URI> contains data for the action (phone number to call, etc.) formatted as a URI
scheme://host/path/segments getScheme(), getHost(), getPathSegments()

category: additional information about the component that should respond


CATEGORY_BROWSABLE: component can be invoked by the browser to display content CATEGORY_LAUNCHER: component can be shown as the initial activity from the launcher

manage categories with addCategory() and removeCategory()

Bundles
Android: Resources, Animation and Intents Tommy MacWilliam Resources Styles Animations Intent Filters

we can pass additional data to Activities with bundles sending data: intent.putExtra(key, value); retrieving passed data: Bundle bundle = getIntent().getExtras(); bundle.getString(key);

Intent Filters
Android: Resources, Animation and Intents Tommy MacWilliam Resources Styles Animations Intent Filters

we can also allow other apps to open Activities within our app via Intent Filters we register these in AndroidManifest.xml
generally not created using Java also where we tell Android what Activities exist in our app

Intent Filters
Android: Resources, Animation and Intents Tommy MacWilliam Resources Styles Animations Intent Filters

<intent-filter> must be a child of an <activity> and can contain <action>, <data>, and <category> elements
<action>: action to be performed <data>: format through which data is passed to the activity
accessed via getIntent().getData()

<category>: component properties

Intent Filters
Android: Resources, Animation and Intents Tommy MacWilliam Resources Styles Animations Intent Filters

we can also use Intent Filters to open our apps Activities from within our app
the other syntax is kinda ugly

if we give our <action> a unique android:name attribute, we can just pass that to the Intent constructor

Intent Filters
Android: Resources, Animation and Intents Tommy MacWilliam Resources Styles Animations Intent Filters

example time!

You might also like