Use of WebView components for Android Development

Source: Internet
Author: User

BKJIA once exclusively recommended a special topic for Android development and application explanation. This article hopes to show you how to use WebView components in detail:

Network Content

1. LoadUrl directly displays the webpage content (displays the network image separately)

2. LoadData displays Chinese webpage content (including space processing)

Files in the APK package

1. LoadUrl: display Html and image files in APK

2. LoadData (loadDataWithBaseURL) displays Html content mixed with images and text in the APK.

Res/layout/main. xml

Xml Code

 
 
  1. < ?xml version="1.0" encoding="utf-8"?> 
  2.  
  3. < LINEARLAYOUT android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"> 
  4.  
  5. < WEBVIEW android:layout_height="fill_parent" android:layout_width="fill_parent" android:id="@+id/webview" /> 
  6.  
  7. < /LINEARLAYOUT> 
  8.  
  9. < ?xml version="1.0" encoding="utf-8"?> 
  10.  
  11. < LINEARLAYOUT android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"> 
  12.  
  13. < WEBVIEW android:layout_height="fill_parent" android:layout_width="fill_parent" android:id="@+id/webview" /> 
  14.  
  15. < /LINEARLAYOUT> 
  16.  
  17. Example_webview.java 

Java code

 
 
  1. Package cn. coolworks;
  2.  
  3. Import java.net. URLEncoder;
  4.  
  5. Import android. app. Activity;
  6.  
  7. Import android. OS. Bundle;
  8.  
  9. Import android. webkit. WebView;
  10.  
  11. Public class Example_webview extends Activity {
  12.  
  13. WebView webView;
  14.  
  15. Final String mimeType = "text/html ";
  16.  
  17. Final String encoding = "UTF-8 ";
  18.  
  19. /** Called when the activity is first created .*/
  20.  
  21. @ Override
  22.  
  23. Public void onCreate (Bundle savedInstanceState ){
  24.  
  25. Super. onCreate (savedInstanceState );
  26.  
  27. SetContentView (R. layout. main );
  28.  
  29. WebView = (WebView) findViewById (R. id. webview );
  30.  
  31. WebView. getSettings (). setJavaScriptEnabled (true );
  32.  
  33. //
  34.  
  35. // WebHtml ();
  36.  
  37. //
  38.  
  39. // WebImage ();
  40.  
  41. //
  42.  
  43. // LocalHtmlZh ();
  44.  
  45. //
  46.  
  47. // LocalHtmlBlankSpace ();
  48.  
  49. //
  50.  
  51. // LocalHtml ();
  52.  
  53. //
  54.  
  55. // LocalImage ();
  56.  
  57. //
  58.  
  59. LocalHtmlImage ();
  60.  
  61. }
  62.  
  63. /**
  64.  
  65. * Direct webpage display
  66.  
  67. */
  68.  
  69. Private void webHtml (){
  70.  
  71. Try {
  72.  
  73. WebView. loadUrl ("http://www.google.com ");
  74.  
  75. } Catch (Exception ex ){
  76.  
  77. Ex. printStackTrace ();
  78.  
  79. }
  80.  
  81. }
  82.  
  83. /**
  84.  
  85. * Direct network image display
  86.  
  87. */
  88.  
  89. Private void webImage (){
  90.  
  91. Try {
  92.  
  93. WebView
  94.  
  95. . LoadUrl ("http://www.gstatic.com/codesite/ph/images/code_small.png ");
  96.  
  97. } Catch (Exception ex ){
  98.  
  99. Ex. printStackTrace ();
  100.  
  101. }
  102.  
  103. }
  104.  
  105. /**
  106.  
  107. * Chinese display
  108.  
  109. */
  110.  
  111. Private void localHtmlZh (){
  112.  
  113. Try {
  114.  
  115. String data = "test Html data containing Chinese characters ";
  116.  
  117. // UTF-8 encoding (garbled characters may occur on SDK1.5 simulators and real devices, and can be normally displayed on SDK1.6)
  118.  
  119. // WebView. loadData (data, mimeType, encoding );
  120.  
  121. // Encode the data (SDK1.5)
  122.  
  123. WebView. loadData (URLEncoder. encode (data, encoding), mimeType,
  124.  
  125. Encoding );
  126.  
  127. } Catch (Exception ex ){
  128.  
  129. Ex. printStackTrace ();
  130.  
  131. }
  132.  
  133. }
  134.  
  135. /**
  136.  
  137. * Chinese display (processing of spaces)
  138.  
  139. */
  140.  
  141. Private void localHtmlBlankSpace (){
  142.  
  143. Try {
  144.  
  145. String data = "testing Html data with spaces ";
  146.  
  147. // Do not process Spaces
  148.  
  149. WebView. loadData (URLEncoder. encode (data, encoding), mimeType,
  150.  
  151. Encoding );
  152.  
  153. // WebView. loadData (data, mimeType, encoding );
  154.  
  155. // Process spaces (in SDK1.5)
  156.  
  157. WebView. loadData (URLEncoder. encode (data, encoding). replaceAll (
  158.  
  159. "\ +", ""), MimeType, encoding );
  160.  
  161. } Catch (Exception ex ){
  162.  
  163. Ex. printStackTrace ();
  164.  
  165. }
  166.  
  167. }
  168.  
  169. /**
  170.  
  171. * Display local image files
  172.  
  173. */
  174.  
  175. Private void localImage (){
  176.  
  177. Try {
  178.  
  179. // Local file processing (if the file name contains spaces, use + to replace it)
  180.  
  181. WebView. loadUrl ("file: // android_asset/icon.png ");
  182.  
  183. } Catch (Exception ex ){
  184.  
  185. Ex. printStackTrace ();
  186.  
  187. }
  188.  
  189. }
  190.  
  191. /**
  192.  
  193. * Display local webpage files
  194.  
  195. */
  196.  
  197. Private void localHtml (){
  198.  
  199. Try {
  200.  
  201. // Local file processing (if the file name contains spaces, use + to replace it)
  202.  
  203. WebView. loadUrl ("file: // android_asset/test.html ");
  204.  
  205. } Catch (Exception ex ){
  206.  
  207. Ex. printStackTrace ();
  208.  
  209. }
  210.  
  211. }
  212.  
  213. /**
  214.  
  215. * Display Html content mixed with local images and text
  216.  
  217. */
  218.  
  219. Private void localHtmlImage (){
  220.  
  221. Try {
  222.  
  223. String data = "test the mixed display of local images and text. This is an image in APK ";
  224.  
  225. // SDK1.5 local file processing (Images cannot be displayed)
  226.  
  227. // WebView. loadData (URLEncoder. encode (data, encoding), mimeType,
  228.  
  229. // Encoding );
  230.  
  231. // SDK1.6 and later versions
  232.  
  233. // WebView. loadData (data, mimeType, encoding );
  234.  
  235. // Process local files (images can be displayed)
  236.  
  237. WebView. loadDataWithBaseURL ("about: blank", data, mimeType,
  238.  
  239. Encoding ,"");
  240.  
  241. } Catch (Exception ex ){
  242.  
  243. Ex. printStackTrace ();
  244.  
  245. }
  246.  
  247. }
  248.  
  249. }

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.