lecture 3 agenda layouts intents (both explicit and implicit) listviews and adapters

59
Lecture 3 agenda Layouts Intents (both explicit and implicit) ListViews and Adapters

Upload: job-marsh

Post on 11-Jan-2016

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 3 agenda Layouts Intents (both explicit and implicit) ListViews and Adapters

Lecture 3 agendaLayoutsIntents (both explicit and implicit)ListViews and Adapters

Page 2: Lecture 3 agenda Layouts Intents (both explicit and implicit) ListViews and Adapters

Layouts in Android

Page 3: Lecture 3 agenda Layouts Intents (both explicit and implicit) ListViews and Adapters

6/9/12

layout_margin (layout means in relation to parent)padding

Page 4: Lecture 3 agenda Layouts Intents (both explicit and implicit) ListViews and Adapters

6/9/12

Page 5: Lecture 3 agenda Layouts Intents (both explicit and implicit) ListViews and Adapters

6/9/12

SP scale independent pixels respects the users' settings for font size.

Page 6: Lecture 3 agenda Layouts Intents (both explicit and implicit) ListViews and Adapters

6/9/12

Page 7: Lecture 3 agenda Layouts Intents (both explicit and implicit) ListViews and Adapters

6/9/12

Page 8: Lecture 3 agenda Layouts Intents (both explicit and implicit) ListViews and Adapters

6/9/12

(layout means in relation to parent)

Page 9: Lecture 3 agenda Layouts Intents (both explicit and implicit) ListViews and Adapters

6/9/12

Page 10: Lecture 3 agenda Layouts Intents (both explicit and implicit) ListViews and Adapters

6/9/12

weight

Page 11: Lecture 3 agenda Layouts Intents (both explicit and implicit) ListViews and Adapters

6/9/12

FrameLayout

Page 12: Lecture 3 agenda Layouts Intents (both explicit and implicit) ListViews and Adapters

6/9/12

Page 13: Lecture 3 agenda Layouts Intents (both explicit and implicit) ListViews and Adapters

6/9/12

Page 14: Lecture 3 agenda Layouts Intents (both explicit and implicit) ListViews and Adapters

6/9/12

Page 15: Lecture 3 agenda Layouts Intents (both explicit and implicit) ListViews and Adapters

6/9/12

Page 16: Lecture 3 agenda Layouts Intents (both explicit and implicit) ListViews and Adapters

6/9/12

Page 17: Lecture 3 agenda Layouts Intents (both explicit and implicit) ListViews and Adapters

6/9/12

Page 18: Lecture 3 agenda Layouts Intents (both explicit and implicit) ListViews and Adapters

6/9/12

Page 19: Lecture 3 agenda Layouts Intents (both explicit and implicit) ListViews and Adapters

6/9/12

Page 20: Lecture 3 agenda Layouts Intents (both explicit and implicit) ListViews and Adapters

6/9/12

Fragments...what good are they?

Page 21: Lecture 3 agenda Layouts Intents (both explicit and implicit) ListViews and Adapters

Intents

Page 22: Lecture 3 agenda Layouts Intents (both explicit and implicit) ListViews and Adapters
Page 23: Lecture 3 agenda Layouts Intents (both explicit and implicit) ListViews and Adapters
Page 24: Lecture 3 agenda Layouts Intents (both explicit and implicit) ListViews and Adapters
Page 25: Lecture 3 agenda Layouts Intents (both explicit and implicit) ListViews and Adapters

From Google: An Intent object is a bundle of information. It contains information of interest to the component that receives the intent (such as the action to be taken and the data to act on) plus information of interest to the Android system (such as the category of component that should handle the intent and instructions on how to launch a target activity)

Page 26: Lecture 3 agenda Layouts Intents (both explicit and implicit) ListViews and Adapters

Activated by Intents

Activities

Services

Broadcast Receivers (aka Receivers)

(http://developer.android.com/guide/components/intents-filters.html)

Page 27: Lecture 3 agenda Layouts Intents (both explicit and implicit) ListViews and Adapters

Explicit Intent //Explicit; all you need is the Context and the target class

Intent itn = new Intent(this, ResultActivity.class);

ResultActivity.class

Explicit

Page 28: Lecture 3 agenda Layouts Intents (both explicit and implicit) ListViews and Adapters

Explicit Intent //Explicit; all you need is the packageContext and the target class Intent itn = new Intent(this, ResultActivity.class); itn.putExtra(QuizActivity.CORRECT, mCorrect); itn.putExtra(QuizActivity.INCORRECT, mIncorrect); itn.putExtra(QuizActivity.NAME, mName);

CORRECT: 5

INCORRECT: 3

PLAYER: Adam

ResultActivity.class

Explicit

Page 29: Lecture 3 agenda Layouts Intents (both explicit and implicit) ListViews and Adapters

Intent Architecture::Action/Data/etc//Explicit; all you need is the ComponentName and optionally the bundle.//Implicit; usually Action/Data and then optionally the bundle.

name : Adam Gerber

email : [email protected]

ret : something...

Action

Data

Scheme

Categories

ComponentNameExplicit

Implicit

Page 30: Lecture 3 agenda Layouts Intents (both explicit and implicit) ListViews and Adapters
Page 31: Lecture 3 agenda Layouts Intents (both explicit and implicit) ListViews and Adapters

Explicit Intents: intra-app call by name

//********************************//This method works great for intra-app calls between

activities//********************************

//use the calling class and the called class as params to constructor.

Intent itn = new Intent(this, Second.class);startActivity(itn);

//

Page 32: Lecture 3 agenda Layouts Intents (both explicit and implicit) ListViews and Adapters

Explicit Intents: intra-app call by name

Intent itnThird = new Intent(this, ThirdActivity.class);

itnThird.putExtra("name", "Adam Gerber");itnThird.putExtra("email", "[email protected]");

startActivity(itnThird);

Page 33: Lecture 3 agenda Layouts Intents (both explicit and implicit) ListViews and Adapters

Implicit Intents: inter-app call

Intent itn = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com/"));

startActivity(itn);

Implicit intents are anonymous and loosely-coupled.

You request the component like a service of the operating system.

Page 34: Lecture 3 agenda Layouts Intents (both explicit and implicit) ListViews and Adapters

Implicit Intents: Action/Data pairsACTION_VIEW content://contacts/people/1 -- Display information about the person whose identifier is "1".

ACTION_DIAL content://contacts/people/1 -- Display the phone dialer with the person filled in.

ACTION_VIEW tel:123 -- Display the phone dialer with the given number filled in. Note how the VIEW action does what what is considered the most reasonable thing for a particular URI.

ACTION_DIAL tel:123 -- Display the phone dialer with the given number filled in.

ACTION_EDIT content://contacts/people/1 -- Edit information about the person whose identifier is "1".

ACTION_VIEW content://contacts/people/ -- Display a list of people, which the user can browse through. This example is a typical top-level entry into the Contacts application, showing you the list of people.

Page 35: Lecture 3 agenda Layouts Intents (both explicit and implicit) ListViews and Adapters

Intent Architecture::Bundle (aka extras)//The bundle is a data structure inside the Intent.//It holds key/value pairs. //Keys are always Strings, and values are always primitives, Serializable or Parcelable.

name : Adam Gerber

email : [email protected]

ret : something...

Action

Data

Scheme

Categories

ComponentName

Page 36: Lecture 3 agenda Layouts Intents (both explicit and implicit) ListViews and Adapters

Serializable versus ParcelableParcelable is an Interface very much like Serializable

only it has been optimized for Android. Unlike Serializeable, all the reflection meta-data has been stripped-out of the parcelized object.

Page 37: Lecture 3 agenda Layouts Intents (both explicit and implicit) ListViews and Adapters

IntentsIntent messaging is a facility for late run-time binding

between components in the same or different applications.

Intents are handled by the Android OS. In order for the OS to know about a component, it must be declared in the application manifest file.

If a component does not define Intent filters, it can only be called by explicit Intents. A component with filters can receive both explicit and implicit intents.

6/12/12

Page 38: Lecture 3 agenda Layouts Intents (both explicit and implicit) ListViews and Adapters

AndroidManifest.xml fileIs an inventory of all the components in an

application.

If the component is not listed there, the Android OS won't know it's there. Not even intra-app component communication will resolve.

6/12/12

Page 39: Lecture 3 agenda Layouts Intents (both explicit and implicit) ListViews and Adapters

Android OSAndroid OS

A

*A

I

AndroidManifest.xml

A

*A

Page 40: Lecture 3 agenda Layouts Intents (both explicit and implicit) ListViews and Adapters

Categories

//Use Categories to further refine your Intent//Most important is CATEGORY_DEFAULT

<intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter>

Page 41: Lecture 3 agenda Layouts Intents (both explicit and implicit) ListViews and Adapters

ListViews and Adapters

Page 42: Lecture 3 agenda Layouts Intents (both explicit and implicit) ListViews and Adapters

Adapters

Page 43: Lecture 3 agenda Layouts Intents (both explicit and implicit) ListViews and Adapters

AdapterViews

Page 44: Lecture 3 agenda Layouts Intents (both explicit and implicit) ListViews and Adapters

Resources in Android

Page 45: Lecture 3 agenda Layouts Intents (both explicit and implicit) ListViews and Adapters
Page 46: Lecture 3 agenda Layouts Intents (both explicit and implicit) ListViews and Adapters

Layouts and resources

Code: Java (or C if you use NDK)Metafiles: AndroidManifest, project.properties, .gitignore. These all describe the project. Resources “anything in android that is not code or metafiles”

Activities have one or more layouts, and all Layouts have a root-ViewGroup. This root-ViewGroup is the container for the Views.

R.java (gen directory) is a bridge between your resources and your code. If you want to interact programmatically with a resource, it must have an id.

Page 47: Lecture 3 agenda Layouts Intents (both explicit and implicit) ListViews and Adapters
Page 48: Lecture 3 agenda Layouts Intents (both explicit and implicit) ListViews and Adapters

Inspecting layouts and resources

You can view both xml and design mode in AS.

You can see how it would look on multiple devices || preview all screens, and toggle with remove previews.

Page 49: Lecture 3 agenda Layouts Intents (both explicit and implicit) ListViews and Adapters

Layouts and resources

res directories can have suffixes, such as layout-land, or drawable-hdpi, values-es, etc.

These suffixes allow you to differentiate at RUNTIME depending on the settings, hardware, and configuration of the device.

For example, if your device is in landscape mode, it'll try to fetch the layout from layout-land first, then it will try to fetch the layout from layout. Vice versa as well; if it's in portait mode, it'll try for layout-port, then layout.

shown in preview mode of resource and rotate device

Page 50: Lecture 3 agenda Layouts Intents (both explicit and implicit) ListViews and Adapters

Lifecycle in Android

Page 51: Lecture 3 agenda Layouts Intents (both explicit and implicit) ListViews and Adapters
Page 52: Lecture 3 agenda Layouts Intents (both explicit and implicit) ListViews and Adapters
Page 53: Lecture 3 agenda Layouts Intents (both explicit and implicit) ListViews and Adapters
Page 54: Lecture 3 agenda Layouts Intents (both explicit and implicit) ListViews and Adapters

how to copy files from a bc branch to a lb branch

Actions || Open in Terminal

git checkout c7c7 res/drawable-*/.

git checkout c7c7 res/values/str*

before a commit you may remove from stage, then:discard: for existing files that you want to roll backremove: for new files that you want to delete

Page 55: Lecture 3 agenda Layouts Intents (both explicit and implicit) ListViews and Adapters

ADB (Android Device Bridge) adb kill-server adb start-server adb get-state adb devices

Use kill-server / start-server to reboot the adb if your device/emulator is not communicating with your dev-machine.

Page 56: Lecture 3 agenda Layouts Intents (both explicit and implicit) ListViews and Adapters

Navigating in Android Studioalt-1 is project view (alt-F1 is show it in project view)alt-2 is favorites (including bookmarks and breakpoints)alt-3 is the search view (cntl-shift-F to find)alt-4 run-consolealt-5 is debug alt-6 is android view (ddms, logcat)alt-7 is structure view (see members and methods)alt-9 is changes(VCS) view

Look at the margin of your project

Page 57: Lecture 3 agenda Layouts Intents (both explicit and implicit) ListViews and Adapters

Get helpcntl-shift-A (find anything in Android studio)

Searching (on mac, replace cntrl with command) cntl-F (find something in the local file)cntl-shift-F (find something in the project)

Go to file in code (on mac, replace cntrl with command) cntl-N (go to files typically in src)cntl-shift-n (go to any file, including res)cntl-E (open recent files)

Go to file in project alt-F1

Page 58: Lecture 3 agenda Layouts Intents (both explicit and implicit) ListViews and Adapters

Go to definition cntl-B (go directly to the method definition)

Javadocscntl-Q (open the javadocs)

Live Templatescntl-J

adding your own Live Templates (cntl-shift-A “live template”)

Page 59: Lecture 3 agenda Layouts Intents (both explicit and implicit) ListViews and Adapters

DebuggingUsing the debugger (alt-5)See bookmarks and breakpoints (alt-2)F11 to toggle bookmark

Using logcat (alt-6)

Using lint and AS analyzer: Analyze || Inspect Code

///TODO this is my todo message