Java & Android. Java Fundamentals. Madis Pink 2016 Tartu

Similar documents
Diving into Android. By Jeroen Tietema. Jeroen Tietema,

Android HelloWorld - Example. Tushar B. Kute,

Mobile Application Development - Android

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

Introduction To Android

Hello World. Lesson 1. Android Developer Fundamentals. Android Developer Fundamentals. Layouts, and. NonCommercial

Android Application Development. By : Shibaji Debnath

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

Android Development Tutorial. Yi Huang

Mobile Programming Lecture 1. Getting Started

Android Application Development

EMBEDDED SYSTEMS PROGRAMMING Application Basics

INTRODUCTION TO ANDROID

Introduction to Android Development

Android Workshop: Model View Controller ( MVC):

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

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

Security model. Marco Ronchetti Università degli Studi di Trento

COMP4521 EMBEDDED SYSTEMS SOFTWARE

Computer Science E-76 Building Mobile Applications

Android Beginners Workshop

Mobile Application Development Android

Applications. Marco Ronchetti Università degli Studi di Trento

Real-Time Embedded Systems

Embedded Systems Programming - PA8001

Software Practice 3 Before we start Today s lecture Today s Task Team organization

Android Apps Development for Mobile and Tablet Device (Level I) Lesson 2

Android Basics. Android UI Architecture. Android UI 1

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

CSCU9YH Development with Android

University of Stirling Computing Science Telecommunications Systems and Services CSCU9YH: Android Practical 1 Hello World

Produced by. Mobile Application Development. Eamonn de Leastar

User Interface: Layout. Asst. Prof. Dr. Kanda Runapongsa Saikaew Computer Engineering Khon Kaen University

App Development for Android. Prabhaker Matet

Android UI Development

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

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

Solving an Android Threading Problem

Android App Development. Ahmad Tayeb

Introduction to Android

EMBEDDED SYSTEMS PROGRAMMING Application Tip: Switching UIs

BCA 6. Question Bank

CS 528 Mobile and Ubiquitous Computing Lecture 2a: Android UI Design in XML + Examples. Emmanuel Agu

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

Lab 1: Getting Started With Android Programming

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

ANDROID USER INTERFACE

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

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

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

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

Chapter 5 Defining the Manifest

Agenda. The Android GUI Framework Introduction Anatomy A real word example Life cycle Findings

Mobile Application Development

IEMS 5722 Mobile Network Programming and Distributed Server Architecture

Android Application Model I

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

Android Basics. - Bhaumik Shukla Android Application STEALTH FLASH

ANDROID SYLLABUS. Advanced Android

CS 4518 Mobile and Ubiquitous Computing Lecture 3: Android UI Design in XML + Examples. Emmanuel Agu

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

PROGRAMMING APPLICATIONS DECLARATIVE GUIS

Android for Java Developers Dr. Markus Schmall, Jochen Hiller

Android & iphone. Amir Eibagi. Localization

Android Programming (5 Days)

CS 528 Mobile and Ubiquitous Computing Lecture 2: Intro to Android Programming Emmanuel Agu

Fig. 2.2 New Android Application dialog. 2.3 Creating an App 41

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

Mobile Development Lecture 8: Intents and Animation

Android App Development

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

CS 234/334 Lab 1: Android Jump Start

Lecture 1 - Introduction to Android

Syllabus- Java + Android. Java Fundamentals

Android UI: Overview

(Refer Slide Time: 0:48)

Software Practice 3 Today s lecture Today s Task Porting Android App. in real device

MARS AREA SCHOOL DISTRICT Curriculum TECHNOLOGY EDUCATION

Multiple devices. Use wrap_content and match_parent Use RelativeLayout/ConstraintLayout Use configuration qualifiers

Starting Another Activity Preferences

EMBEDDED SYSTEMS PROGRAMMING Android NDK

Android Programmierung leichtgemacht. Lars Vogel

1. Implementation of Inheritance with objects, methods. 2. Implementing Interface in a simple java class. 3. To create java class with polymorphism

CS 4330/5390: Mobile Application Development Exam 1

UNDERSTANDING ACTIVITIES

MC Android Programming

Multiple Activities. Many apps have multiple activities

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

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

Android Application Model I. CSE 5236: Mobile Application Development Instructor: Adam C. Champion, Ph.D. Course Coordinator: Dr.

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

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

CS260 Intro to Java & Android 05.Android UI(Part I)

android-espresso #androidespresso

Android System Architecture. Android Application Fundamentals. Applications in Android. Apps in the Android OS. Program Model 8/31/2015

EMBEDDED SYSTEMS PROGRAMMING UI and Android

CS260 Intro to Java & Android 05.Android UI(Part I)

Questions and Answers. Q.1) Which of the following is the most ^aeuroeresource hungry ^aeuroepart of dealing with activities on android?

Lecture 2 Android SDK

Comparative Study on Layout and Drawable Resource Behavior in Android for Supporting Multi Screen

Transcription:

Java & Android Java Fundamentals Madis Pink 2016 Tartu 1

Agenda» Brief background intro to Android» Android app basics:» Activities & Intents» Resources» GUI» Tools 2

Android» A Linux-based Operating System» Announced by Google & the Open Handset Alliance Nov 2007» First device with Android 1.0 shipped in 2008 3

Android» The primary language for writing apps is actually Java» The standard library is mainly Java 6 with a lot of Android-specific classes on top» There are 25 API versions, most apps target Android 4.4 (API 19) or higher today 4

VMs on Android» Dalvik - all Android versions up to 4.4, just-intime compilation added in Android version 2.2» ART - New runtime with ahead-of-time compilation, shipped with Android 5.0 and later» Both operate on DEX (Dalvik EXecutable) bytecode 5

Dex Bytecode» Design goals» Compilation target for.java sources» Code size» Interpreting performance» Bytecode instructions operate on registers» Many classes in a single.dex file 6

Dex Toolchain(s)» Dexer (dx) - transforms Java 7 bytecode (.class) to dex» Jack & Jill - Experimental direct Java compilation to dex, supports Java 8 7

Dex & Dalvik Example class Hello { public static void main(string[] args) { System.out.println("Hello world!"); } } 8

Dex & Dalvik Example $ javac Hello.java -target 1.7 -source 1.7 $ java Hello Hello world! 9

Dex & Dalvik Example $ javac Hello.java -target 1.7 -source 1.7 $ java Hello Hello world! $ dx --dex --output=out.dex Hello.class 10

Dex & Dalvik Example $ javac Hello.java -target 1.7 -source 1.7 $ java Hello Hello world! $ dx --dex --output=out.dex Hello.class $ adb push out.dex /data/local/tmp/out.dex 11

Dex & Dalvik Example $ javac Hello.java -target 1.7 -source 1.7 $ java Hello Hello world! $ dx --dex --output=out.dex Hello.class $ adb push out.dex /data/local/tmp/out.dex $ adb shell dalvikvm -cp /data/local/tmp/out.dex Hello Hello world! 12

Anatomy of an App Each app is an.apk file - a zip in disguise - with the following contents:» AndroidManifest.xml - the manifest, describes the app» classes.dex - the dex bytecode» resources.arsc - compiled Android resources» res/* - compiled layouts, PNG bitmaps, etc» assets/* - blob assets: databases, audio files, etc 13

Anatomy of an App» No main(string[])» Components as entry points:» Activities» Services» Broadcast receivers» Content Providers» These are defined in the AndroidManifest.xml 14

Android Projects» Built with Gradle!» src/main/java - Java sources» src/main/res - Android resources» src/main/androidmanifest.xml - the manifest» build.gradle - Gradle build file, describes how to build the app 15

Intermission all samples at https://github.com/javafundamentalszt/jf-android-samples 16

Activities 17

Activity» class * extends android.app.activity» Started with an Intent - a user action» Represents a screen in an application» Owns a View hierarchy (GUI objects)» Handles user input events» Instantiated by the framework» Lifecycle controlled by the framework 18

Activities Example - Email App» InboxActivity - lists all emails in Inbox» ViewEmailActivity - view a single email» ComposeActivity - compose/send a new email» LoginActivity - log the user in 19

Activity Lifecycle» Driven by the framework through calls to specific Activity methods» oncreate/ondestroy - activity created» onstart/onstop - activity visible» onresume/onpause - activity is "on top"» When overriding any of these, make sure to call to super! 20

Starting an Activity - Intents» All Activities are started by defining an Intent - an object that represents user's intentions» Sort of like a request that apps can respond to» Requires a Context instance to send the Intent to the OS» All components, including activities extend Context» Two kinds of intents - explicit and implicit 21

Explicit Intents public void clickhelp() { // launch HelpActivity of this app Intent i = new Intent(context, HelpActivity.class); context.startactivity(i); }» Used to launch a specific activity on the device, usually within the same app 22

Implicit Intents public void sendfeedback() { Uri uri = Uri.parse("mailto:jf@zeroturnaround.com"); Intent i = new Intent(Intent.ACTION_SENDTO, uri); startactivity(i); }» Used for general actions like Send email, Open a webpage, Take a picture etc 23

Implicit Intents 24

Implicit Intents public void openwebpage() { Uri uri = Uri.parse("https://courses.cs.ut.ee/2016/javaFund/fall"); Intent i = new Intent(Intent.ACTION_VIEW, uri); startactivity(i); } 25

Implicit Intents 26

Defining an Activity package org.zeroturnaround.jf.android; import android.app.activity; public class HelloActivity extends Activity { } 27

Defining an Activity package org.zeroturnaround.jf.android; import android.app.activity; import android.os.bundle; import android.widget.toast; public class HelloActivity extends Activity { protected void oncreate(bundle savedinstancestate) { // always need to call super super.oncreate(savedinstancestate); // display our message String msg = "Hello World!"; Toast.makeText(this, msg, Toast.LENGTH_SHORT).show(); } } 28

AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="org.zeroturnaround.jf.android" > <application> <activity android:name="org.zeroturnaround.jf.android.helloactivity"> <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> </application> </manifest> 29

AndroidManifest.xml The <manifest /> tag tells us that this is an app with the application ID org.zeroturnaround.jf.android <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="org.zeroturnaround.jf.android" > <application> <!-- components go here --> </application> </manifest> 30

AndroidManifest.xml The <activity /> tag declares an Activity component with the classname org.zeroturnaround.jf.android.helloactivity <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="org.zeroturnaround.jf.android" > <application> <activity android:name="org.zeroturnaround.jf.android.helloactivity"> <!-- optional intent filter goes here --> </activity> </application> </manifest> 31

AndroidManifest.xml The <intent-filter /> tag declares which implicit Intents our Activity handles, in this case it is the MAIN action under the LAUNCHER category This makes our app visible in the Launcher app <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> 32

AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="org.zeroturnaround.jf.android" > <application> <activity android:name="org.zeroturnaround.jf.android.helloactivity"> <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> </application> </manifest> 33

34

More Information Activities: https://d.android.com/guide/ components/activities.html Intents: https://d.android.com/guide/ components/intents-filters.html Manifest: https://d.android.com/guide/topics/ manifest/manifest-intro.html 35

Resources 36

37

Resources» Placed in src/main/res folder» Precompiled by aapt during the Gradle build» Value resources - strings, floats, integers, booleans,...» Complex resources - arrays, plurals,...» File resources - bitmaps, XMLs, drawables, layouts,... 38

Value Resources <?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">hello</string> <string name="hello_world">hello World!</string> </resources>» Placed in src/main/res/values/<filename>.xml, where <FILENAME> can be anything, like strings.xml 39

Drawables» Placed in src/main/res/drawable» Can be XML (describing shapes, colours, gradients) or bitmaps» Automatically scaled by the OS 40

Layouts» Placed in src/main/res/layout» XML files for describing UI» More on these later :) 41

Accessing Resources from XML» Resources can be referenced in XML resources (including the manifest) via @<type>/ <identifier>» A few examples:» @string/app_name» @drawable/ic_launcher 42

Accessing Resources from Code» All resources get a runtime integer ID, placed in a class called R» Need to dereference through a Resources instance, obtained through a Context» Example dereferences:» getresources().getstring(r.string.app_name)» getresources().getdrawable(r.drawable.ic_launcher) 43

Resource Qualifiers» Resource folders can have extra qualifiers with -suffixes» values-et containing Estonian strings» Framework decides at runtime which resource to use» Also used to have custom layouts for tablets, different drawables for device densities, etc 44

Resource Qualifiers Resource qualifier examples:» res/layout-land for landscape layouts» res/drawable-xhdpi, res/drawable-hdpi, res/ drawable-mdpi for icons at various resolutions» res/values-et, res/values-ru for localization 45

Resources <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="org.zeroturnaround.jf.android" > <application> <!-- components go here --> </application> </manifest> 46

Resources <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="org.zeroturnaround.jf.android" > <application android:label="hello World"> <!-- components go here --> </application> </manifest> 47

Resources <?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">hello</string> <string name="hello_world">hello World!</string> </resources> 48

Resources <application android:label="hello World"> 49

Resources <application android:label="@string/app_name"> 50

Resources <application android:label="@string/app_name" android:icon="@drawable/ic_launcher"> 51

Resources // display our message String msg = "Hello World!"; Toast.makeText(this, msg, Toast.LENGTH_SHORT).show(); 52

Resources // display our message String msg = getresources().getstring(r.string.hello_world); Toast.makeText(this, msg, Toast.LENGTH_SHORT).show(); 53

Resources 54

More Information Resources overview - https://d.android.com/ guide/topics/resources/overview.html 55

GUI 56

GUI - Units» No absolute coordinates!» Units:» dip (dp) - density-independent pixels» sp - scaled pixels, scale with the text size» px - actual physical pixels» Even with dp the sizes of screens vary wildly 57

View Hierarchy» ViewGroup (container/node) and View (leaf node)» View groups lay out their children, which could be nested groups» Can be declared through code» Can be inflated from XML layouts» Use setcontentview(view root) or setcontentview(int layoutresid) to attach a hierarchy to an Activity 58

Views» TextView - displays text» ImageView - displays images» EditText - text input» Button - clickable TextView with various states 59

ViewGroups» FrameLayout - simple layouts for framing children» LinearLayout - lays views out in a single row/ column» RelativeLayout - lays views out in relation to each other with rules» ListView - scrolling a lot of similar views with caching 60

Views protected void oncreate(bundle savedinstancestate) { // always need to call super super.oncreate(savedinstancestate); // display our message String msg = getresources().getstring(r.string.hello_world); Toast.makeText(this, msg, Toast.LENGTH_SHORT).show(); } 61

Views Replace our toast with a TextView // display our message String msg = "Hello World!"; TextView root = new TextView(this); root.settext(msg); setcontentview(root); 62

Views src/main/res/layout/hello.xml: <?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:text="@string/hello_world" /> 63

Views src/main/res/layout/hello.xml: <?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:text="@string/hello_world" />... and in our HelloActivity.onCreate: setcontentview(r.layout.hello); 64

Views 65

Views Add some padding with android:padding <?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:text="@string/hello_world" android:padding="16dp" /> 66

Views Make the font larger with android:textsize <?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:text="@string/hello_world" android:padding="16dp" android:textsize="32sp" /> 67

Views 68

Handling Events» Views can be given generated IDs via android:id="@+id/someidentifier"» This will generate a unique integer accessible via R.id.someIdentifier» Use findviewbyid(int id) to find the view in code» Note! Always returns View so might need to cast 69

Handling Events Adding a button to our view <Button android:id="@+id/my_button" android:layout_width="match_parent" android:layout_height="match_parent" android:text="@string/click" /> 70

Handling Events Adding a button to our view <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="16dp" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="@string/hello_world" android:textsize="32sp" /> <Button android:id="@+id/my_button" android:layout_width="match_parent" android:layout_height="match_parent" android:text="@string/click" /> </LinearLayout> 71

Handling Events We can use setonclicklistener to attach a handler to the click event setcontentview(r.layout.hello); // attach an OnClickListener to the view with id `my_button` Button mybutton = (Button) findviewbyid(r.id.my_button); mybutton.setonclicklistener(new View.OnClickListener() { @Override public void onclick(view v) { Toast.makeText(v.getContext(), R.string.click, Toast.LENGTH_SHORT).show(); } }); 72

Handling Events 73

More information Layouts: https://d.android.com/guide/topics/ui/ declaring-layout.html Handling events: https://d.android.com/guide/ topics/ui/ui-events.html 74

Tools 75

Android Studio in 5 minutes» The official IDE for Android development» Based on IntelliJ IDEA Community Edition» Download: https://d.android.com/studio/ index.html» Before opening any project, open Configure -> SDK Manager to download necessary SDK components for the homework 76

Android Studio - Configuring SDK Under the SDK Platforms tab, click Show Package Details and make sure the following items are checked in the Android 7.0 (Nougat) category:» Android SDK Platform 24» Sources for Android 24» Google APIs Intel x86 Atom System Image 77

Android Studio - Configuring SDK Under the SDK Tools tab, click Show Package Details and make sure the following items are checked:» 25.0.1 under Android SDK Build-Tools» Android SDK Platform-Tools 25.0.1» Android SDK Tools 25.2.3» Intel x86 Emulator Accelerator (HAXM installer) 78

Android Studio - Configuring SDK With that being done, press Apply, read through and Accept both Android SDK license and Intel licenses and press Next to install the components 79

Android Studio in 5 minutes» Import the homework/samples folder via Open an existing Android Studio project» Find the green ' ' next to a dropdown in the toolbar» Studio will ask you to create an emulator, any emulator with the version later than 4.0.3 (API 15) will do» Note: we've already downloaded the image for Nougat 7.0» You should see the app on the emulator \o/ 80

Developing with a Device» Settings -> About phone» Scroll down & tap Build number 7 times» You are now a developer!» Settings -> Developer options» Make sure USB Debugging is enabled 81

Gradle» Project layout similar to Maven» Instead of XML the build is defined through a Groovy DSL» No installation needed, gradlew will download Gradle for you» gradlew build - builds the project 82

More Information Android Studio - https://d.android.com/studio/ index.html Android and Android Studio: Getting Started (~10min video) - https://youtu.be/z98hxv9gmzy 83

Homework 14 84

Homework 14 All the details are at: https://github.com/javafundamentalszt/jf-hwandroid-calculator Due 2016-12-05 23:59:59 Estonian time Do not leave this for the last minute! Help/questions: jf@zeroturnaround.com 85

Links Samples - https://github.com/javafundamentalszt/jf-android-samples Activities - https://d.android.com/guide/components/activities.html Intents - https://d.android.com/guide/components/intents-filters.html Manifest - https://d.android.com/guide/topics/manifest/manifest-intro.html Resources overview - https://d.android.com/guide/topics/resources/overview.html Layouts - https://d.android.com/guide/topics/ui/declaring-layout.html Handling events - https://d.android.com/guide/topics/ui/ui-events.html Android Studio - https://d.android.com/studio/index.html Android and Android Studio: Getting Started (~10min video) - https://youtu.be/z98hxv9gmzy 86