Android Control WebView

Android Control WebView

How to open a website in an Android app? Google has provided a solution for us. Now let's take a look at the WebView control.

For the convenience of summary, let's summarize it based on the following effect:

First, let's take a look at its layout file. The entire interface is divided into two parts, the upper part is similar to the title bar, which is composed of two Buttons and a TextView, and the lower part is a WebView control. By removing the system title through AndroidManifest.xml (if you don't understand, please refer to my previous blog: Common Android Attributes), the effect above has been achieved. For your convenience, the code is presented below:

  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= "Return" />
  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>

*** Let's start writing our 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/" );//Set 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 website 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. //Button 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. }

***Don't forget to add the network usage declaration in AndroidManifest.xml: <uses-permission android:name="android.permission.INTERNET"/>

That's it, our initial introduction to WebView ends here.

<<:  Intelligent "public opinion management", Net Entertainment Zhixin enables enterprises to develop clairvoyance

>>:  Common properties for Android development

Recommend

20 rules for short video ads to go viral!

Brand advertisers want to achieve results, and pe...

Uncovering the secrets of Asia’s “water tower”: majestic yet fragile

Your browser does not support the video tag The Q...

How to design a complete operation plan?

This article hopes to help entry-level product ma...

Do you know the real reason why lithium battery performance deteriorates?

Lithium-ion batteries are now widely used in mobi...

User growth ≠ fission and new user acquisition!

Fission and attracting new users should not be th...

Between ofo and Mobike, which one has the better operational strategy?

In December 1922, the last emperor Puyi married W...

Analysis of Xianyu's competitive products

The demand for second-hand goods market is growin...

Hidden in the city: the mysterious desert cat hiding in the man-made forest

The desert cat has obvious tufts of hair on the e...

Server rental for thousands of people online at the same time

When renting a server with thousands of people on...

Event planning and operation skills!

Today I want to talk to you about how to plan an ...

How to set up event prizes like Alipay Koi?

Prizes are inducement conditions that stimulate us...