Android Program Development: (16) Other views -- 16.2 WebView

Source: Internet
Author: User

WebView allows you to embed a web browser in the activity. This is useful if your application needs to embed some web content.
 
The following describes how to encode content in a webpage in an activity.
 
1. Create a new project, WebView.
 
2. Code in main. xml.
 
[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">
 
<WebView android: id = "@ + id/webview1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/>
 
</LinearLayout>
3. Code in WebViewActivity. java.
[Java]
Public class WebViewActivity extends Activity {
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );

WebView wv = (WebView) findViewById (R. id. webview1 );

WebSettings webSettings = wv. getSettings ();
WebSettings. setBuiltInZoomControls (true );

Wv. loadUrl (
Http://chart.apis.google.com/chart +
"? Chs = 300x225 "+
"& Cht = v" +
"& Chco = FF6342, ADDE63, 63C6DE" +
"& Chd = t:, 80, 60, 30, 30, 10" +
"& Chdl = A | B | C ");
 
}
4. Add the network access permission in AndroidManifest. xml.
[Html]
<Uses-permission android: name = "android. permission. INTERNET"/>
5. Press F11 to debug the simulator.
 
 
 
 
To use WebView to load a webpage, you must use the loadUrl () method and input URL parameters, as shown in the following figure:
 
[Java]
Wv. loadUrl (
Http://chart.apis.google.com/chart +
"? Chs = 300x225 "+
"& Cht = v" +
"& Chco = FF6342, ADDE63, 63C6DE" +
"& Chd = t:, 80, 60, 30, 30, 10" +
"& Chdl = A | B | C ");
To display built-in zoom-in or zoom-out controls, you need to obtain the WebSettings property object from WebView and then call the setBuildInZoomControls () method:
[Java]
WebSettings webSettings = wv. getSettings ();
WebSettings. setBuiltInZoomControls (true );
 
 
 
Sometimes, when you load a webpage, the webpage will point to www.google.com, and WebView will call the Browser of the device to load the website. Note: The URL bar at the top of the screen has already called the local Browser application.
 
 
 
To prevent calling the system's own browser, you must implement the WebViewClient class, and then rewrite the shouldOverrideUrlLoading () method. The following is an example:
 
[Java]
Public class WebViewActivity extends Activity {
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );

WebView wv = (WebView) findViewById (R. id. webview1 );

WebSettings webSettings = wv. getSettings ();
WebSettings. setBuiltInZoomControls (true );
 
Wv. setWebViewClient (new Callback ());
Wv. loadUrl ("http://www.wrox.com ");
 
}

Private class Callback extends WebViewClient {
@ Override
Public boolean shouldOverrideUrlLoading (WebView view, String url ){
Return (false );
}
}
 
}
 
This time, the specified URL is directly loaded in WebView.
 
 
 
In addition, you can dynamically implement an HTML string and load it into WebView. This requires the loadDataWithBaseURL () method:
 
[Java]
WebView wv = (WebView) findViewById (R. id. webview1 );
Final String mimeType = "text/html ";
Final String encoding = "UTF-8 ";
String html = "<H1> A simple HTML page </H1> <body>" +
"<P> The quick brown fox jumps over the lazy dog </p> </body> ";
Wv. loadDataWithBaseURL ("", html, mimeType, encoding ,"");
 
 
 
Similarly, if there is an HTML file under the assets folder, you can use the loadUrl () method to load it into WebView:
 
[Java]
WebView wv = (WebView) findViewById (R. id. webview1 );
Wv. loadUrl ("file: // android_asset/Index.html ");
 
WebView after loading the html file:
 
 
 
 
 
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.