CS 528 Mobile and Ubiquitous Computing Lecture 4a: Fragments, Camera Emmanuel Agu

Similar documents
CS 4518 Mobile and Ubiquitous Computing Lecture 5: Rotating Device, Saving Data, Intents and Fragments Emmanuel Agu

CS 4518 Mobile and Ubiquitous Computing Lecture 6: Databases, Camera, Face Detection Emmanuel Agu

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

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

CS 403X Mobile and Ubiquitous Computing Lecture 5: Web Services, Broadcast Receivers, Tracking Location, SQLite Databases Emmanuel Agu

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

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

CS 4518 Mobile and Ubiquitous Computing Lecture 5: Data-Driven Views and Android Components Emmanuel Agu

ACTIVITY, FRAGMENT, NAVIGATION. Roberto Beraldi

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

CS 528 Mobile and Ubiquitous Computing Lecture 4b: Face Detection, recognition, interpretation + SQLite Databases Emmanuel Agu

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

CS 4518 Mobile and Ubiquitous Computing Lecture 4: WebView (Part 2) Emmanuel Agu

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

CS378 -Mobile Computing. Anatomy of and Android App and the App Lifecycle

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

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

CS 528 Mobile and Ubiquitous Computing Lecture 4a: Playing Sound and Video Emmanuel Agu

CS371m - Mobile Computing. More UI Navigation, Fragments, and App / Action Bars

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

CS371m - Mobile Computing. More UI Action Bar, Navigation, and Fragments

CS378 - Mobile Computing. Anatomy of an Android App and the App Lifecycle

Minds-on: Android. Session 2

CS 4330/5390: Mobile Application Development Exam 1

ANDROID MOCK TEST ANDROID MOCK TEST IV

Fragments and the Maps API

Tablets have larger displays than phones do They can support multiple UI panes / user behaviors at the same time

Android Basics. Android UI Architecture. Android UI 1

ACTIVITY, FRAGMENT, NAVIGATION. Roberto Beraldi

Application Fundamentals

Activities and Fragments

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

Lifecycle-Aware Components Live Data ViewModel Room Library

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

Android App Development. Ahmad Tayeb

Spring Lecture 5 Lecturer: Omid Jafarinezhad

Produced by. Mobile Application Development. David Drohan Department of Computing & Mathematics Waterford Institute of Technology

CMSC436: Fall 2013 Week 3 Lab

INTRODUCTION TO ANDROID

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

Android Application Development

UI Fragment.

CS378 -Mobile Computing. Intents

CHAPTER 4. Fragments ActionBar Menus

Mobile Computing. Introduction to Android

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

Understanding Application

Overview of Activities

COSC 3P97 Mobile Computing

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

COLLEGE OF ENGINEERING, NASHIK-4

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

EMBEDDED SYSTEMS PROGRAMMING Application Basics

University of Babylon - College of IT SW Dep. - Android Assist. Lect. Wadhah R. Baiee Activities

Android Application Model I

what we will cover - setting up multiple activities - intents - lab 2 What you need to know for Lab 2

CE881: Mobile & Social Application Programming

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

Introduction to Android development

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

Lecture 2 Android SDK

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

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

Android Fundamentals - Part 1

LECTURE NOTES OF APPLICATION ACTIVITIES

Android Programmierung leichtgemacht. Lars Vogel

CS 403X Mobile and Ubiquitous Computing Lecture 14: Google Places, Other Useful Android APIs and Cool Location Aware Apps Emmanuel Agu

Android Programming Lecture 2 9/7/2011

Programming with Android: Android for Tablets. Dipartimento di Scienze dell Informazione Università di Bologna

Android Camera. Alexander Nelson October 6, University of Arkansas - Department of Computer Science and Computer Engineering

ORACLE UNIVERSITY AUTHORISED EDUCATION PARTNER (WDP)

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

Produced by. Mobile Application Development. David Drohan Department of Computing & Mathematics Waterford Institute of Technology

CS 235AM, Mobile Application Development: Android Spring 2016

Mobile Computing Fragments

Fragments. Fragments may only be used as part of an ac5vity and cannot be instan5ated as standalone applica5on elements.

Java Training Center - Android Application Development

Building User Interface for Android Mobile Applications II

ANDROID DEVELOPMENT. Course Details

UNDERSTANDING ACTIVITIES

Lifecycle Callbacks and Intents

Developing Android Applications

States of Activities. Active Pause Stop Inactive

CS 4518 Mobile and Ubiquitous Computing Lecture 10: Location-Aware Computing Emmanuel Agu

Activities. Repo:

Google Maps Troubleshooting

ANDROID SYLLABUS. Advanced Android

Camera and Intent. Elena Fortini

Android Ecosystem and. Revised v4presenter. What s New

Embedded Systems Programming - PA8001

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

Exercise 1: First Android App

EMBEDDED SYSTEMS PROGRAMMING UI and Android

Mobile Development Lecture 10: Fragments

CS378 -Mobile Computing. What's Next?

Fragments. Lecture 11

ANDROID APPS (NOW WITH JELLY BEANS!) Jordan Jozwiak November 11, 2012

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

CS 528 Mobile and Ubiquitous Computing Lecture 5b: Location-Aware Computing Emmanuel Agu

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

Transcription:

CS 528 Mobile and Ubiquitous Computing Lecture 4a: Fragments, Camera Emmanuel Agu

Fragments

Recall: Fragments Sub-components of an Activity (screen) An activity can contain multiple fragments, organized differently on different devices (e.g. phone vs tablet) Fragments need to be attached to Activities.

Fragments Ref: Android Nerd Ranch (2 nd ed), Ch 7, pg 121 To illustrate fragments, we create new app CriminalIntent Used to record office crimes e.g. leaving plates in sink, etc Record includes: Title, date, photo List-detail app using fragments On tablet: show list + detail On phone: swipe to show next crime Fragment 1 (list of Crimes) Fragment 2 (Details of selected Crime)

Fragments Activities can contain multiple fragments Fragment s views are inflated from a layout file Can rearrange fragments as desired on an activity i.e. different arrangement on phone vs tablet

Starting Criminal Intent Initially, develop detail view of CriminalIntent using Fragments Final Look of CriminalIntent Start small Develop detail view using Fragments

Starting Criminal Intent Crime: holds record of 1 office crime. Has Title e.g. Someone stole my yogurt! ID: unique identifier of crime CrimeFragment: UI fragment to display Crime Details CrimeActivity: Activity that contains CrimeFragment Next: Create CrimeActivity

Create CrimeActivity in Android Studio Creates CrimeActivity.java Formatted using activity_crime.xml

Fragment Hosted by an Activity Each fragment must be hosted by an Activity To host a UI fragment, an activity must Define a spot in its layout for the fragment Manage the lifecycle of the fragment instance (next) E.g.: CrimeActivity defines spot for CrimeFragment

Fragment s Life Cycle Fragment s lifecycle similar to activity lifecycle Has states running, paused and stopped Also has some similar activity lifecycle methods (e.g. onpause(), onstop( ), etc) Key difference: Android OS calls Activity s oncreate, onpause( ), etc Fragment s oncreateview( ), onpause( ), etc called by hosting activity NOT Android OS! E.g. Fragment has oncreateview

Hosting UI Fragment in an Activity 2 options. Can add fragment to either Activity s XML file (layout fragment), or Activity s.java file (more complex but more flexible) We will add fragment to activity s.java file now First, create a spot for the fragment s view in CrimeActivity s layout

Creating a UI Fragment Creating Fragment is similar to creating activity 1. Define widgets in a layout (XML) file 2. Create java class and specify layout file as XML file above 3. Get references of inflated widgets in java file (findviewbyid), etc XML layout file for CrimeFragment (fragment_crime.xml)

Java File for CrimeFragment In CrimeFragment Override CrimeFragment s oncreateview( ) function Format Fragment using fragment_crime.xml Note: Fragment s view inflated in Fragment.onCreateView(), NOT oncreate

Wiring up the EditText Widget Add listener to listen for text change events Get handle to EditText widget Store user s input as Crime Title (if text entered)

Adding UI Fragment to FragmentManager An activity adds new fragment to activity using FragmentManager FragmentManager Manages fragments Adds fragment s views to activity s view Handles List of fragments Back stack of fragment transactions Find Fragment using its ID Interactions with FragmentManager are done using transactions Add Fragment to activity s view

Examining Fragment s Lifecycle FragmentManager calls fragment lifecycle methods 1. onattach( ), oncreate( ) and oncreateview() called when a fragment is added to FragmentManager 1. First create fragment.. then wait for Activity to add fragment

Examining Fragment s Lifecycle FragmentManager calls fragment lifecycle methods onattach( ), oncreate( ) and oncreateview() called when a fragment is added to FragmentManager onactivitycreated( ) called after hosting activity s oncreate( ) method is executed If fragment is added to already running Activity then onattach( ), oncreate( ), oncreateview(), onactivitycreated( ), onstart( ) and then onresume( ) called

The Mobile Camera Interesting application

Mobile App: Word Lens Translates signs in foreign Language Google bought company. Now integrated into Google Translate [ Video ]

Camera:Taking Pictures

Taking Pictures with Camera Ref: https://developer.android.com/training/camera/photobasics.html How to take photos from your app using existing Android Camera app Steps: 1. Request Camera Permission 2. Take a Photo with the Camera App 3. Get the Thumbnail 4. Save the Full-size Photo

Request Permission to Use SmartPhone Camera If your app takes pictures using Android Camera, on Google Play, only devices with a camera will see your app on the Google Play Store E.g. This app requires a smartphone camera Make the following declaration in AndroidManifest.xml

Take a Photo with the Camera App To take picture, your app needs to send Intent to Android s Camera app, (i.e. action is capture an image) Potentially, multiple apps/activities can handle take a picture Check that at least 1 Activity that can handle request to take picture using resolveactivity Call startactivityforresult( ) with Camera intent since picture sent back startactivityforresult Your App Android Camera app onactivityresult Big picture: taking a picture

Code to Take a Photo with the Camera App 1. Build Intent describing taking a picture 3. Send Intent requesting taking a picture (usually handled by Android s Camera app) startactivityforresult 2. Check that there s at least 1 Activity that can handle request to take picture Your App Android Camera app onactivityresult

Get the Thumbnail Android Camera app returns thumbnail of photo (small bitmap) Thumbnail returned in extra of Intent delivered to onactivityresult( ) startactivityforresult Your App Android Camera app In onactivityresult( ), receive thumbnail picture sent back onactivityresult

Save Full-Sized Photo Ref: https://developer.android.com/training/basics/data-storage/files.html We need phone owner s permission to write to external storage Android systems have: Internal storage: data stored here is available by only your app External storage: available stored here is available to all apps Would like all apps to read pictures this app takes, so use external storage In AndroidManifest.xml, make the following declaration

Save Full-Sized Photo Ref: https://developer.android.com/training/basics/data-storage/files.html Android Camera app can save full-size photo to 1. Public external storage (shared by all apps) getexternalstoragepublicdirectory( ) Need to get permission 2. Private storage (Seen by only your app, deleted when your app uninstalls): getexternalfilesdir( ) Either way, need phone owner s permission to write to external storage In AndroidManifest.xml, make the following declaration

Taking Pictures: Bigger Example

Taking Pictures with Intents Ref: Ch 16 Android Nerd Ranch 2 nd edition Would like to take picture of Crime to document it Use implicit intent to start Camera app from our CrimeIntent app Recall: Implicit intent used to call component in different activity Click here to take picture Launches Camera app

Create Placeholder for Picture Modify layout to include ImageView for picture Button to take picture

Create Layout for Thumbnail and Button First, build out left side

Create Camera and Title Build out right side

Include Camera and Title in Layout Include in previously created top part Create, add in bottom part Camera and Title The rest of the layout

Get Handle of Camera Button and ImageView To respond to Camera Button click, in camera fragment, need handles to Camera button ImageView

Firing Camera Intent Create new intent for image capture Check with PackageManager that a Camera exists on this phone Build Intent to capture image, store at uri location Take picture when button is clicked

Declaring Features Declaring uses-features.. But android:required=false means app prefers to use this feature Phones without a camera will still see and can download this app

References Google Camera Taking Photos Simply Tutorials, http://developer.android.com/training/camera/phot obasics.html Busy Coder s guide to Android version 4.4 CS 65/165 slides, Dartmouth College, Spring 2014 CS 371M slides, U of Texas Austin, Spring 2014