36

Android Introduction

Embed Size (px)

DESCRIPTION

A brief overview of the basic concepts involved in developing an Android application

Citation preview

Page 1: Android Introduction

Android Programming

Daniela da Cruz

Instituto Politécnico do Cávado e do Ave

III Jornadas de Tecnologia

May 6, 2013

1 of 14

Page 2: Android Introduction

The Story

Advantages of using Android

Disadvantages of using Android

What will be done along this workshop?

How to start programming?

2 of 14

Page 3: Android Introduction

The Story

- Android is a is a Linux-based operating system for smartphones andtablets created by Google.- Developers write applications in a customized version of Java, andapps can be downloaded from online stores such as Google Play(formerly Android Market), the app store run by Google, or third-partysites.- In June 2012, there were more than 600 000 apps available forAndroid, and the estimated number of applications downloaded fromGoogle Play was 20 billion (according tohttp://www.engadget.com).

3 of 14

Page 4: Android Introduction

The Story

4 of 14

Page 5: Android Introduction

The Story

5 of 14

Page 6: Android Introduction

The Story

6 of 14

Page 7: Android Introduction

The Story

7 of 14

Page 8: Android Introduction

The Story

8 of 14

Page 9: Android Introduction

The Story

9 of 14

Page 10: Android Introduction

Advantages of using Android

• The Android OS is simple to learn, and Google provides manylibraries to make it easy to implement rich and complex applications

• Multitasking: Android phones can run many applications, it meansyou can browse Facebook while listening a song.

• Easy access to thousands of applications via the Google AndroidApp Market.

• Phone options are diverse: di�erent from iOS that is limited toiPhone from Apple, Android is available on mobile phones fromvarious manufacturers (Sony Ericsson, Motorola, HTC toSamsung).

10 of 14

Page 11: Android Introduction

Disadvantages of using Android

• The only aspect lacking, as mentioned by many in the Androiddeveloper community, is clear and well-explained documentation.

• Advertising: application in the Android phones can indeed beobtained easily and for free, but the consequences in each of theseapplications, will always be ads on display, either the top or bottomof the application.

11 of 14

Page 12: Android Introduction

What will be done in this workshop?

• The classical Hello World!

• Take a pic and show it in our app

• Show a Google Map and change its center using our location

12 of 14

Page 13: Android Introduction

How to start programming?

• Download and install the Android SDK (4.2 version - Jelly Bean)� it provides the API libraries and developer tools necessary tobuild, test, and debug apps for Android.

Details on http://developer.android.com/sdk/index.html

Pre-requisites:

• Eclipse 3.6.2 (Helios) or greater

• Eclipse JDT plugin (included in most Eclipse IDE packages)

• JDK 6 (JRE alone is not su�cient)

• Android Development Tools plugin (recommended)

13 of 14

Page 14: Android Introduction

Android Application Overview

Activity Lifecycle

Basic Android User Interface componentsActivityFragmentsView and ViewGroup

LayoutsAbsoluteLayoutFrameLayoutLinearLayoutRelativeLayoutTableLayout

XML Layout Attributes

Dimensions

2 of 20

Page 15: Android Introduction

Android Application Overview

An Android application consists of various functionalities. Someexamples are editing a note, playing a music file, ringing an alarm, oropening a phone contact.These functionalities can be classified intofour different Android components:

Every application is made up of one or more of these components.

3 of 20

Page 16: Android Introduction

Activity Lifecycle

4 of 20

Page 17: Android Introduction

Activity Lifecycle

Note the following:

• Changing the screen orientation destroys and recreates the activityfrom scratch.

• Pressing the Home button pauses the activity, but does not destroyit.

• Pressing the Application icon might start a new instance of theactivity, even if the old one was not destroyed.

• Letting the screen sleep pauses the activity and the screenawakening resumes it. (This is similar to taking an incoming phonecall.)

5 of 20

Page 18: Android Introduction

Activity

An Activity represents the visual representation of an Androidapplication.

Activities use Views and Fragments to create the user interface andto interact with the user.

An Android application can have several Activities.

6 of 20

Page 19: Android Introduction

Fragments

Fragments are components which run in the context of an Activity.

Fragment components encapsulate application code so that it is easierto reuse it and to support different sized devices.

Fragments are optional, you can use Views and ViewGroups directly inan Activity but in professional applications you always use them toallow the reuse of your user interface components on different sizeddevices.

7 of 20

Page 20: Android Introduction

View and ViewGroup

Views are user interface widgets, e.g. buttons or text fields. The baseclass for all Views is the android.view.View class. Views have attributeswhich can be used to configure their appearance and behavior.

A ViewGroup is responsible for arranging other Views. ViewGroups isalso called layout managers. The base class for these layout managersis the android.view.ViewGroup class which extends the View class.

ViewGroups can be nestled to create complex layouts. You should notnestle ViewGroups too deeply as this has a negative impact on theperformance.

8 of 20

Page 21: Android Introduction

View and ViewGroup

The user interface for each component of your app is defined using ahierarchy of View and ViewGroup objects.

The easiest and most effective way to define a layout is with an XMLfile.

9 of 20

Page 22: Android Introduction

Layouts

An Android layout is a class that handles arranging the way itschildren appear on the screen. Anything that is a View (or inheritsfrom View) can be a child of a layout. All of the layouts inherit fromViewGroup (which inherits from View) so you can nest layouts.The standard Layouts are:• AbsoluteLayout• FrameLayout• LinearLayout• RelativeLayout• TableLayout

10 of 20

Page 23: Android Introduction

XML Layout Attributes

At compile time, references to the resources are gathered into anauto-generated wrapper class called R.java. The Android AssetPackaging Tool (aapt) autogenerates this file.

The syntax for an ID, inside an XML tag is:

android:id="@+id/my_button"

The at-symbol (@) at the beginning of the string indicates that theXML parser should parse and expand the rest of the ID string andidentify it as an ID resource. The plus-symbol (+) means that this is anew resource name that must be created and added to the R.java file.

16 of 20

Page 24: Android Introduction

XML Layout Attributes

When referencing an Android resource ID, you do not need theplus-symbol, but must add the android package namespace, like so:

android:id="@android:id/empty"

With the android package namespace in place, we’re now referencingan ID from the android.R resources class, rather than the localresources class.

17 of 20

Page 25: Android Introduction

XML Layout Attributes

In order to create views and reference them from the application, acommon pattern is to:

1. Define a view/widget in the layout file and assign it aunique ID:<Button android:id="@+id/my_button"

android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/my_button_text"/>

18 of 20

Page 26: Android Introduction

XML Layout Attributes

In order to create views and reference them from the application, acommon pattern is to:

2. Then create an instance of the view object and captureit from the layout (typically in the onCreate() method):Button myButton = (Button) findViewById(R.id.my_button);

Defining IDs for view objects is important when creating aRelativeLayout. In a relative layout, sibling views can define theirlayout relative to another sibling view, which is referenced by theunique ID.

19 of 20

Page 27: Android Introduction

Dimensions

A dimension is specified with a number followed by a unit of measure.The following units of measure are supported by Android:• dp — Density-independent Pixels: An abstract unit that is based onthe physical density of the screen. These units are relative to a 160dpi (dots per inch) screen, on which 1dp is roughly equal to 1px.When running on a higher density screen, the number of pixels usedto draw 1dp is scaled up by a factor appropriate for the screen’s dpi.

• sp — Scale-independent Pixels: This is like the dp unit, but it isalso scaled by the user’s font size preference.

• pt — Points: 1/72 of an inch based on the physical size of thescreen.

20 of 20

Page 28: Android Introduction

Dimensions

A dimension is specified with a number followed by a unit of measure.The following units of measure are supported by Android:• px — Pixels: Corresponds to actual pixels on the screen. This unitof measure is not recommended because the actual representationcan vary across devices.

• mm — Millimeters: Based on the physical size of the screen.• in — Inches: Based on the physical size of the screen.

21 of 20

Page 29: Android Introduction

IntentsExplicit IntentsImplicit IntentsUsing Intents to call ActivitiesCalling Sub-Activities for result data

2 of 8

Page 30: Android Introduction

Intents

Intents are asynchronous messages which allow Android componentsto request functionality from other components of the Android system.For example an Activity can send an Intents to the Android systemwhich starts another Activity.An Intent can also contain data. This data can be used by thereceiving component.

There are two types of Intents: Explit and Implict.

3 of 8

Page 31: Android Introduction

Explicit Intents

Explicit Intents explicitly defines the component which should becalled by the Android system, by using the Java class as identifier.The following shows an explicit Intent.

Explicit Intents are typically used within on application as the classesin an application are controlled by the application developer.

4 of 8

Page 32: Android Introduction

Implicit Intents

Implicit Intents do not directly specify the Android components whichshould be called.For example the following tells the Android system to view a webpage.

If these Intents are send to the Android system it searches for allcomponents which are registered for the specific action and the datatype.If only one component is found, Android starts this componentdirectly. If several components are identifier by the Android system,the user will get an selection dialog and can decide which componentshould be used for the Intent.

5 of 8

Page 33: Android Introduction

Retrieving data from Intents

The component which receives the Intent can use thegetIntent().getExtras() method call to get the extra data.

6 of 8

Page 34: Android Introduction

Using Intents to call Activities

If you send an Intent to the Android system, Android requires that youtell it to which type of component your Intent should be send.

To start an Activity use the method startActivity(Intent). Thismethod is defined on the Context object and available in everyActivity object.

If you call an Activity with the startActivity(Intent) method thecaller requires no result from the called Activity.

7 of 8

Page 35: Android Introduction

Calling Sub-Activities for result data

If you need some information from the called Activity use thestartActivityForResult() method.If you use the startActivityForResult() method then the startedActivity is called a Sub-Activity.

8 of 8

Page 36: Android Introduction

Bibliography

• Android - Introdução ao Desenvolvimento de Aplicações, RicardoQueirós (Abril 2013).

• Programming Android. Zigurd Mednieks, Laird Dornin, G. BlakeMeike, Masumi Nakamura. O'Reilly Media. July 2011

• The Android Developer's Cookbook: Building Applications with the

Android SDK. James Steele, Nelson To.

• http://www.learn-android.com

• http://www.vogela.com

14 of 14