30
Developing Applications for Android Muhammad Usman Chaudhry SZABIST CS4615 Lecture # 3

Developing Applications for Android - Lecture#3

Embed Size (px)

DESCRIPTION

3rd Lecture on Developing Applications for Android.

Citation preview

Page 1: Developing Applications for Android - Lecture#3

Developing Applications for Android

Muhammad Usman ChaudhrySZABIST CS4615

Lecture # 3

Page 2: Developing Applications for Android - Lecture#3

Today

● Setting up your development Environment○ Eclipse○ Android SDK○ Android Development Tools (ADT)

● Creating HelloWorld Application● File Structure

○ AndroidManifest.xml, src, assets, res, bin, gen, etc.● Activity Lifecycle

○ onCreate, onStart, onRestart, onResume, onPause, onStop, onDestroy

● A program to study Lifecycle behavior.

Muhammad Usman Chaudhry CS4615 SZABIST

Page 3: Developing Applications for Android - Lecture#3

Today

Muhammad Usman Chaudhry CS4615 SZABIST

● Android Virtual Devices (AVD)○ Creating & Launching Virtual Devices/Emulator

● Running applications on Real Device● Debugging

○ DDMS ○ adb○ Normal debugging○ Using LogCat

● Basics About○ View, Fragment, Intent, Content Provider, Service

● Learning about VCS, DVCS, Git & Hg.● Using Hg/Bitbucket as version control & how-to, clone,

pull, commit and push code changes.

Page 4: Developing Applications for Android - Lecture#3

Setting Up IDE

Muhammad Usman Chaudhry CS4615 SZABIST

● Download Eclipse (http://eclipse.org/downloads - Eclipse Classic 4.2)

● Download & Installing Android SDK (http://developer.android.com/sdk)

● Configuring Platform APIs (Via SDK Manager)● Installing ADT Plugin (https://dl-ssl.google.

com/android/eclipse)

Page 5: Developing Applications for Android - Lecture#3

Hello World Android

● Let's create and discuss HelloWorld Application.

Muhammad Usman Chaudhry CS4615 SZABIST

Page 6: Developing Applications for Android - Lecture#3

File Structure

● AndroidManifest.xml:○ This file manages, Icons, Labels,

Permissions, Libraries, Intent Filters and many other configuration parameters related to application.

○ Let's have a look and discuss basics of Manifest file in Eclipse.

● src:○ Contains all the source packages, activity

and non-activity Java class files.

Muhammad Usman Chaudhry CS4615 SZABIST

Page 7: Developing Applications for Android - Lecture#3

File Structure● assets:

○ Used to store raw data.○ Example would be textures, game data,

static html/xml/json files etc.○ Can access via AssetManager

● libs: Contain private libraries.● bin: Contain compiled resources. (APK etc.)● gen: Generated Java files. (R.java etc.)● jni: Native code sources, developed using NDK.

Muhammad Usman Chaudhry CS4615 SZABIST

Page 8: Developing Applications for Android - Lecture#3

File Structure

● res: ○ Contain all application resources○ Include most of the following directories:

■ anim - for xml files that are compiled into Animation objects.

■ color - for xml files that describe colors.■ drawable - for images, drawable

resource types like BMP,JPG, 9-patch etc. Let's discuss more about drawable over Eclipse.

Muhammad Usman Chaudhry CS4615 SZABIST

Page 9: Developing Applications for Android - Lecture#3

File Structure

Muhammad Usman Chaudhry CS4615 SZABIST

■ layout - XML files compiled into screen objects/views.

■ menu - XML files defining application menus.

■ raw - Same as assets, except it can be accessed via R file.

■ values - XML files containing string values. Let's see how its used.

■ xml - Other xml files, eg. PreferenceScreen, etc.

Page 10: Developing Applications for Android - Lecture#3

Android Activity Lifecycle

Muhammad Usman Chaudhry CS4615 SZABIST

● Activity instances in our apps transition between various states whenever a user interacts with the application.

● Mostly these events are called in following order:○ onCreate(Bundle savedInstanceState)○ onStart()○ onResume()○ onPause()○ onRestart()○ onStop()○ onDestroy()

● Let's have a look at code in Eclipse and view the diagram in next slide.

Page 11: Developing Applications for Android - Lecture#3

Android Activity Lifecycle

Muhammad Usman Chaudhry CS4615 SZABIST

Page 12: Developing Applications for Android - Lecture#3

Activity Lifecycle

● Let's implement the activity lifecycle on Eclipse.

Muhammad Usman Chaudhry CS4615 SZABIST

Page 13: Developing Applications for Android - Lecture#3

Creating and Managing AVDs

Muhammad Usman Chaudhry CS4615 SZABIST

● Let's create AVDs and run our application on Virtual Device.

Page 14: Developing Applications for Android - Lecture#3

Running on Real Device

Muhammad Usman Chaudhry CS4615 SZABIST

● Running our application● Enabling debugging support

Page 15: Developing Applications for Android - Lecture#3

Debugging

Muhammad Usman Chaudhry CS4615 SZABIST

● Try-It-On-Eclipse:○ Normal Debugging○ Logcat○ DDMS○ adb

Page 16: Developing Applications for Android - Lecture#3

Let's Discuss About

● View● Fragment● Intent● Content Provider● Service

Muhammad Usman Chaudhry CS4615 SZABIST

Page 17: Developing Applications for Android - Lecture#3

View● View represents the basic building block for UI components.● All the controls within android are direct or indirect

subclasses of View. ● We may extend View or even its subclasses to create

custom controls.● Examples are:

○ Direct Subclasses:■ ImageView■ TextView■ And many other...

○ Indirect Subclasses:■ Button■ ListView■ And many more...

Muhammad Usman Chaudhry CS4615 SZABIST

Page 18: Developing Applications for Android - Lecture#3

Fragment

● A fragment represents a behavior or a portion of user interface in an activity.

● You may combine multiple fragments in a single activity to build a multi-pane UI.

● Fragment have lifecycle of its own, but we'll study it later.

● Fragments are more targeted towards tablet applications.

● Let's have a look at its design philosophy.

Muhammad Usman Chaudhry CS4615 SZABIST

Page 19: Developing Applications for Android - Lecture#3

Fragment

Muhammad Usman Chaudhry CS4615 SZABIST

Page 20: Developing Applications for Android - Lecture#3

Intent

● More like an 'intention' to do some work.● Intents are used to:

○ Broadcast a message○ Start a service○ Launch an activity○ Display a web page○ Dial a phone number

● Intents can be explicit or implicit.● Let's have a look at intent.

Muhammad Usman Chaudhry CS4615 SZABIST

Page 21: Developing Applications for Android - Lecture#3

Content Provider

● Standard mechanism to share data among applications.

● It doesn't expose underlying storage, structure and implementation.

● Your application can expose data to other applications or even read the data shared by other applications.

Muhammad Usman Chaudhry CS4615 SZABIST

Page 22: Developing Applications for Android - Lecture#3

Service

● Background processes that runs for long time.

● Android has 2 type of services:○ Local Services:

■ Only used by the application that is hosting it.○ Remote Services:

■ Accessible by other applications as well.● Local Vs Remote

Muhammad Usman Chaudhry CS4615 SZABIST

Page 23: Developing Applications for Android - Lecture#3

What's Version Control & Why We Need It?

● Version control is the management of changes to the source files.

● It's used to keep track of who did what and when in the code.

● In case of problem in latest code, you can easily revert back to any past version.

● Easier to collaborate and track the code changes among programmers working on the same project and files.

Muhammad Usman Chaudhry CS4615 SZABIST

Page 24: Developing Applications for Android - Lecture#3

Standard Version Control System

● Usually a central server.● A programmer may check-out a file, work

over it and checks-in back. ● When a programmer has checked the file

out, no other programmers can make changes to that particular file.

● Famous framework is SVN, CVS etc.

Muhammad Usman Chaudhry CS4615 SZABIST

Page 25: Developing Applications for Android - Lecture#3

DVCSDistributed Version Control System

● Instead of centralized system it uses P2P.● Avoids single point of failure.● Allow users to check-in and merge the code

anytime they want to. ● Famous frameworks are Hg(Mercurial) and

Git.● Let's watch this video to get more detailed

idea:○ http://www.fogcreek.com/kiln/

Muhammad Usman Chaudhry CS4615 SZABIST

Page 26: Developing Applications for Android - Lecture#3

Mercurial & Git

● Mercurial (Hg) & Git are 2 leading frameworks used for Revision Control.

● Basic difference is, ○ git provide set of tools like git-pull, git-push, etc.○ Hg is monolithic and all in one.

● Both have almost identical features.● We prefer Mercurial, due to easier learning

curve.

Muhammad Usman Chaudhry CS4615 SZABIST

Page 27: Developing Applications for Android - Lecture#3

Using Hg & BB

● Installing Hg● Let's create a repository at Bitbucket (BB) Source

code hosting site.● Create a local repository. (hg init)● Add Files. (hg add .)● Commit changes (hg commit -m "first")● Push our changes to BB. (hg push REPO-URL)● Remember repeat cycle

○ Pull - Update - Merge/Resolve - Commit - Push● Adding users to BB & using (hg clone to get current

copy).● We shall look into branching etc. later.

Muhammad Usman Chaudhry CS4615 SZABIST

Page 28: Developing Applications for Android - Lecture#3

Lab Tasks Today

● Setup Android Development Environment● Create HelloWorld Application● Create AVDs● Run your application in at least 2 different

AVDs● Create Lifecycle Activity Application● Log all lifecycle events

Muhammad Usman Chaudhry CS4615 SZABIST

Page 29: Developing Applications for Android - Lecture#3

Lab Tasks Today

● Setup Hg on System, create Bitbucket account.● Create a group of 2, invite your partner so he/she

could clone the code, make changes and pass through the following Steps:○ init-add-commit-push (For you)○ clone (For other group partner)○ //Make certain changes○ pull-update-commit-push (For other group

partner)○ //Make certain changes○ pull-update-commit-push (For you)

Muhammad Usman Chaudhry CS4615 SZABIST

Page 30: Developing Applications for Android - Lecture#3

Coming Up Next

● Detail in Design!● We'll learn to use all the controls.

Muhammad Usman Chaudhry CS4615 SZABIST