Designing Apps Using The WebView Control

Slides:



Advertisements
Similar presentations
Android Application Development Tutorial. Topics Lecture 4 Overview Overview of Sensors Programming Tutorial 1: Tracking location with GPS and Google.
Advertisements

Android architecture overview
The Web Warrior Guide to Web Design Technologies
Android Fonts Notes are based on: The Busy Coder's Guide to Android Development by Mark L. Murphy Copyright © CommonsWare, LLC. ISBN:
User Interface Android Applications. Activities An activity presents a visual user interface. Each activity is given a default window to draw in. The.
Android 101 Application Fundamentals January 29, 2010.
Android Application Development with Java UPenn CS4HS 2011 Chris Murphy
@2011 Mihail L. Sichitiu1 Android Introduction Hello World.
Android Application Development Tutorial. Topics Lecture 5 Overview Overview of Networking Programming Tutorial 2: Downloading from the Internet.
Presented by…. Group 2 1. Programming language 2Introduction.
Android Sensors & Async Callbacks Jules White Bradley Dept. of Electrical and Computer Engineering Virginia Tech
Android Boot Camp for Developers Using Java, Comprehensive: A Guide to Creating Your First Android Apps Chapter 9: Customize! Navigating with a Master/Detail.
CS378 - Mobile Computing Web - WebView and Web Services.
Component-Based Software Engineering Internet Applications Paul Krause.
Mobile Programming Lecture 9 Bound Service, Location, Sensors, IntentFilter.
1 Mobile Computing Monetizing An App Copyright 2014 by Janson Industries.
Mobile Programming Lecture 6
DUE Hello World on the Android Platform.
1 Announcements Homework #2 due Feb 7 at 1:30pm Submit the entire Eclipse project in Blackboard Please fill out the when2meets when your Project Manager.
Android Accessing GPS Ken Nguyen Clayton State University 2012.
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
Android Boot Camp for Developers Using Java, Comprehensive: A Guide to Creating Your First Android Apps Chapter 2: Simplify! The Android User Interface.
Networking: Part 1 (Web Content). Networking with Android Android provides A full-featured web browser based on Chromium, the open source browser engine.
Maps Dr. David Janzen Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution 2.5 License.
Webview and Web services. Web Apps You can make your web content available to users in two ways in a traditional web browser in an Android application,
Android Using Menus Notes are based on: The Busy Coder's Guide to Android Development by Mark L. Murphy Copyright © CommonsWare, LLC. ISBN:
INNOV-7: Building a Richer UI for the Browser Chris Skeldon Senior Solution Consultant.
Sensors – Part 2 SE 395/595. Location in Android LocationManager class – Configure providers and their listeners LocationListener class – Handles update.
Chapter 11 Adding Media and Interactivity. Chapter 11 Lessons Introduction 1.Add and modify Flash objects 2.Add rollover images 3.Add behaviors 4.Add.
Understanding JavaScript and Coding Essentials Lesson 8.
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Android Boot Camp.
Internet & World Wide Web How to Program, 5/e.  JavaScript events  allow scripts to respond to user interactions and modify the page accordingly  Events.
Android 基本 I/O. 基本 I/O 介面元件 在此節中主要介紹常見的 I/O 使用者介 面元件 – Button, TextView, 以及 EditText , 學習者可以學會: – Android 的視窗表單設計 res/layout/main.xml – Android SDK –
1. 2 Android location services Determining a device’s current location Tracking device movements Proximity alerts.
Android The WebKit Browser Victor Matos Cleveland State University Notes are based on: Android Developers Google.
CS499 – Mobile Application Development Fall 2013 Location & Maps.
Android Programming.
Build in Objects In JavaScript, almost "everything" is an object.
Lab7 – Advanced.
Android Programming - Features
Cleveland State University
Lecture 3 Zablon Ochomo Android Layouts Lecture 3 Zablon Ochomo
Android Introduction Hello World
JavaScript and Ajax (Ajax Tutorial)
Using DHTML to Enhance Web Pages
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
Android Introduction Hello World.
Android External Resources
Android Widgets 1 7 August 2018
Sensors, maps and fragments:
4. Javascript Pemrograman Web I Program Studi Teknik Informatika
תכנות ב android אליהו חלסצ'י.
CIS 470 Mobile App Development
CS323 Android Model-View-Controller Architecture
CIS 470 Mobile App Development
CIS 470 Mobile App Development
DHTML Javascript Internet Technology.
CIS 470 Mobile App Development
DHTML Javascript Internet Technology.
© 2015, Mike Murach & Associates, Inc.
BMI Android Application will take weight and height from the users to calculate Body Mass Index (BMI) with the information, whether user is underweight,
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CIS 470 Mobile App Development
Android Notifications
Activities, Fragments, and Intents
AJAX By Prof. B.A.Khivsara
Android Sensor Programming
CIS 694/EEC 693 Android Sensor Programming
Software Engineering for Internet Applications
Presentation transcript:

Designing Apps Using The WebView Control 28 Designing Apps Using The WebView Control Victor Matos Cleveland State University Notes are based on: Android Developers http://developer.android.com/index.html The Busy Coder's Guide to Advanced Android Development by Mark L. Murphy. Ed. CommonsWare.

28. Android - WebView WebView An interesting class of Android applications could be created by combining JavaScripted HTML pages and the WebView control. The WebView widget is typically used by the WebKit browser engine to display pages accessed from the Internet. However, you could also display local HTML pages on a WebView. The Android application interacts with the WebView through user created objects which are passed back and forth between the WebView and the Android Activities.   2 2 2

WebView Architecture: HTML & Javascript pages + Android Activities 28. Android - WebView WebView Javascript uses custom object Architecture: HTML & Javascript pages + Android Activities   UI designed using HTML WebView 3 3 3

28. Android - WebView WebView What is New? The addJavascriptInterface() method on WebView allows you to pass a Java object from Android activities to the WebView, exposing its methods. The various getters/setters methods defined in the object allow data exchange between the HTML-UI and Android activities. In addition Javascript events (clicking buttons, making selections, filling boxes, etc) could be used to react to the user requests and correspondingly pass data from the UI to the Android classes.   4 4 4 4

WebView Example: How is the HTML-Android exchange done? 28. Android - WebView WebView Example: How is the HTML-Android exchange done? In this example the current coordinates (latitude, longitude) of the device will be displayed on the screen. Assume: The application’s UI consists of a WebView called “browser”, also in the Assets folder the developer has introduced a Javascripted web-page called geoweb2.html. The Android application has defined a custom object called “locater” with get/set methods exposing the [lat, lon] values.   Cont. 5 5 5

WebView Example: How is the HTML-Android exchange done? (continuation) 28. Android - WebView WebView Example: How is the HTML-Android exchange done? (continuation) The following statements are held in the Android main activity browser.getSettings().setJavaScriptEnabled(true); browser.addJavascriptInterface(new Locater(), "locater"); browser.loadUrl("file:///android_asset/geoweb2.html"); The first allows the use of Javascript on the WebView The second statement passes the object type and name. The last stat. loads the local HTML page on the WebView.   Cont. 6 6 6

WebView Example: How does the HTML page uses the object ? 28. Android - WebView WebView Example: How does the HTML page uses the object ? (continuation) The HTML page could manage the “locater” object through its accessors such as in the following lines: document.getElementById("lat").innerHTML=locater.getLatitude(); locater.setAddress ( document.getElementById(“address").innerHTML ); Locater.doSomething(); Where “lat” (and “address”) are HTML placeholders defined using <span id="lat"> (unknown) </span>   Cont. 7 7 7

document.getElementById("lat").innerHTML 28. Android - WebView WebView Example: How does the HTML page uses the object ? (continuation) Consider the JavaScript expression:   document.getElementById("lat").innerHTML The innerHTML property is used along with getElementById within your JavaScript code to refer to an HTML element and change its contents. innerHTML allows you to change the page's content without refreshing the page (this makes your website feel quicker and more responsive to user input). The innerHTML property is not actually part of the official DOM specification, despite this, it is supported in all major browsers including Android’s WebKit. Cont. 8 8 8

WebView Complete Example: Get Location – Show on a Local WebView 28. Android - WebView WebView Complete Example: Get Location – Show on a Local WebView   main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <WebView android:id="@+id/browser" /> </LinearLayout> Add to the Manifest <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 9 9 9 Cont.

WebView Complete Example: Get Location – Show on a Local WebView 28. Android - WebView WebView Complete Example: Get Location – Show on a Local WebView   assets: geoweb2.html <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <title>Android GeoWebOne Demo</title> <script language="javascript"> function whereami() { document.getElementById("lat").innerHTML=locater.getLatitude(); document.getElementById("lon").innerHTML=locater.getLongitude(); var spy = "Spy data coming from HTML\n" + document.getElementById("lat").innerHTML + "\n" + document.getElementById("lon").innerHTML; locater.setValue(spy); } function whereami2() { locater.htmlPassing2Android(spy); </script> </head> <body> <p>You are at</p> <table border="1" cellspacing="1" cellpadding="1"> <tr> <td bgcolor="#FFFFCC">Latitude</td> <td><span id="lat">(unknown)</span></td> </tr> <td bgcolor="#FFFFCC">Longitude</td> <td><span id="lon">(unknown)</span></td> </table> <p><a onClick="whereami()"><u>Click to Update Location</u></a></p> <p> <input type="button" onclick= "whereami2()" value="Spying"> </body> </html> Cont. 10 10 10

WebView Complete Example: Get Location – Show on a Local WebView Cont. 28. Android - WebView WebView Complete Example: Get Location – Show on a Local WebView   package ucr.geowebtwo; // code based on M. Murphy - CommonsWare, V. Matos import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.webkit.WebView; import android.widget.Toast; public class GeoWebTwo extends Activity { private String PROVIDER = "gps"; private WebView browser; private LocationManager myLocationManager = null; Locater locater = new Locater(); @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); browser = (WebView) findViewById(R.id.browser); // request GPS location services myLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); // enable JavaScript, pass user's object, load page browser.getSettings().setJavaScriptEnabled(true); browser.addJavascriptInterface(locater, "locater"); browser.loadUrl("file:///android_asset/geoweb2.html"); } Cont. 11 11 11

WebView Complete Example: Get Location – Show on a Local WebView Cont. 28. Android - WebView WebView Complete Example: Get Location – Show on a Local WebView   @Override public void onResume() { super.onResume(); myLocationManager.requestLocationUpdates( PROVIDER, 3000, 10,onLocationChange); } public void onPause() { super.onPause(); myLocationManager.removeUpdates(onLocationChange); // ---------------------------------------------------------------------- LocationListener onLocationChange = new LocationListener() { // passing the actual values of lat & lon. Waiting for the function // whereami(...) to drop the arguments into HTML placeholders public void onLocationChanged(Location location) { StringBuilder buf = new StringBuilder("javascript:whereami("); buf.append(String.valueOf(location.getLatitude())); buf.append(","); buf.append(String.valueOf(location.getLongitude())); buf.append(")"); browser.loadUrl(buf.toString()); public void onProviderDisabled(String provider) { // required for interface, not used Cont. 12 12 12

WebView Complete Example: Get Location – Show on a Local WebView Cont. 28. Android - WebView WebView Complete Example: Get Location – Show on a Local WebView   public void onProviderEnabled(String provider) { // required for interface, not used } public void onStatusChanged(String provider, int status, Bundle extras) { }; Cont. 13 13 13

WebView Complete Example: Get Location – Show on a Local WebView Last 28. Android - WebView WebView Complete Example: Get Location – Show on a Local WebView   // /////////////////////////////////////////////////////////////// public class Locater { public String spy = ""; public double getLatitude() { Location loc = myLocationManager.getLastKnownLocation(PROVIDER); if (loc == null) { return (0); } return (loc.getLatitude()); public double getLongitude() { return (loc.getLongitude()); public void htmlPassing2Android(String dataFromHtml) { // changes to the HTML place-holders lat & lon can be seen here. // There is an HTML button that when clicked calls this Android method. spy = dataFromHtml; Toast.makeText(getApplicationContext(), spy, 1).show(); } // Locater } // GeoWebTwo Last 14 14 14

28. Android - WebView WebView Questions Zipped code: 15 15