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

General process of online event operation

There is no clear definition of operation and spe...

Will programmers be fired when they get older?

Internet companies like BAT may not recruit progr...

A brief introduction to Douyin operation

How did Tik Tok become so popular? The editor ana...

Product analysis, function analysis | Bilibili Live!

Editor's Note: Since its listing , Bilibili (...

Do user needs really need to be met?

"Users say they want this feature, why haven...

When planning a hit event, you need to pay attention to 3 issues

Activities are an important means of operation an...

Spring Festival holiday arrangements for 12 app stores

360 Mobile Assistant Application & Game Revie...

B station marketing promotion methods and strategies!

Brand marketing is a long-term thing. The communi...