Gauthier Picard. MINES Saint-Étienne. October 10, 2017

Similar documents
Gauthier Picard. Ecole Nationale Supérieure des Mines

Gauthier Picard. Ecole Nationale Supérieure des Mines

Introduction To Android

Diving into Android. By Jeroen Tietema. Jeroen Tietema,

Android Application Development

Android App Development. Muhammad Sharjeel COMSATS Institute of Information Technology, Lahore

Android App Development. Mr. Michaud ICE Programs Georgia Institute of Technology

Android Basics. - Bhaumik Shukla Android Application STEALTH FLASH

EMBEDDED SYSTEMS PROGRAMMING Application Basics

Programming with Android: System Architecture. Dipartimento di Scienze dell Informazione Università di Bologna

Getting Started. Dr. Miguel A. Labrador Department of Computer Science & Engineering

Android HelloWorld - Example. Tushar B. Kute,

Android Development Tutorial. Yi Huang

Android. Operating System and Architecture. Android. Screens. Main features

Android for Ubiquitous Computing Researchers. Andrew Rice University of Cambridge 17-Sep-2011

Real-Time Embedded Systems

Android. Lesson 1. Introduction. Android Developer Fundamentals. Android Developer Fundamentals. to Android 1

UNIT:2 Introduction to Android

ANDROID USER INTERFACE

Embedded Systems Programming - PA8001

Understand applications and their components. activity service broadcast receiver content provider intent AndroidManifest.xml

INTRODUCTION TO ANDROID

Mobile Programming Lecture 1. Getting Started

Overview. What are layouts Creating and using layouts Common layouts and examples Layout parameters Types of views Event listeners

Android UI Development

Programming with Android: System Architecture. Luca Bedogni. Dipartimento di Scienze dell Informazione Università di Bologna

Lab 1: Getting Started With Android Programming

ACTIVITY, FRAGMENT, NAVIGATION. Roberto Beraldi

Android Beginners Workshop

BCA 6. Question Bank

Android Ecosystem and. Revised v4presenter. What s New

Programming with Android: System Architecture. Dipartimento di Scienze dell Informazione Università di Bologna

Android Basics. Android UI Architecture. Android UI 1

Applications. Marco Ronchetti Università degli Studi di Trento

UNDERSTANDING ACTIVITIES

8/30/15 MOBILE COMPUTING. CSE 40814/60814 Fall How many of you. have implemented a command-line user interface?

Fragments were added to the Android API in Honeycomb, API 11. The primary classes related to fragments are: android.app.fragment

Introduction to Android

Computer Science E-76 Building Mobile Applications

Android Exam AND-401 Android Application Development Version: 7.0 [ Total Questions: 129 ]

Group B: Assignment No 8. Title of Assignment: To verify the operating system name and version of Mobile devices.

ANDROID APPS DEVELOPMENT FOR MOBILE AND TABLET DEVICE (LEVEL I)

CS 4518 Mobile and Ubiquitous Computing Lecture 4: Data-Driven Views, Android Components & Android Activity Lifecycle Emmanuel Agu

Mobile Programming Lecture 5. Composite Views, Activities, Intents and Filters

ANDROID APPS DEVELOPMENT FOR MOBILE AND TABLET DEVICE (LEVEL I)

Mobile OS. Symbian. BlackBerry. ios. Window mobile. Android

Xin Pan. CSCI Fall

LECTURE NOTES OF APPLICATION ACTIVITIES

Android development. Outline. Android Studio. Setting up Android Studio. 1. Set up Android Studio. Tiberiu Vilcu. 2.

Mobile Computing. Introduction to Android

Android UI: Overview

Java & Android. Java Fundamentals. Madis Pink 2016 Tartu

Required Core Java for Android application development

Programming Concepts and Skills. Creating an Android Project

CS 528 Mobile and Ubiquitous Computing Lecture 3: Android UI, WebView, Android Activity Lifecycle Emmanuel Agu

CS 528 Mobile and Ubiquitous Computing Lecture 3b: Android Activity Lifecycle and Intents Emmanuel Agu

Mobile Programming Lecture 2. Layouts, Widgets, Toasts, and Event Handling

Security model. Marco Ronchetti Università degli Studi di Trento

MODULE 2: GETTING STARTED WITH ANDROID PROGRAMMING

Getting started: Installing IDE and SDK. Marco Ronchetti Università degli Studi di Trento

Introduction to Android

Mobile Application Development

Why Android? Why Android? Android Overview. Why Mobile App Development? 20-Nov-18

CPET 565 Mobile Computing Systems CPET/ITC 499 Mobile Computing. Lab & Demo 2 (Part 1-2) Hello-Goodbye App Tutorial

Agenda. Overview of Xamarin and Xamarin.Android Xamarin.Android fundamentals Creating a detail screen

Android Overview. Most of the material in this section comes from

Solving an Android Threading Problem

INDEX. CHAPTER Android Introduction

Android Programming (5 Days)

COLLEGE OF ENGINEERING, NASHIK-4

ORACLE UNIVERSITY AUTHORISED EDUCATION PARTNER (WDP)

Mobile Software Development for Android - I397

Android Software Development Kit (Part I)

Activities and Fragments

CS 4330/5390: Mobile Application Development Exam 1

UI Fragment.

CS 370 Android Basics D R. M I C H A E L J. R E A L E F A L L

Android Workshop: Model View Controller ( MVC):

Vienos veiklos būsena. Theory

LifeStreet Media Android Publisher SDK Integration Guide

Android Application Development 101. Jason Chen Google I/O 2008

Mobila applikationer och trådlösa nät, HI1033, HT2013

Mobila applikationer och trådlösa nät, HI1033, HT2012

CS 528 Mobile and Ubiquitous Computing Lecture 1b: Introduction to Android. Emmanuel Agu

Android. Mobile operating system developed by Google A complete stack. Based on the Linux kernel Open source under the Apache 2 license

CS 4518 Mobile and Ubiquitous Computing Lecture 2: Introduction to Android Programming. Emmanuel Agu

CS 4518 Mobile and Ubiquitous Computing Lecture 2: Introduction to Android. Emmanuel Agu

CS 403X Mobile and Ubiquitous Computing Lecture 3: Introduction to Android Programming Emmanuel Agu

Stanislav Rost CSAIL, MIT

IJRDTM Kailash ISBN No Vol.17 Issue

Android Development Tools = Eclipse + ADT + SDK

Introduction to Android development

Embedded Systems Programming - PA8001

Android Development Crash Course

CSCU9YH Development with Android

COSC 3P97 Mobile Computing

Android. Michael Greifeneder. Image source: Android homepage

Understanding Application

Upon completion of the second part of the lab the students will have:

MC Android Programming

Transcription:

Android Programming Gauthier Picard MINES Saint-Étienne October 10, 2017 This presentation is based on Jean-Paul Jamont s one (Université Pierre Mendès France, IUT de Valence) Android Programming Gauthier Picard 1 / 53

Agenda Android Overview Basics Android Projects Activities XML Layout Manifest Some Android-specific Classes References Android Programming Gauthier Picard 2 / 53

Agenda Android Overview Basics Android Projects Activities XML Layout Manifest Some Android-specific Classes References Android Programming Gauthier Picard 3 / 53

What is Android? An open source operating system A mobile-device-oriented operating system It consists in a set of software to interface hardware and applicative software An open source operating system source code is available huge developper community An operating system based on the Linux kernel A free development environment Programming with Java or C language Development kit (SDK Android) available at http://developer.android.com/sdk/index.html Android Programming Gauthier Picard 4 / 53

Android Version History A lot of versions... The different versions of Android are all named as desserts since version 1.5 and follow an alphabetical order: 100% 90% 80% 70% 60% 50% 40% 30% 20% 10% 0% Dec 2009 Feb 2010 Apr 2010 Jun 2010 Aug 2010 Oct 2010 Dec 2010 Feb 2011 Apr 2011 Jun 2011 Aug 2011 Oct 2011 Dec 2011 Feb 2012 Apr 2012 Jun 2012 Aug 2012 Oct 2012 Dec 2012 Feb 2013 Apr 2013 Jun 2013 Aug 2013 Oct 2013 Dec 2013 Feb 2014 Apr 2014 Jun 2014 Aug 2014 * no data Oct 2014* Dec 2014 Feb 2015 Apr 2015 * no data Jun 2015 Jul 2015* Aug 2015 Oct 2015 Dec 2015 Feb 2016 Apr 2016 Jun 2016 Aug 2016 * no data Oct 2016* Dec 2016 Feb 2017 Apr 2017 Jun 2017 100% 90% 80% 70% 60% 50% 40% 30% 20% 10% 0% Android version 1.1 1.5 1.6 2.0 2.0.1 2.1 2.2 2.3-2.3.2 2.3.3-2.3.7 3.0 3.1 3.2 4.0-4.0.2 4.0.3-4.0.4 4.1 4.2 4.3 4.4 Cupcake Donut Eclair Eclair Eclair Froyo Gingerbread Gingerbread Honeycomb Honeycomb Honeycomb Ice Cream Sandwich Ice Cream Sandwich Jelly Bean Jelly Bean Jelly Bean KitKat 5.0 Lollipop 5.1 Lollipop 6.0 Marshmallow 7.0-7.1 Nougat Android Programming Gauthier Picard 5 / 53

Android Platforms: More and more equipments Google TV! Phones and PDA Consoles Tablets Android Programming Gauthier Picard 6 / 53

Android and its competitors Open Handset Alliance Creation: 5th November 2007, initiated by Google Objectives: Develop open norms for mobile phone devices Members: 84 technology and mobile companies Competitors Apple with ios Research In Motion (RIM) with BlackBerry OS Microsoft with Windows Phone Samsung with Bada (even Samsung is more and more using Android) HP with Palm webos then webos (stopped in 2011) Nokia with Symbian OS (stopped in 2011, Nokia is using now Windows Phone)... Android Programming Gauthier Picard 7 / 53

Android and its competitors (cont.) Share of worldwide 2009 to 2017 smartphone sales to end users by operating system, according to Statistica Android Programming Gauthier Picard 8 / 53

The Challenges Android Functionnalities Applicative Framework Dalvik Virtual Machine Integrated Web Browser 2D and 3D Graphical Library SQLite Audio and Video Codecs WiFi, EDGE, 3G, Bluetooth... Camera, GPS, Accelerometer, Compass... Android Programming Gauthier Picard 9 / 53

The Challenges (cont.) Constraints Low frequency CPU Little available RAM Low write access on flash disk Application-specific lifecycle (energy-efficiency, limited resources) Low rate and intermittent networks Specific HMI design Small screens: HVGA (320x480) portrait and landscape Small fonts can be unreadable (DPI) Low resolution of tactil sensor (25 pixels) Android Programming Gauthier Picard 10 / 53

Downloading and Uploading Apps Google Play Store (former Android Market) Standard system to upload/download apps No filtering/verification of apps Navigation more difficult than AppMarket (search by category, keywords, price) Requires a certified terminal (camera, 3G, compass...) Authorization management before installation 2.7 million apps published in 2017 (57% free-of-charge) If you want to sell your apps: 25$ to register as a developer 85% of the app price to the developer, 15% to Google Revenue earned from the Google Play is paid to developers via Google Wallet Android Programming Gauthier Picard 11 / 53

Downloading and Uploading Apps (cont.) Other Platforms There exist other legal application distribution platforms (i.e. authorized by Google). They allow access to non certified devices AppsLib (Archos) : http://appslib.com AndroLib : http://www.androlib.com Market Samsung... Android Programming Gauthier Picard 12 / 53

Agenda Android Overview Basics Android Projects Activities XML Layout Manifest Some Android-specific Classes References Android Programming Gauthier Picard 13 / 53

Important Terms Activity Building block for user interfaces Equivalent to a window (Windows, Linux) or a dialog box An activity may not have any user interface (in case of services) Content provider Abstraction level for any data stored in the device Android encourages the provision of its own data to other programs the content provider enables this by proposing an access control mechanism Android Programming Gauthier Picard 14 / 53

Important Terms (cont.) Intent An intent is a kind of system message called event to glue activities Emitted by the device to alert applications that a certain event occured (system event) or by any other app (applicative event). System: insertion of SD card, SMS received Applicative: App HelloWorld starts, The user is arriving in Paris Service A Service is an application component representing either an application s desire to perform a longer-running operation while not interacting with the user or to supply functionality for other applications to use Does not need any UI Examples: Service verifying RSS flux updates Service enabling to listen a given playlist (independently from any activity) Android Programming Gauthier Picard 15 / 53

Important Terms (cont.) Manifest Starting point for every Android app Declares what the app contains (activities, services,...) Specifies how components are linked to Android (e.g. what is displayed in the menu?) Specifies permissions for the app (e.g. webcam, network, location access,...) Widget Portmanteau combining window and gadget Concretely it is a GUI component (labels, text fields, buttons,...) Android Programming Gauthier Picard 16 / 53

Important Terms (cont.) View Represents the basic building block for user interface components Occupies a rectangular area on the screen and is responsible for drawing and event handling View is the base class for widgets Container (View Group) A View Group is a special view that can contain other views (called children) Used to have a set of widgets to the desired presentation Most tools to build GUIs provide layout managers that are most frequently organized in containers Android Programming Gauthier Picard 17 / 53

Important Terms (cont.) XML Extensible Markup Language Used to structure data XML Layout Used to design GUI easier than using Java Used to instantiate widgets, position views and viewgroups The XML file is often generated using tools to build graphically GUIs Android Programming Gauthier Picard 18 / 53

Important Terms (cont.) Uniform Resource Identifier (URI) is a string of characters used to identify a name of a web resource (real or not) respects the Internet norm RFC 3986 Some URIs: Uniform Resource Locator (URL): identifies a resource on a network and provides means of acting upon or obtaining a representation of the resource by providing both the primary access mechanism, and the network "location" Example: http://www.wikipedia.org/ identifies a resource (Wikipedia s homepage) and implies a representation of this resource (HTML page) can be obtained via the HTTP protocol from an network host whose name is www.wikipedia.org Uniform Resource Name (URN): identifies a resource by its name in a specific namespace Example: urn:isbn:0-395-36341-1 identifies a resource by an ISBN number, allows to reference a book, but does not indicate where nor how to obtain a physical copy of it Android Programming Gauthier Picard 19 / 53

Important Terms (cont.) Android Virtual Devices AVDs allows to simulate the execution of an Android device on a computer These devices are customizable (Android version, CPU model, memory,...) Eases the development and the la implementation of apps Android Programming Gauthier Picard 20 / 53

Android Software Architecture Major components The Linux Kernel Hardware Abstraction Layer (HAL) Android Runtime Native C/C++ Libraries Java API Framework System Apps Android Programming Gauthier Picard 21 / 53

Agenda Android Overview Basics Android Projects Activities XML Layout Manifest Some Android-specific Classes References Android Programming Gauthier Picard 22 / 53

Create a project from an existing sample Android Programming Gauthier Picard 23 / 53

Create a project from an existing sample Android Programming Gauthier Picard 23 / 53

Create a project from an existing sample Android Programming Gauthier Picard 23 / 53

Create a project from an existing sample Android Programming Gauthier Picard 23 / 53

Create your own project Android Programming Gauthier Picard 24 / 53

Create your own project Android Programming Gauthier Picard 24 / 53

Create your own project Android Programming Gauthier Picard 24 / 53

Create your own project Android Programming Gauthier Picard 24 / 53

Create your own project Android Programming Gauthier Picard 24 / 53

Android Project Directory Structure Android Programming Gauthier Picard 25 / 53

Android Project Files.idea : Directory for IntelliJ IDEA settings app : Application module directories and files build : This directory stories the build output for all project modules gradle : Contains the gradler-wrapper files.gitignore : Specifies the untracked files that Git should ignore build.gradle : Customizable properties for the build system gradle.properties : Project-wide Gradle settings gradlew : Gradle startup script for Unix gradlew.bat : Gradle startup script for Windows local.properties : Customizable computer-specific properties for the build system, such as the path to the SDK installation.iml : Module file created by the IntelliJ IDEA to store module information settings.gradle : Specifies the sub-projects to build Android Programming Gauthier Picard 26 / 53

Android Application Modules build/ : Contains build folders for the specified build variants libs/ : Contains private libraries src/ : Contains your stub Activity file, which is stored at src/main/java//activityname>.java androidtest/ : Contains the instrumentation tests main/java/com.>project<.>app< : Contains Java code source for the app activities main/gen/ : Contains the Java files generated by Android Studio, such as your R.java file main/assets/ : This is empty ; you can use it to store raw asset files Android Programming Gauthier Picard 27 / 53

Android Application Modules (cont.) main/res/ : Contains application resources, such as drawable files, layout files, and string values in the following directories anim/ : For XML files that are compiled into animation objects color/ : For XML files that describe colors drawable/ : For bitmap files (PNG, JPEG, or GIF), 9-Patch image files, and XML files that describe Drawable shapes or Drawable objects that contain multiple states (normal, pressed, or focused) layout/ : XML files that are compiled into screen layouts (or part of a screen) menu/ : For XML files that define application menus raw/ : For arbitrary raw asset files values/ : For XML files that define resources by XML element type xml/ : For miscellaneous XML files that configure application components AndroidManifest.xml : The control file that describes the nature of the application and each of its components.gitignore/ : Specifies the untracked files ignored by git app.iml/ : IntelliJ IDEA module build.gradle : Customizable properties for the build system Android Programming Gauthier Picard 28 / 53

Agenda Android Overview Basics Android Projects Activities XML Layout Manifest Some Android-specific Classes References Android Programming Gauthier Picard 29 / 53

What is an Activity? Definition (Activity) An Activity is an application component that provides a screen with which users can interact in order to do something, such as dial the phone, take a photo, send an email, or view a map. Each activity is given a window in which to draw its user interface. The window typically fills the screen, but may be smaller than the screen and float on top of other windows. An Activity is a UI for a use case (á la UML): Example: Consider a SMS sending app One activity to choose the contact One activity to write the message One activity to display the message history From an operational point of view: An activity must extends the android.app.activity An activity is independent from the other activities BUT: You must specify a starting activity (the first one to be used) You must chain the activities (an activity activates another one) We consider (for simplification) that one activity corresponds to one XML layout Android Programming Gauthier Picard 30 / 53

Activity Lifecycle oncreate(): called when the activity is created if this is the first activity of the app, this method is executed when the user call the app this method is used to initialize: the XML view if necessary, the temporary files and data Android Programming Gauthier Picard 31 / 53

Activity Lifecycle onrestart(): called after the activity has been stopped, just prior to it being started again always followed by onstart() this method is therefore called when the app becomes the foreground app after a long pause Android Programming Gauthier Picard 31 / 53

Activity Lifecycle onstart(): called just before the activity becomes visible to the user this method is called after each call to oncreate() or onrestart() followed by onresume() if the activity comes to the foreground, or onstop() if it becomes hidden if necessary, it reloads data saved before the previous interruption Android Programming Gauthier Picard 31 / 53

Activity Lifecycle onresume(): called just before the activity starts interacting with the user (foreground) called after each onstart() at this point the activity is at the top of the activity stack, with user input going to it always followed by onpause() typically: it manages the connexion to the database it updates data that may be changed before onresume() Android Programming Gauthier Picard 31 / 53

Activity Lifecycle onpause(): Called when: the system is about to start resuming another activity the user asks a finish() for this activity the system needs to free some memory automatically called before onstop() typically: commit unsaved changes to persistent data stop animations disconnect the database... It should do whatever it does very quickly, because the next activity will not be resumed until it returns Android Programming Gauthier Picard 31 / 53

Activity Lifecycle onstop(): called when the activity is no longer visible to the user (destruction or another activity is foreground) followed either by onrestart() if the activity is coming back to interact with the user, or by ondestroy() if this activity is going away free resources Android Programming Gauthier Picard 31 / 53

Activity Lifecycle ondestroy(): called before the activity is destroyed this is the final call that the activity will receive it could be called either because the activity is finishing (someone called finish() on it), or because the system is temporarily destroying this instance of the activity to save space oncreate() should be called again to launch the activity if necessary free temporary files Android Programming Gauthier Picard 31 / 53

Agenda Android Overview Basics Android Projects Activities XML Layout Manifest Some Android-specific Classes References Android Programming Gauthier Picard 32 / 53

Simple XML Example (reminder) Example (A sample XML declaration for a library stock) 1 <?xml version="1.0" encoding="iso-8859-1"?> <!-- header of the XML file (i.e. prologue) --> 2 <BIBLIOTHEQUE> <!-- start tag for BIBLIOTHEQUE --> 3 4 <MAGAZINE> <!-- declaration of a MAGAZINE --> 5 <TITRE>Science et Vie</TITRE> 6 <DATEPARUTION>01-02-2012</DATEPARUTION> 7 <PRIX devise="euro">4.20</prix> <!-- "PRIX" is a tag, "devise" is an attribute, "32.30" is a value --> 8 </MAGAZINE> 9 10 <LIVRE type="education" > <!-- declaration of a LIVRE of type "education" --> 11 <TITRE>L'art du développement Android</TITRE> 12 <AUTEUR>Mark Murphy</AUTEUR> 13 <PRIX devise="euro">32.30 </PRIX> 14 </LIVRE> 15 16 <LIVRE type="roman"> <!-- declaration of a LIVRE of type "roman" --> 17 <TITRE>Le livre d'android</titre> 18 <AUTEUR>Patrick Beuzit</AUTEUR> 19 <PRIX devise="dollar">22.25</prix> 20 </LIVRE> 21 22 </BIBLIOTHEQUE> <!-- end tag for BIBLIOTHEQUE --> Android Programming Gauthier Picard 33 / 53

XML Layout Example Example (Sample XML Layout) 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:id="@id/actionbar_compat" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 android:orientation="vertical" > 6 7 <TextView 8 android:id="@+id/textview1" 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" 11 android:text="hello, I am a TextView" /> 12 13 <Button 14 android:id="@+id/button1" 15 android:layout_width="wrap_content" 16 android:layout_height="wrap_content" 17 android:text="hello, I am a Button" /> 18 19 <ImageView 20 android:id="@+id/imageview1" 21 android:layout_width="168dp" 22 android:layout_height="0dip" 23 android:layout_weight="0.36" 24 android:src="@drawable/ic_menu_share" /> 25 26 </LinearLayout> LinearLayout (lines 1 to 5) 1 <LinearLayout... / >: specifies the view group (here widgets will be inline) 2 android:id="...": declaration of the name (id) of the resource 3 android:layout_width="...": the width set to fill the parent (the screen) 4 android:layout_height="...": the height set to fill the parent (the screen) 5 android:orientation="...": the contained views are be vertically lined up Android Programming Gauthier Picard 34 / 53

XML Layout Example Example (Sample XML Layout) 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:id="@id/actionbar_compat" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 android:orientation="vertical" > 6 7 <TextView 8 android:id="@+id/textview1" 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" 11 android:text="hello, I am a TextView" /> 12 13 <Button 14 android:id="@+id/button1" 15 android:layout_width="wrap_content" 16 android:layout_height="wrap_content" 17 android:text="hello, I am a Button" /> 18 19 <ImageView 20 android:id="@+id/imageview1" 21 android:layout_width="168dp" 22 android:layout_height="0dip" 23 android:layout_weight="0.36" 24 android:src="@drawable/ic_menu_share" /> 25 26 </LinearLayout> TextView (lines 7 to 11) 7 <TextView... / >: the view is a label (text) 8 android:id="@+id/text": the id associated to this view is textview1 9 android:layout_width="wrap_content": the width is adapted to the contained text 10 android:layout_height="wrap_content": the height is adapted to the contained text 11 android:text="hello, I am a TextView": the displayed text Android Programming Gauthier Picard 34 / 53

XML Layout Example Example (Sample XML Layout) 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:id="@id/actionbar_compat" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 android:orientation="vertical" > 6 7 <TextView 8 android:id="@+id/textview1" 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" 11 android:text="hello, I am a TextView" /> 12 13 <Button 14 android:id="@+id/button1" 15 android:layout_width="wrap_content" 16 android:layout_height="wrap_content" 17 android:text="hello, I am a Button" /> 18 19 <ImageView 20 android:id="@+id/imageview1" 21 android:layout_width="168dp" 22 android:layout_height="0dip" 23 android:layout_weight="0.36" 24 android:src="@drawable/ic_menu_share" /> 25 26 </LinearLayout> Button (lines 13 to 17) 13 <Button.../ >: the view is a button 14 android:id="@+id/button1": the id of the button is button1 (cf. Java code) In Java, we can create an instance of class Button to manipulate this view: Button mybutton = (Button) findviewbyid(r.id.button); Android Programming Gauthier Picard 34 / 53

XML Layout Example Example (Sample XML Layout) 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:id="@id/actionbar_compat" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 android:orientation="vertical" > 6 7 <TextView 8 android:id="@+id/textview1" 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" 11 android:text="hello, I am a TextView" /> 12 13 <Button 14 android:id="@+id/button1" 15 android:layout_width="wrap_content" 16 android:layout_height="wrap_content" 17 android:text="hello, I am a Button" /> 18 19 <ImageView 20 android:id="@+id/imageview1" 21 android:layout_width="168dp" 22 android:layout_height="0dip" 23 android:layout_weight="0.36" 24 android:src="@drawable/ic_menu_share" /> 25 26 </LinearLayout> ImageView (lines 19 to 24) 19 <ImageView... / >: the view is an image 20 android:id="@+id/imageview1": the id of the image view is imageview1 21 android:layout_width="161dp": the width of the image is 168dot point 22 android:layout_height="wrap_content": the height is adapted to the content 23 android:layout_weight="0.36": see the property android:layout_weight 24 android:src="@android:drawable/ic_menu_share": URI of the image Android Programming Gauthier Picard 34 / 53

Integrate Layouts in Your App Steps In your XML editor (e.g. Android Studio): 1. Create an XML layout (pay particular attention to the views ids) Dans le programme Java: 2. Load the layout 3. Instantiate the views upon which your want to act Android Programming Gauthier Picard 35 / 53

(1) Create an XML layout Manual Edition Documentation available on http://developer.android.com/guide/topics/ui/declaring-layout.html Create a new.xml file in the directory res/layout Edit the layout with an XML editor Android Programming Gauthier Picard 36 / 53

(1) Create an XML layout (cont.) (1.a) Choosing a Layout LinearLayout: arranges its children in a single column or a single row AbsoluteLayout : lets you specify exact locations (x/y coordinates) of its children RelativeLayout : the positions of the children can be described in relation to each other or to the parent TableLayout : arranges its children into rows and columns (1.b) Choosing and Positionning the Views Android Programming Gauthier Picard 37 / 53

(1) Create an XML layout (cont.) EditText : an editable text field Toast :a view containing a quick popup message for the user ImageView : an arbitrary image, such as an icon CheckBox : a specific type of two-states button that can be either checked or unchecked Button : a push-button widget that can be pressed, or clicked, by the user to perform an action RadioButton : a two-states button that can be either checked or unchecked ImageButton: a button with an image (instead of text) that can be pressed or clicked by the user DatePicker : a widget for selecting a date SlidingDrawer : hides content out of the screen and allows the user to drag a handle to bring the content on screen Android Programming Gauthier Picard 38 / 53

(1) Create an XML layout (cont.) Creation with Android Studio Android Programming Gauthier Picard 39 / 53

(1) Create an XML layout (cont.) Creation with Android Studio Android Programming Gauthier Picard 39 / 53

(1) Create an XML layout (cont.) Creation with Android Studio Android Programming Gauthier Picard 39 / 53

(2) Load the Layout Linking Java Code and Layouts When the app is compiled, each XML layout is compiled as a View resource The application code must load the layout in the oncreate() method of the activity calling the setcontentview() method The setcontentview() method takes as input the rerefence of the layout (its id), e.g. R.layout.layout1 Example (A minimal oncreate() method loading a layout) public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main_layout); } Android Programming Gauthier Picard 40 / 53

(3) Instantiate the Views Manipulating views by id Create an instance of each object on which you want to add a handler in the oncreate() method of your activity Use the findviewbyid() to get the instance for your view (using the id defined in the XML layout) Manipulate your view (e.g. add an event listener for a button, get the content of an editable text field) Example (A sample oncreate() method adding an event listener to a Button) public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.layout1); Button button1 = findviewbyid(r.id.button1).setonclicklistener(new View.OnClickListener() { @Override public void onclick(view view) { // perform an action on click } }); } Android Programming Gauthier Picard 41 / 53

Agenda Android Overview Basics Android Projects Activities XML Layout Manifest Some Android-specific Classes References Android Programming Gauthier Picard 42 / 53

What is the manifest file? Definition (Manifest File) Every application must have an AndroidManifest.xml file (with precisely that name) in its root directory. The manifest presents essential information about the application to the Android system, information the system must have before it can run any of the application s code. XML file Specify the architecture of the app AndroidManifest.xml is at the root of the project Android Programming Gauthier Picard 43 / 53

Contents In a glance... It names the Java package for the app (the package name unique identifier for the app!) It describes the components of the app It lists the activities, services, and content providers that the app is composed of It names the classes that implement each of the components and publishes their capabilities (e.g. which Intent messages they can handle) These let the Android system know what the components are and under what conditions they can be launched It determines which processes will host app components It declares which permissions the app must have in order to access protected parts of the API (GPS, internet, etc.) and interact with other apps It declares the minimum level of the Android API that the app requires It lists the libraries that the app must be linked against It lists the Instrumentation classes that provide profiling and other information as the app is running (for development issues) Android Programming Gauthier Picard 44 / 53

AndroidManifest.xml Conventions Only two elements are required: <manifest>: contains the package, the version... this root englobes the whole manifest <application>: describes the app and contains the list of its components All values are set through attributes, not as character data within an element All attribute names begin with an android: prefix (except for some attributes of the root <manifest> element) Resources Instead of containing data per se, the manifest refers to resources using the following format: @[package:]type:name e.g. <activity android:icon="@drawable/smallpic"...> Resources are stored in the res directory of the app Android Programming Gauthier Picard 45 / 53

AndroidManifest.xml (cont.) Permissions If an application needs access to a feature protected by a permission, it must declare that it requires that permission Specifiy permission with a <uses-permission> element in the manifest There exist several standard permissions: android.permission.call_emergency_numbers android.permission.read_owner_data android.permission.set_wallpaper android.permission.device_power It is also possible to define your own permissions Android Programming Gauthier Picard 46 / 53

AndroidManifest.xml (cont.) Example (A sample manifest file declaring permissions) 1 <manifest... > 2 <permission android:name="com.example.project.debit_acct"... /> 3 <uses-permission android:name="com.example.project.debit_acct" /> 4... 5 <application...> 6 <activity android:name="com.example.project.freneticactivity" 7 android:permission="com.example.project.debit_acct" 8... > 9... 10 </activity> 11 </application> 12 </manifest> Android Programming Gauthier Picard 47 / 53

AndroidManifest.xml (cont.) Intent Filter Components advertise their capabilities âăť the kinds of intents they can respond to âăť through intent filters A component may have any number of filters, each one describing a different capability (e.g. a filter to edit an existing document, a filter to initialize a new document, etc.) A filter must point to an action Android Programming Gauthier Picard 48 / 53

AndroidManifest.xml(cont.) Example (A sample manifest file) 1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.eyrolles.android.multimedia" 4 android:versioncode="1" 5 android:versionname="1.0"> 6 <application android:icon="@drawable/icon" android:label="@string/app_name"> 7 <activity android:name=".capturephoto" 8 android:label="@string/app_name"> 9 <intent-filter> 10 <action android:name="android.intent.action.main" /> 11 <category android:name="android.intent.category.launcher" /> 12 </intent-filter> 13 </activity> 14 </application> 15 <uses-sdk android:minsdkversion="3" /> 16 <uses-permission android:name="android.permission.camera"/> 17 </manifest> Android Programming Gauthier Picard 49 / 53

Agenda Android Overview Basics Android Projects Activities XML Layout Manifest Some Android-specific Classes References Android Programming Gauthier Picard 50 / 53

Deep into the Android-specific classes Let s look at the online documentation... android.widget.toast android.app.alarmmanager android.media.audiomanager android.os.asynctask android.hardware.camera android.hardware.sensor Android Programming Gauthier Picard 51 / 53

Agenda Android Overview Basics Android Projects Activities XML Layout Manifest Some Android-specific Classes References Android Programming Gauthier Picard 52 / 53

References Books www.it-ebooks.info Other documents (lectures...) I Romain Raveaux, Cours Android DÃľveloppement et API, Laboratoire L3I, IUT de La Rochelle I Nazim Benbourahla, several resources at developpez.com I Philippe Lacomme, Raksmey Phan, CrÃľer des applications Android Android Programming Gauthier Picard 53 / 53