0% found this document useful (0 votes)
3 views23 pages

Web View Layout

The document provides an overview of using WebView in Android for embedding web content within applications. It covers the functionalities of WebView, including loading URLs and HTML content, as well as the necessary permissions and handling redirects. Additionally, it includes examples and best practices for implementing WebView in Android applications.

Uploaded by

hlama7681
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views23 pages

Web View Layout

The document provides an overview of using WebView in Android for embedding web content within applications. It covers the functionalities of WebView, including loading URLs and HTML content, as well as the necessary permissions and handling redirects. Additionally, it includes examples and best practices for implementing WebView in Android applications.

Uploaded by

hlama7681
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Kenya institute of software engineering

Mobile Development II

Web view Layout

URL: www.KenyaInstituteofsoftwareengineering.com Lecturer: Mr. John Kinyanjui


Tel: +254 202529389, +254 713 810 752, +254 732 609 809 email: [email protected]
WebView
 The WebView enables you to embed a
web browser in your activity.
 This is very useful if your application
needs to embed some web content, such
as maps from some other providers, and
so on
 There are a couple of ways to display
HTML in Android, with the most powerful
being the WebView widget

URL: www.KenyaInstituteofsoftwareengineering.com Lecturer: Mr. John Kinyanjui


Tel: +254 202529389, +254 713 810 752, +254 732 609 809 email: [email protected]
WebView
 WebView can also assist you with
common “browsing” metaphors, such as
history list of visited URLs to support
backwards and forwards navigation.
 On the other hand, WebView is a much
more expensive widget to use, in terms
of memory consumption

URL: www.KenyaInstituteofsoftwareengineering.com Lecturer: Mr. John Kinyanjui


Tel: +254 202529389, +254 713 810 752, +254 732 609 809 email: [email protected]
WebKit
 WebKit is an open source Web rendering
engine that forms the heart of major
Web browsers, such as Chrome and
Safari.
 While the version of WebKit that lives in
Android is one optimized for mobile use,
it still represents a fairly substantial
code base, and rendering complex Web
pages takes up a fair amount of RAM

URL: www.KenyaInstituteofsoftwareengineering.com Lecturer: Mr. John Kinyanjui


Tel: +254 202529389, +254 713 810 752, +254 732 609 809 email: [email protected]
Adding the Widget
 For simple stuff, WebView is not
significantly different than any other
widget in Android
 Pop it into a layout, tell it what URL to
navigate to via Java code, and you are
done.

URL: www.KenyaInstituteofsoftwareengineering.com Lecturer: Mr. John Kinyanjui


Tel: +254 202529389, +254 713 810 752, +254 732 609 809 email: [email protected]
Adding the widget
 As with any other widget, you need to
tell it how it should fill up the space in
the layout (in this case, it fills all
remaining space).
 And, just as with other widgets, you can
drag a WebView out of the “Composite”
section of the Eclipse tool palette and
into a layout XML resource in the
Graphical Layout editor:

URL: www.KenyaInstituteofsoftwareengineering.com Lecturer: Mr. John Kinyanjui


Tel: +254 202529389, +254 713 810 752, +254 732 609 809 email: [email protected]
URL: www.KenyaInstituteofsoftwareengineering.com Lecturer: Mr. John Kinyanjui
Tel: +254 202529389, +254 713 810 752, +254 732 609 809 email: [email protected]
Example:
<WebView
android:id=”@+id/webview1”
android:layout_width=”wrap_content

android:layout_height=”wrap_content
” />

URL: www.KenyaInstituteofsoftwareengineering.com Lecturer: Mr. John Kinyanjui


Tel: +254 202529389, +254 713 810 752, +254 732 609 809 email: [email protected]
Loading Content Via a URL
 There are a number of ways to load
HTML content into a WebView widget.
 The simplest is to use the loadUrl()
method, which takes a URL and
retrieves its contents over the Internet.

URL: www.KenyaInstituteofsoftwareengineering.com Lecturer: Mr. John Kinyanjui


Tel: +254 202529389, +254 713 810 752, +254 732 609 809 email: [email protected]
Example:
 browser=(WebView)findViewById(R.id.w
ebview1);
 browser.loadUrl("http://www.google.com
");

URL: www.KenyaInstituteofsoftwareengineering.com Lecturer: Mr. John Kinyanjui


Tel: +254 202529389, +254 713 810 752, +254 732 609 809 email: [email protected]
Permission
 However, we also have to make one
change to AndroidManifest.xml, adding a
line where we request permission to
access the Internet:
◼ <uses-permission
android:name="android.permission.
INTERNET"/>
 If we fail to add this permission, the
browser will refuse to load pages.

URL: www.KenyaInstituteofsoftwareengineering.com Lecturer: Mr. John Kinyanjui


Tel: +254 202529389, +254 713 810 752, +254 732 609 809 email: [email protected]
Web browser
 As with a regular Android Web browser,
you can pan around the page by
dragging it, while the directional pad
moves you around all the focusable
elements on the page.
 What is missing is all the extra stuff that
make up a Web browser, such as a
navigational toolbar. WebView does not
provide any of that — if you want those
sorts of UI features, you will need to
implement those yourself
URL: www.KenyaInstituteofsoftwareengineering.com Lecturer: Mr. John Kinyanjui
Tel: +254 202529389, +254 713 810 752, +254 732 609 809 email: [email protected]
No internet!

URL: www.KenyaInstituteofsoftwareengineering.com 13
Lecturer: Mr. John Kinyanjui
Tel: +254 202529389, +254 713 810 752, +254 732 609 809 email: [email protected]
Alternatives for Loading Content
 You can also use loadData(). Here, you
supply the HTML for the WebView to
display. You might use this to:
◼ display a manual that was installed as a
file with your application package
◼ display snippets of HTML you retrieved
as part of other processing
◼ generate a whole user interface using
HTML, instead of using the Android
widget set

URL: www.KenyaInstituteofsoftwareengineering.com Lecturer: Mr. John Kinyanjui


Tel: +254 202529389, +254 713 810 752, +254 732 609 809 email: [email protected]
loadData()
 There are two flavors of loadData(). The
simpler one allows you to provide the
content, the MIME type, and the
encoding, all as strings.
 Typically, your MIME type will be
text/html and your encoding will be UTF-
8 for ordinary HTML.

URL: www.KenyaInstituteofsoftwareengineering.com Lecturer: Mr. John Kinyanjui


Tel: +254 202529389, +254 713 810 752, +254 732 609 809 email: [email protected]
For example
 If you replace the loadUrl() invocation in
the previous example with the following:
◼ browser.loadData("<html><body>H
ello, world!</body></html>",
"text/html", "UTF-8");
 You get:

URL: www.KenyaInstituteofsoftwareengineering.com 16
Lecturer: Mr. John Kinyanjui
Tel: +254 202529389, +254 713 810 752, +254 732 609 809 email: [email protected]
URL: www.KenyaInstituteofsoftwareengineering.com Lecturer: Mr. John Kinyanjui
Tel: +254 202529389, +254 713 810 752, +254 732 609 809 email: [email protected]
Alternatively
 If you have an HTML file located in the
assets folder of the project you can load
it into the WebView using the loadUrl()
method:
◼ WebView wv = (WebView)
findViewById(R.id.webview1);
◼ wv.loadUrl(“file:///android_asset/I
ndex.html”);

URL: www.KenyaInstituteofsoftwareengineering.com 18
Lecturer: Mr. John Kinyanjui
Tel: +254 202529389, +254 713 810 752, +254 732 609 809 email: [email protected]
URL: www.KenyaInstituteofsoftwareengineering.com 19
Lecturer: Mr. John Kinyanjui
Tel: +254 202529389, +254 713 810 752, +254 732 609 809 email: [email protected]
URL: www.KenyaInstituteofsoftwareengineering.com 20
Lecturer: Mr. John Kinyanjui
Tel: +254 202529389, +254 713 810 752, +254 732 609 809 email: [email protected]
Redirect
 Sometimes when you load a page that
redirects you, WebView will cause your
application to launch the device’s
Browser application to load the desired
page.
 To prevent this from happening, you
need to implement the WebViewClient
class and override the
shouldOverrideUrlLoading() method

URL: www.KenyaInstituteofsoftwareengineering.com Lecturer: Mr. John Kinyanjui


Tel: +254 202529389, +254 713 810 752, +254 732 609 809 email: [email protected]
Example:
 wv = (WebView)
findViewById(R.id.webView1);
 wv.setWebViewClient(new
WebViewClient());
 wv.loadUrl("https://google.com");

URL: www.KenyaInstituteofsoftwareengineering.com Lecturer: Mr. John Kinyanjui


Tel: +254 202529389, +254 713 810 752, +254 732 609 809 email: [email protected]
END

URL: www.KenyaInstituteofsoftwareengineering.com Lecturer: Mr. John Kinyanjui


Tel: +254 202529389, +254 713 810 752, +254 732 609 809 email: [email protected]

You might also like