Android development: WebView of controls

Source: Internet
Author: User

Android development: WebView of controls

How can I open a Web site in an Android app? Google provides us with a solution. Now let's take a look at the WebView control.

To facilitate the summary, we will summarize the following results:

First, let's take a look at its layout file. The entire interface is divided into two parts: the upper part is a title bar effect, which consists of two buttons and a TextView, the lower part is a WebView control through AndroidManifest. xml removes the system title (if you do not understand it, please refer to my previous blog: common Android attributes. For your convenience, the following code is provided:

 
  1. <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
  2. Xmlns: tools = "http://schemas.android.com/tools"
  3. Android: layout_width = "match_parent"
  4. Android: layout_height = "match_parent"
  5. Android: orientation = "vertical"
  6. Tools: context = ". MainActivity">
  7.  
  8. <LinearLayout
  9. Android: layout_width = "fill_parent"
  10. Android: layout_height = "wrap_content"
  11. Android: weightSum = "1">
  12. <Button
  13. Android: id = "@ + id/quit"
  14. Android: layout_gravity = "left"
  15. Android: layout_width = "wrap_content"
  16. Android: layout_height = "wrap_content"
  17. Android: text = "back"/>
  18. <TextView
  19. Android: id = "@ + id/web"
  20. Android: layout_gravity = "center"
  21. Android: gravity = "center"
  22. Android: layout_width = "222dp"
  23. Android: layout_height = "wrap_content"
  24. Android: layout_weight = "1.13"/>
  25. <Button
  26. Android: id = "@ + id/news"
  27. Android: layout_gravity = "right"
  28. Android: layout_width = "wrap_content"
  29. Android: layout_height = "wrap_content"
  30. Android: text = "refresh"/>
  31. </LinearLayout>
  32. <WebView
  33. Android: id = "@ + id/webView"
  34. Android: layout_width = "fill_parent"
  35. Android: layout_height = "fill_parent"/>
  36.  
  37. </LinearLayout>

Finally, we started to write MainActivity. java:

 
  1. Public class MainActivity extends Activity {
  2. Private TextView mTextView;
  3. Private WebView mWebView;
  4. Private Button mbreak;
  5. Private Button mnews;
  6. @ Override
  7. Protected void onCreate (Bundle savedInstanceState ){
  8. Super. onCreate (savedInstanceState );
  9. SetContentView (R. layout. activity_main );
  10. Init ();
  11. }
  12. Public void init (){
  13. MTextView = (TextView) findViewById (R. id. web );
  14. MWebView = (WebView) findViewById (R. id. webView );
  15. Mbreak = (Button) findViewById (R. id. quit );
  16. Mnews = (Button) findViewById (R. id. news );
  17. Mbreak. setOnClickListener (new myListener ());
  18. Mnews. setOnClickListener (new myListener ());
  19. MWebView. loadUrl ("http://www.baidu.com/"); // sets the URL to open
  20.  
  21. MWebView. setWebChromeClient (new WebChromeClient (){
  22. @ Override
  23. Public void onReceivedTitle (WebView view, String title ){
  24. Super. onReceivedTitle (view, title );
  25. MTextView. setText (title); // display the opened URL Information
  26. }
  27. });
  28.  
  29. MWebView. setWebViewClient (new WebViewClient (){
  30. @ Override
  31. Public boolean shouldOverrideUrlLoading (WebView view, String url ){
  32. View. loadUrl (url );
  33. Return super. shouldOverrideUrlLoading (view, url );
  34. }
  35. });
  36. }
  37.  
  38. // Click event monitoring
  39. Class myListener implements View. OnClickListener {
  40. @ Override
  41. Public void onClick (View view ){
  42. Switch (view. getId ()){
  43. Case R. id. quit:
  44. Finish ();
  45. Break;
  46. Case R. id. news:
  47. MWebView. reload ();
  48. Break;
  49. }
  50. }
  51. }

Do not forget to add the network declaration in AndroidManifest. xml: <uses-permission android: name = "android. permission. INTERNET"/>

This is the end of our WebView introduction.

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.