Mobile and Ubiquitous Computing: Android Programming (part 3)

Similar documents
Mobile Application Development Android

Views & View Events View Groups, AdapterViews & Layouts Menus & ActionBar Dialogs

05. RecyclerView and Styles

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

ANDROID APPS DEVELOPMENT FOR MOBILE GAME

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

Android Programs Day 5

Android Basics. Android UI Architecture. Android UI 1

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

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

Building User Interface for Android Mobile Applications II

Android permissions Defining and using permissions Component permissions and related APIs

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

Programming Android UI. J. Serrat Software Design December 2017

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

EMBEDDED SYSTEMS PROGRAMMING Application Basics

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

Action Bar. Action bar: Top navigation bar at each screen The action bar is split into four different functional areas that apply to most apps.

EMBEDDED SYSTEMS PROGRAMMING Android Services

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

Android Application Model I

Action Bar. (c) 2010 Haim Michael. All Rights Reserv ed.

UI, Continued. CS 2046 Mobile Application Development Fall Jeff Davidson CS 2046

Diving into Android. By Jeroen Tietema. Jeroen Tietema,

Android UI Development

Mobile User Interfaces

Android User Interface Android Smartphone Programming. Outline University of Freiburg

Android Programming (5 Days)

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

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

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

Android Programming - Jelly Bean

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

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

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

MC Android Programming

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

CS371m - Mobile Computing. User Interface Basics

Android User Interface

06. Advanced Widget. DKU-MUST Mobile ICT Education Center

Mobile Programming Lecture 7. Dialogs, Menus, and SharedPreferences

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

ANDROID USER INTERFACE

Required Core Java for Android application development

Stanislav Rost CSAIL, MIT

Android Application Development

Produced by. Mobile Application Development. Eamonn de Leastar

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

Meniu. Create a project:

1. Location Services. 1.1 GPS Location. 1. Create the Android application with the following attributes. Application Name: MyLocation

INTRODUCTION TO ANDROID

Beginning Android 4 Application Development

CS378 -Mobile Computing. User Interface Basics

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

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

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

TextView. A label is called a TextView. TextViews are typically used to display a caption TextViews are not editable, therefore they take no input

LECTURE 08 UI AND EVENT HANDLING

When programming in groups of people, it s essential to version the code. One of the most popular versioning tools is git. Some benefits of git are:

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

ORACLE UNIVERSITY AUTHORISED EDUCATION PARTNER (WDP)

Open Lecture Mobile Programming. Intro to Material Design

User Interface Design & Development

EECS 4443 Mobile User Interfaces. More About Layouts. Scott MacKenzie. York University. Overview (Review)

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

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

GUI Widget. Lecture6

Java Training Center - Android Application Development

Android Programming Lecture 2 9/7/2011

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

Android Development Crash Course

Android Development Community. Let s do Material Design

Programming with Android: Introduction. Layouts. Dipartimento di Informatica: Scienza e Ingegneria Università di Bologna

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

EECS 4443 Mobile User Interfaces. More About Layouts. Scott MacKenzie. York University

MVC Apps Basic Widget Lifecycle Logging Debugging Dialogs

Mobile Programming Lecture 1. Getting Started

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

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

android-espresso #androidespresso

Our First Android Application

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

Fragments and the Maps API

Android. Broadcasts Services Notifications

Android Navigation Drawer for Sliding Menu / Sidebar

Android Fundamentals - Part 1

Praktikum Entwicklung Mediensysteme. Implementing a User Interface

INTRODUCTION COS MOBILE DEVELOPMENT WHAT IS ANDROID CORE OS. 6-Android Basics.key - February 21, Linux-based.

Mobile Software Development for Android - I397

Intents. Your first app assignment

Android Basics. - Bhaumik Shukla Android Application STEALTH FLASH

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

Presented by: Megan Bishop & Courtney Valentine

Android Specifics. Jonathan Diehl (Informatik 10) Hendrik Thüs (Informatik 9)

CS 4330/5390: Mobile Application Development Exam 1

Introduction To Android

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

Mobile Software Development for Android - I397

Introduction to Android

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

Transcription:

Mobile and Ubiquitous Computing: Android Programming (part 3) Master studies, Winter 2015/2016 Dr Veljko Pejović Veljko.Pejovic@fri.uni-lj.si Based on Programming Handheld Systems, Adam Porter, University of Maryland Examples from: Smartphone Programming Andrew Campbell, Dartmouth College

Android Security Applications are sandboxed Each app has its user ID and group ID that is unique for the device Allocated resources for the user ID Only the most basic functionalities are available to an application, other functionalities have to be explicitly asked for

Permissions An app requests permissions to access: User data (e.g. contacts) Some cost-sensitive APIs (e.g. send SMS) Some system resources (e.g. camera) Permissions are declared in AndroidManifest.xml Predefined Strings from Manifest.permission <user-permission> in Manifest indicates a request: <uses-permission android:name= android.permission.access_fine_location />

Permissions Types Normal permissions FLASHLIGHT, VIBRATE, BLUETOOTH, etc. http://developer.android.com/guide/topics/security/normal-permissions.html Dangerous permissions READ_CONTACTS, SEND_SMS, ACCESS_COARSE_LOCATION, WRITE_EXTERNAL_CONTACTS, etc. The OS will explicitly ask the user to grant access for dangerous permissions

Handling Permissions Prior to Marshmallow (API 23): Users must accept permissions before the app starts Marshmallow (API 23): Permissions can also be granted at the runtime, when the functionality is about to be used Ask straight away, educate up front, ask in context, educate in context See a short explanation by Google devs: www.youtube.com/watch?v=izqddvhtzj0

Custom Permissions Define your own permission if the app performs a privileged/dangerous function, and you don t want just any app to be able to launch yours In AndroidManifest.XML under <permission> Example: <permission android:name="com.testpackage.mypermission" android:label="my_permission android:protectionlevel="dangerous /> Any app that wants to launch yours must request com.testpackage.mypermission

Component Permissions Permissions can be defined for individual components restricting which other component can call them Activities Which components can call startactivity() Services Which components can start or bind to a Service BroadcastReceivers Which components can receive and send broadcasts ContentProviders Which components can read and write content provider data

Permissions Design Do not request permissions unless you really need them E.g. do you need to write data to an external storage (file) or can you keep the information in SharedPrefs? Show immediate benefit of granting a permission E.g. your ToDo list app requires location info show the user how she can make location-based reminders Use Intents to call other apps in case you don t need to handle the functionality within your app: E.g. call a camera app, rather than requesting the camera permission for your app

Google Map in Android Provided by Google Play Services Add to Android Studio via SDK Manager Needs maps API key Obtain your apps short signature (SHA-1) Create an API project in the Google API console Generate the maps API key Put it in your app s AndroidManifest.xml <meta-data android:name="com.google.android.geo.api_key" android:value=your_key/>

Google Map in Android Android Studio automates this via New->Google Maps Activity Google maps require permissions <uses-permission android:name= "android.permission.internet" /> <uses-permission android:name= "android.permission.access_network_state" /> <uses-permission android:name= "android.permission.write_external_storage" /> <uses-permission android:name= "com.google.android.providers.gsf.permission.read_gservices" />

Google Map in Android Map is basically a Fragment com.google.android.gms.maps.supportmapfragment Note: you need Internet connectivity to show it Operations: Set map type (satellite, normal, hybrid, terrain) Capture onclick events Add markers, capture onmarkerclick events

Google Map in Android You can do much more: Draw on the map Different layers Get address Show user s location Get directions Show 3D view (in selected areas) Try at home: http://android-er.blogspot.si/2013/01/googlemaps-android-api-v2-example_5213.html

Services Activities run on the main thread UI thread Processing-heavy functions on the main thread impact the responsiveness Services run on a separate background thread Outside UI, for long-running operations Services can be created: Explicitly using Context.startService() Implicitly, if not already running, when a client requests connection to a Service via Context.bindService()

Multiple startservice calls do not nest you only have one service; however, onstartcommand() will be called repeatedly Service will be stopped only once with Context.stopService() or stopself() Services

Services Bound Services started through binding do not call onstartcommand() Return IBinder object from onbind(intent) so that connected clients can call the Service The service remains running as long as the connection is established

BroadcastReceiver Receive events announced by other components Events announced via Intents Not the same Intent as the one starting an Activity: this one remains in the background Events can be announced within your app or publicly to every app on the phone Announce via sendbroadcast() Events captured if the receiver is registered: onreceiverregistered() and then onreceive()

Notifications Allows the app to initiate contact with the user Shown in the Status Bar (Notification Bar) Custom icons Alerts: sound, vibration, LED flashing Notification drawer NotificationManager

Service, Notification, BroadcastReceiver Example

Android UI UI is usually provided via Activities and Fragments setcontentview(view v) View System from the Application Framework

View View is the main class on which the View System operates Responsible for drawing itself and handling events From View we derive other UI objects: Button, EditText fields, MapView, RadioButton, WebView, LinearLayout, etc. View and its derived classes can be instantiated: through the XML code in Resources through Java source code

View Example Button: In an XML layout: <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/reg_button_text" android:id="@+id/register_button" android:layout_gravity="center_horizontal /> The benefit of using XML separate functionality from presentation (think MVC)

View Example Button: In Activity through Java code : final Button button = (Button) findviewbyid(r.id.register_button); button.setonclicklistener(new View.OnClickListener() { @Override public void onclick(view view) { // Do something, i.e. register a user } }); Yet, accessing through Java brings a lot of flexibility

Other View Examples TextView EditText CheckBox SeekBar Switch CalendarView

Other View Examples AdapterViews Separation between the data (model) and children views Adapter manages the data and provides it to the views Example: ListView Displays a scrollable list of items, where the list is populated through an Adapter ArrayAdapter is the most common, but you can implement your own Adapter as well Convenience classes: ListActivity and ListFragment

View Properties View parameters Text, padding, layout, colour, etc. Layout: WRAP_CONTENT vs MATCH_PARENT android:layout_width="wrap_content android:layout_height="wrap_content android:text="@string/reg_button_text android:id="@+id/register_button android:layout_gravity="center_horizontal Also accessible through Java: change visibility, modify text, get checked state, etc.

View Events Stem from user interaction and lifecycle changes Listeners used for handling View events OnClickListener.onClick() capture View clicked event OnLongClickListener.onLongClick() capture View clicked and held for some time OnTouchListener.onTouch() capture View touched And quite a few more Capturing gestures: GestureDetector.OnGestureListener

View Events Example Capture onclick event private OnClickListener mlistener = new OnClickListener() { public void onclick(view v) { // do something when the button is clicked } }; protected void oncreate(bundle savedvalues) {... // Capture our button from layout Button button = (Button)findViewById(R.id.button); // Register the onclick listener with the implementation above button.setonclicklistener(mlistener);... }

Displaying Views Views are organised in a tree with the root View containing all the others UI Hierarchy Viewer in Android Studio allows you to examine the hierarchy of your views Displaying Views includes Measuring each of the elements (calling onmeasure()) Aligning the children (calling onlayout()) Rendering the view (ondraw())

Grouping Views ViewGroup An invisible View that contains other Views Used for grouping and organizing views Example: RadioGroup, TimePicker, Spinner Layout A ViewGroup that defines a structure for the Views it contains

Layouts LinearLayout Child views arranged in a single horizontal or vertical row RelativeLayout Child views are positioned relative to each other and to parent view TableLayout Child views arranged into rows and columns But also other layouts: FrameLayout, GridLayout, TabLayout, etc.

Menus Formalised in Android to provide consistent user experience across applications Activities support menus: Add items to a menu Handle onclick events for the menu Menu types: Options menu Context menu Popup menu

Options Menu Primary menu where actions such as compose new email, settings and similar are added Define in an XML file, put in res/menu <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/new_game" android:icon="@drawable/ic_new_game" android:title="@string/new_game" android:showasaction="ifroom"/> <item android:id="@+id/help" android:icon="@drawable/ic_help" android:title="@string/help" /> </menu>

Options Menu Inflate in Activity- override oncreateoptionsmenu @Override public boolean oncreateoptionsmenu(menu menu) { MenuInflater inflater = getmenuinflater(); inflater.inflate(r.menu.game_menu, menu); return true; } Handle onclick events Note: Fragments have their own oncreateoptionsmenu @Override public boolean onoptionsitemselected(menuitem item) { switch (item.getitemid()) { case R.id.new_game: newgame(); return true; case R.id.help: showhelp(); return true;

Context Menu Options that affect the selected item in the context of the current UI frame Floating context menu Contextual action mode

Context Menu Define your menu in res/menu as an XML file Register a View for a context menu Button btn = (Button) findviewbyid(r.id.btn); registerforcontextmenu(btn); Inflate context menu @Override public void oncreatecontextmenu(contextmenu menu, View v, ContextMenuInfo menuinfo) { super.oncreatecontextmenu(menu, v, menuinfo); MenuInflater inflater = getmenuinflater(); inflater.inflate(r.menu.context_menu, menu);} Capture on item selected events public boolean oncontextitemselected(menuitem item)

Toolbar Allows quick access to frequently used operations Title, logo, navigation, action menu Similar to the application bar in desktop apps Toolbar - Introduced in API 21 (Lollipop) to replace ActionBar introduced in API 11 Toolbar is more general than ActionBar (which is closely tied to an Activity), as it can be placed anywhere within the view hierarchy

Use Toolbar as ActionBar dependencies { compile filetree(dir: 'libs', include: ['*.jar']) compile "com.android.support:appcompat-v7:21.0.+" } Gradle <android.support.v7.widget.toolbar android:id="@+id/toolbar android:layout_width="match_parent android:layout_height="wrap_content"> </android.support.v7.widget.toolbar> import android.support.v7.app.appcompatactivity; import android.support.v7.widget.toolbar; Layout XML Activity Java public class MyActivity extends AppCompatActivity{ @Override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_my); Toolbar toolbar = (Toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); } }