Lecture 2 agenda Setting-up projects properly Review git config Java event model Anatomy of and...

55
Lecture 2 agenda Setting-up projects properly Review git config Java event model Anatomy of and Android Application Activity Lifecycle State Changes; saving and restoring state Model-View-Controller in Android Views, View Groups, Layouts A/S Designer Tool layouts and resources

Transcript of Lecture 2 agenda Setting-up projects properly Review git config Java event model Anatomy of and...

Page 1: Lecture 2 agenda Setting-up projects properly Review git config Java event model Anatomy of and Android Application Activity Lifecycle State Changes; saving.

Lecture 2 agendaSetting-up projects properlyReview git configJava event modelAnatomy of and Android Application Activity LifecycleState Changes; saving and restoring stateModel-View-Controller in AndroidViews, View Groups, LayoutsA/S Designer Toollayouts and resources

Page 2: Lecture 2 agenda Setting-up projects properly Review git config Java event model Anatomy of and Android Application Activity Lifecycle State Changes; saving.

git config --global user.name "Your Name"git config --global user.email "[email protected]"

csgerber/proCapitalquiz3gcsgerber/labLifeCycle3g

Page 3: Lecture 2 agenda Setting-up projects properly Review git config Java event model Anatomy of and Android Application Activity Lifecycle State Changes; saving.

What's inside an APK

Page 4: Lecture 2 agenda Setting-up projects properly Review git config Java event model Anatomy of and Android Application Activity Lifecycle State Changes; saving.

Git ReviewBasic Git Commands

Page 5: Lecture 2 agenda Setting-up projects properly Review git config Java event model Anatomy of and Android Application Activity Lifecycle State Changes; saving.

add/reset/commit:

move files from working-dir to stage-dir(aka index)git add .git add res/.git add src/.git add res/values/strings.xml

move files from stage-dir(aka index) to working-dirgit reset HEAD .git reset head res/.git reset head src/.git reset head res/values/strings.xml

git commit -m “your commit message.”

Page 6: Lecture 2 agenda Setting-up projects properly Review git config Java event model Anatomy of and Android Application Activity Lifecycle State Changes; saving.

Reverting (does the exact opposite)

git revert 0da8 --no-editgit revert head --no-editgit revert head~3 --no-edit

To clear all local changes since last pushgit reset --hard origin/master

To delete a remote branchgit push origin :branchName

Page 7: Lecture 2 agenda Setting-up projects properly Review git config Java event model Anatomy of and Android Application Activity Lifecycle State Changes; saving.

Amending:

Every commit is associated with a sha-1 hash. That hash is derived from 1/ the file changes in that commit and 2/ the previous commit.

You can not change any commit unless that commit is at the head. Since no other commits depend on the head, you may safely change the head.

To change the head, usegit commit --amend -m “your message”git commit --amend --no-edit

Page 8: Lecture 2 agenda Setting-up projects properly Review git config Java event model Anatomy of and Android Application Activity Lifecycle State Changes; saving.

Branching: To list the branches in a project:git branchgit branch -r git branch --all To create a branch: git checkout -b branchName c39bgit checkout -b branchName

To delete a branch:git branch -D branchName

To checkout a branch:git checkout 7afegit checkout master

Page 9: Lecture 2 agenda Setting-up projects properly Review git config Java event model Anatomy of and Android Application Activity Lifecycle State Changes; saving.

Pushing to remotes:

To see the remotes:git remote -v show To push to a remote:git push origin mastergit push --all

To clear all local changes since last pushgit reset --hard origin/master

Page 10: Lecture 2 agenda Setting-up projects properly Review git config Java event model Anatomy of and Android Application Activity Lifecycle State Changes; saving.

Android Studio

Default keymap:http://android.cs.uchicago.edu/content/slides/keymap.pdf

File || settings || keymap

Page 11: Lecture 2 agenda Setting-up projects properly Review git config Java event model Anatomy of and Android Application Activity Lifecycle State Changes; saving.

The Event Model

Page 12: Lecture 2 agenda Setting-up projects properly Review git config Java event model Anatomy of and Android Application Activity Lifecycle State Changes; saving.

Event(onClick)

No Event-Listener listening

No Catcher

Event-Source(Button)

No Event Listener

Page 13: Lecture 2 agenda Setting-up projects properly Review git config Java event model Anatomy of and Android Application Activity Lifecycle State Changes; saving.

Event(onClick)

Event-Listener listening

Catcher ready to catch

Event-Source(Button)

OnClick-Listener

Any Object

Page 14: Lecture 2 agenda Setting-up projects properly Review git config Java event model Anatomy of and Android Application Activity Lifecycle State Changes; saving.

Wrong Event

Page 15: Lecture 2 agenda Setting-up projects properly Review git config Java event model Anatomy of and Android Application Activity Lifecycle State Changes; saving.

Event source not registered

Page 16: Lecture 2 agenda Setting-up projects properly Review git config Java event model Anatomy of and Android Application Activity Lifecycle State Changes; saving.

Event(onClick)

Event-Listener listening

Catcher ready to catch

OnClick-ListenerEvent-Source

(Button)

Any Object

OnMouse-Listener

Event(onMouse)

Page 17: Lecture 2 agenda Setting-up projects properly Review git config Java event model Anatomy of and Android Application Activity Lifecycle State Changes; saving.

Resources in Android

Page 18: Lecture 2 agenda Setting-up projects properly Review git config Java event model Anatomy of and Android Application Activity Lifecycle State Changes; saving.
Page 19: Lecture 2 agenda Setting-up projects properly Review git config Java event model Anatomy of and Android Application Activity Lifecycle State Changes; saving.

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 20: Lecture 2 agenda Setting-up projects properly Review git config Java event model Anatomy of and Android Application Activity Lifecycle State Changes; saving.
Page 21: Lecture 2 agenda Setting-up projects properly Review git config Java event model Anatomy of and Android Application Activity Lifecycle State Changes; saving.

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 22: Lecture 2 agenda Setting-up projects properly Review git config Java event model Anatomy of and Android Application Activity Lifecycle State Changes; saving.

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 23: Lecture 2 agenda Setting-up projects properly Review git config Java event model Anatomy of and Android Application Activity Lifecycle State Changes; saving.
Page 24: Lecture 2 agenda Setting-up projects properly Review git config Java event model Anatomy of and Android Application Activity Lifecycle State Changes; saving.

<string name="earth">Earth</string>

<string name="moon">Moon</string>

<string-array name="system"> <item>@string/earth</item> <item>@string/moon</item> </string-array>

strings.xml

Page 25: Lecture 2 agenda Setting-up projects properly Review git config Java event model Anatomy of and Android Application Activity Lifecycle State Changes; saving.

<color name="gray">#eaeaea</color> <color name="titlebackgroundcolor">#00abd7</color> <color name="titlecolor">#666666</color><color name="opaque_red">#f00</color><color name="translucent_red">#80ff0000</color>

colors.xml

#RGB#ARGB#RRGGBB#AARRGGBB

Page 26: Lecture 2 agenda Setting-up projects properly Review git config Java event model Anatomy of and Android Application Activity Lifecycle State Changes; saving.

  <dimen name="textview_height">25dp</dimen>    <dimen name="textview_width">150dp</dimen>    <dimen name="ball_radius">30dp</dimen>    <dimen name="font_size">16sp</dimen>

dimens.xml

Page 27: Lecture 2 agenda Setting-up projects properly Review git config Java event model Anatomy of and Android Application Activity Lifecycle State Changes; saving.

  <bool name="screen_small">true</bool>    <bool name="adjust_view_bounds">true</bool>

bools.xml

Page 28: Lecture 2 agenda Setting-up projects properly Review git config Java event model Anatomy of and Android Application Activity Lifecycle State Changes; saving.

dp stands for density independent pixels

It's relative to a 160 dpi screen. So 1dp is one pixel on a 160dpi screen.

Don't worry about calculating the dp's. Android does that for you.

Page 29: Lecture 2 agenda Setting-up projects properly Review git config Java event model Anatomy of and Android Application Activity Lifecycle State Changes; saving.

Add resources from the Add Resources from the graphical editor and also manually.

#FFFFFF white

getResources(). getStringArray(R.array.whatever)

Page 30: Lecture 2 agenda Setting-up projects properly Review git config Java event model Anatomy of and Android Application Activity Lifecycle State Changes; saving.

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 31: Lecture 2 agenda Setting-up projects properly Review git config Java event model Anatomy of and Android Application Activity Lifecycle State Changes; saving.

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 32: Lecture 2 agenda Setting-up projects properly Review git config Java event model Anatomy of and Android Application Activity Lifecycle State Changes; saving.

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 33: Lecture 2 agenda Setting-up projects properly Review git config Java event model Anatomy of and Android Application Activity Lifecycle State Changes; saving.

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

Page 34: Lecture 2 agenda Setting-up projects properly Review git config Java event model Anatomy of and Android Application Activity Lifecycle State Changes; saving.

Lifecycle in Android

Page 35: Lecture 2 agenda Setting-up projects properly Review git config Java event model Anatomy of and Android Application Activity Lifecycle State Changes; saving.
Page 36: Lecture 2 agenda Setting-up projects properly Review git config Java event model Anatomy of and Android Application Activity Lifecycle State Changes; saving.
Page 37: Lecture 2 agenda Setting-up projects properly Review git config Java event model Anatomy of and Android Application Activity Lifecycle State Changes; saving.
Page 38: Lecture 2 agenda Setting-up projects properly Review git config Java event model Anatomy of and Android Application Activity Lifecycle State Changes; saving.

proCapitalQuiz (in class)

Page 39: Lecture 2 agenda Setting-up projects properly Review git config Java event model Anatomy of and Android Application Activity Lifecycle State Changes; saving.

labLifeCycle (in class)

Page 40: Lecture 2 agenda Setting-up projects properly Review git config Java event model Anatomy of and Android Application Activity Lifecycle State Changes; saving.

Layouts in Android

Page 41: Lecture 2 agenda Setting-up projects properly Review git config Java event model Anatomy of and Android Application Activity Lifecycle State Changes; saving.

6/9/12

Page 42: Lecture 2 agenda Setting-up projects properly Review git config Java event model Anatomy of and Android Application Activity Lifecycle State Changes; saving.

6/9/12

Page 43: Lecture 2 agenda Setting-up projects properly Review git config Java event model Anatomy of and Android Application Activity Lifecycle State Changes; saving.

6/9/12

layout_margin (layout means in relation to parent)padding

Page 44: Lecture 2 agenda Setting-up projects properly Review git config Java event model Anatomy of and Android Application Activity Lifecycle State Changes; saving.

6/9/12

(layout means in relation to parent)

Page 45: Lecture 2 agenda Setting-up projects properly Review git config Java event model Anatomy of and Android Application Activity Lifecycle State Changes; saving.

6/9/12

Page 46: Lecture 2 agenda Setting-up projects properly Review git config Java event model Anatomy of and Android Application Activity Lifecycle State Changes; saving.

6/9/12

weight

Page 47: Lecture 2 agenda Setting-up projects properly Review git config Java event model Anatomy of and Android Application Activity Lifecycle State Changes; saving.

6/9/12

FrameLayout

Page 48: Lecture 2 agenda Setting-up projects properly Review git config Java event model Anatomy of and Android Application Activity Lifecycle State Changes; saving.

6/9/12

Page 49: Lecture 2 agenda Setting-up projects properly Review git config Java event model Anatomy of and Android Application Activity Lifecycle State Changes; saving.

6/9/12

Page 50: Lecture 2 agenda Setting-up projects properly Review git config Java event model Anatomy of and Android Application Activity Lifecycle State Changes; saving.

6/9/12

Page 51: Lecture 2 agenda Setting-up projects properly Review git config Java event model Anatomy of and Android Application Activity Lifecycle State Changes; saving.

6/9/12

Page 52: Lecture 2 agenda Setting-up projects properly Review git config Java event model Anatomy of and Android Application Activity Lifecycle State Changes; saving.

6/9/12

Page 53: Lecture 2 agenda Setting-up projects properly Review git config Java event model Anatomy of and Android Application Activity Lifecycle State Changes; saving.

6/9/12

Page 54: Lecture 2 agenda Setting-up projects properly Review git config Java event model Anatomy of and Android Application Activity Lifecycle State Changes; saving.

6/9/12

Page 55: Lecture 2 agenda Setting-up projects properly Review git config Java event model Anatomy of and Android Application Activity Lifecycle State Changes; saving.

6/9/12