Introduction to Android App Development · Android App Development EDP 129/A By: Jose Cruz Tuesday,...

33
Introduction to Android App Development EDP 129/A By: Jose Cruz Tuesday, June 25, 13

Transcript of Introduction to Android App Development · Android App Development EDP 129/A By: Jose Cruz Tuesday,...

Page 1: Introduction to Android App Development · Android App Development EDP 129/A By: Jose Cruz Tuesday, June 25, 13. Course Structure • No book (resources + blog) • Teach concepts/write

Introduction toAndroid App Development

EDP 129/ABy: Jose Cruz

Tuesday, June 25, 13

Page 2: Introduction to Android App Development · Android App Development EDP 129/A By: Jose Cruz Tuesday, June 25, 13. Course Structure • No book (resources + blog) • Teach concepts/write

Course Structure

• No book (resources + blog)• Teach concepts/write code• Attendance• 5-minute break• Homework + Quiz• Take home final exam

Tuesday, June 25, 13

Page 3: Introduction to Android App Development · Android App Development EDP 129/A By: Jose Cruz Tuesday, June 25, 13. Course Structure • No book (resources + blog) • Teach concepts/write

Development Environment

• Java SE JDK

• Goto java.oracle.com• Click on Java SE (under Top Downloads)

• Click Java Download button• Download the JDK version for your OS

• Android ADT Bundle• developer.android.com

• Click on “Get the SDK”• Download SDK - ADT Bundle for [OS]

Tuesday, June 25, 13

Page 4: Introduction to Android App Development · Android App Development EDP 129/A By: Jose Cruz Tuesday, June 25, 13. Course Structure • No book (resources + blog) • Teach concepts/write

What is Android

• Operating system for “mobile” devices• Runs on phones, tablets, Google TV, Google Glass, etc• Created by Google• Free, Open-Source• #1 mobile operating system worldwide

• 75% Android• 17.2% iOS• 3.2% Windows Phone• 2.9% Blackberry• Based on IDC (May 2013)

Tuesday, June 25, 13

Page 5: Introduction to Android App Development · Android App Development EDP 129/A By: Jose Cruz Tuesday, June 25, 13. Course Structure • No book (resources + blog) • Teach concepts/write

Let’s start with Java & Programming

• Java is a Programming Language• Android Apps are written in Java• Before learning Android, we need Java• Learning Java + Learning to program

Tuesday, June 25, 13

Page 6: Introduction to Android App Development · Android App Development EDP 129/A By: Jose Cruz Tuesday, June 25, 13. Course Structure • No book (resources + blog) • Teach concepts/write

Java Principles

• Simple, object-oriented, familiar• Robust and Secure• Portable (compile once/run many

times)• High-performance• Interpreted, threaded, dynamic

Tuesday, June 25, 13

Page 7: Introduction to Android App Development · Android App Development EDP 129/A By: Jose Cruz Tuesday, June 25, 13. Course Structure • No book (resources + blog) • Teach concepts/write

Java Tools

• javac - compiler• java - run time to run apps• jar - a packager to package apps• javadoc - to create documentation

Tuesday, June 25, 13

Page 8: Introduction to Android App Development · Android App Development EDP 129/A By: Jose Cruz Tuesday, June 25, 13. Course Structure • No book (resources + blog) • Teach concepts/write

Hello World in Java

class HelloWorld {

public static void main(String[] args) { System.out.println("Hello, World"); }}

Tuesday, June 25, 13

Page 9: Introduction to Android App Development · Android App Development EDP 129/A By: Jose Cruz Tuesday, June 25, 13. Course Structure • No book (resources + blog) • Teach concepts/write

Programming Concepts

• Object• Class• Field, Method• Variable• Data Type• Array• Loops• Other syntax

Tuesday, June 25, 13

Page 10: Introduction to Android App Development · Android App Development EDP 129/A By: Jose Cruz Tuesday, June 25, 13. Course Structure • No book (resources + blog) • Teach concepts/write

Objects

• Entity which has state and behavior• Example: Dog

• state: name, color, breed, hungry• behavior: bark, fetch, wag tail

• Example: Smartphone• state: color, brand, cpu speed, ram• behavior: place call, send text, browse

Tuesday, June 25, 13

Page 11: Introduction to Android App Development · Android App Development EDP 129/A By: Jose Cruz Tuesday, June 25, 13. Course Structure • No book (resources + blog) • Teach concepts/write

Objects (Cont)

• Another way to say it:

• An object has properties• And object can perform some actions

• Example: Calculator• properties: color, brand, energy source

• actions: add, subtract, get square root• Example: Car

• properties: make, model, horse power• actions: turn on, accelerate, break

Tuesday, June 25, 13

Page 12: Introduction to Android App Development · Android App Development EDP 129/A By: Jose Cruz Tuesday, June 25, 13. Course Structure • No book (resources + blog) • Teach concepts/write

Objects - Software

• Object Oriented Programming• State/properties = fields • Actions = methods (aka functions)

Tuesday, June 25, 13

Page 13: Introduction to Android App Development · Android App Development EDP 129/A By: Jose Cruz Tuesday, June 25, 13. Course Structure • No book (resources + blog) • Teach concepts/write

Class

• A class is a definition from which an object is created

• A class is a blueprint from which an object is created

Tuesday, June 25, 13

Page 14: Introduction to Android App Development · Android App Development EDP 129/A By: Jose Cruz Tuesday, June 25, 13. Course Structure • No book (resources + blog) • Teach concepts/write

Anatomy of a Java Class

class Car {...

}

Tuesday, June 25, 13

Page 15: Introduction to Android App Development · Android App Development EDP 129/A By: Jose Cruz Tuesday, June 25, 13. Course Structure • No book (resources + blog) • Teach concepts/write

Anatomy of a Class (continued)

class Car {String make;String model; ...}

Tuesday, June 25, 13

Page 16: Introduction to Android App Development · Android App Development EDP 129/A By: Jose Cruz Tuesday, June 25, 13. Course Structure • No book (resources + blog) • Teach concepts/write

Anatomy of a Class (contintued)

class Car { String make; String model; String engineStatus; int currentSpeed; int topSpeed;

...}

Tuesday, June 25, 13

Page 17: Introduction to Android App Development · Android App Development EDP 129/A By: Jose Cruz Tuesday, June 25, 13. Course Structure • No book (resources + blog) • Teach concepts/write

Anatomy of a Class(continued)

class Car { String make; String model; String engineStatus; int currentSpeed; int topSpeed;

void turnOn() {...

}}

Tuesday, June 25, 13

Page 18: Introduction to Android App Development · Android App Development EDP 129/A By: Jose Cruz Tuesday, June 25, 13. Course Structure • No book (resources + blog) • Teach concepts/write

Anatomy of a Class(continued)

class Car { String make; String model; String engineStatus; int currentSpeed; int topSpeed;

void turnOn() { engineStatus = “on”; }}

Tuesday, June 25, 13

Page 19: Introduction to Android App Development · Android App Development EDP 129/A By: Jose Cruz Tuesday, June 25, 13. Course Structure • No book (resources + blog) • Teach concepts/write

Anatomy of a Class(continued)

class Car { String make; String model; String engineStatus; int currentSpeed; int topSpeed;

void turnOn() {engineStatus = “on”;

}

void turnOff() { engineStatus = “off”; }}

Tuesday, June 25, 13

Page 20: Introduction to Android App Development · Android App Development EDP 129/A By: Jose Cruz Tuesday, June 25, 13. Course Structure • No book (resources + blog) • Teach concepts/write

Class Example

class Multiplier { int firstNumber; int secondNumber; int result;

void multiply() { result = firstNumber * secondNumber; } void displayResult() { System.out.println(“Result is: ” + result); }}

Tuesday, June 25, 13

Page 21: Introduction to Android App Development · Android App Development EDP 129/A By: Jose Cruz Tuesday, June 25, 13. Course Structure • No book (resources + blog) • Teach concepts/write

The Entry Point

class Main { public static void main(String[] args) { }}

• Java apps have an entry point• Only one entry point per app

Tuesday, June 25, 13

Page 22: Introduction to Android App Development · Android App Development EDP 129/A By: Jose Cruz Tuesday, June 25, 13. Course Structure • No book (resources + blog) • Teach concepts/write

Creating the Objectclass Main { public static void main(String[] args) { // step 1: declare object Multiplier myObject; // step 2: create object myObject = new Multiplier(); // step 3: use the object to accomplish task myObject.firstNumber = 4; myObject.secondNumber = 18; myObject.multiply(); myObject.displayResult(); }}

Tuesday, June 25, 13

Page 23: Introduction to Android App Development · Android App Development EDP 129/A By: Jose Cruz Tuesday, June 25, 13. Course Structure • No book (resources + blog) • Teach concepts/write

Things to Note

• A program usually has at least two classes:• One class which has ability to solve problem• The ‘Main’ class which actually solves it

• Before an object is used, it must be declared and created

• Syntax to declare is: ClassName objectName;• Syntax to create is: objectName = new ClassName();• Object’s fields/method accessed with dot notation

• myObject.fieldOrMethod

Tuesday, June 25, 13

Page 24: Introduction to Android App Development · Android App Development EDP 129/A By: Jose Cruz Tuesday, June 25, 13. Course Structure • No book (resources + blog) • Teach concepts/write

Conventions

• Class names use Pascal casing• Car, SportsCar, Multiplier

• Class fields/methods use camel casing• firstNumber, topSpeed, myObject

• Comments delimit sections of code

Tuesday, June 25, 13

Page 25: Introduction to Android App Development · Android App Development EDP 129/A By: Jose Cruz Tuesday, June 25, 13. Course Structure • No book (resources + blog) • Teach concepts/write

Homework

• Set up your dev environment• Install Java SE JDK• Install ADT Bundle• Set up your workspace directory

• Create a program to calculate average of 3 numbers and display

Tuesday, June 25, 13

Page 26: Introduction to Android App Development · Android App Development EDP 129/A By: Jose Cruz Tuesday, June 25, 13. Course Structure • No book (resources + blog) • Teach concepts/write

Variables

• All programs work in the same way:• They get input from user• They do something with that input

• Examples:• Place online order• Place a call• Printer• Multiplier

Tuesday, June 25, 13

Page 27: Introduction to Android App Development · Android App Development EDP 129/A By: Jose Cruz Tuesday, June 25, 13. Course Structure • No book (resources + blog) • Teach concepts/write

Variables (Continued)

• A variable is a placeholder for data• Called “variable” because its value can

vary each time the program runs• The programmer uses a name for the

place holder• All class fields are variables, but not all

variables are fields

Tuesday, June 25, 13

Page 28: Introduction to Android App Development · Android App Development EDP 129/A By: Jose Cruz Tuesday, June 25, 13. Course Structure • No book (resources + blog) • Teach concepts/write

Data Types (Primitive)

Data Type Bits Min Value Max Value

byte 8 -128 127

short 16 -32,768 32,767

int 32 -2,147,483,648 2,147,483,647

long 64 -9.22E+18 9.22E+18

float 32 decimals decimals

double 64 decimals decimals

char 16 single character single character

boolean - true/false true/false

Tuesday, June 25, 13

Page 29: Introduction to Android App Development · Android App Development EDP 129/A By: Jose Cruz Tuesday, June 25, 13. Course Structure • No book (resources + blog) • Teach concepts/write

Naming Rules

• Names can have letters and digits• Also _ character• Cannot begin with a number• Case-sensitive

byte firstNumber = 10; // Okshort 2ndNum = 20; // NOT OKint num2 = 30520; // Okdouble third_num = 3.14d; // Ok

Tuesday, June 25, 13

Page 30: Introduction to Android App Development · Android App Development EDP 129/A By: Jose Cruz Tuesday, June 25, 13. Course Structure • No book (resources + blog) • Teach concepts/write

Examples

• Variables must be declared• Variable declaration examples:

byte firstNumber = 10;short secNum = 20;int thirdNum = 30520;

Tuesday, June 25, 13

Page 31: Introduction to Android App Development · Android App Development EDP 129/A By: Jose Cruz Tuesday, June 25, 13. Course Structure • No book (resources + blog) • Teach concepts/write

More Examples

• Long, float, double require letter:

long forthNum = 50L;float fifthNum = 3.14f;double sixthNum = 10.876d;

• Letter can be upper or lower case• By convention, L is upper case, while d

and f are lowercase

Tuesday, June 25, 13

Page 32: Introduction to Android App Development · Android App Development EDP 129/A By: Jose Cruz Tuesday, June 25, 13. Course Structure • No book (resources + blog) • Teach concepts/write

More Examples

• Long, float, double require letter:

long forthNum = 50L;float fifthNum = 3.14f;double sixthNum = 10.876d;

• Letter can be upper or lower case• By convention, L is upper case, while d

and f are lowercase

Tuesday, June 25, 13

Page 33: Introduction to Android App Development · Android App Development EDP 129/A By: Jose Cruz Tuesday, June 25, 13. Course Structure • No book (resources + blog) • Teach concepts/write

Data Types (complex)

• Complex Data types hold more than a single value• String• Date• Objects

Tuesday, June 25, 13