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

My friend, you must have wasted more than half of your advertising budget...

A few days ago, I met a friend who worked on home...

"Personal Nutritionist" (27 video lessons)

"Personal Nutritionist" (27 video lesso...

How to evaluate whether KOL marketing is effective? Look at these 5 points!

In a recent survey, 76% of marketers said that th...

The Gansu earthquake also triggered another disaster...

At 23:59 on December 18, a 6.2-magnitude earthqua...

How to create an advertisement that touches people’s hearts? Look here

We all know that marketing is a series of activit...

IUNI U2 first review

IUNI U2 has three different versions: Ice Silver, ...

The sky is falling, disposable chopsticks are bleached with sulfur dioxide! ?

I don’t know if you have seen such news online, s...

Practical Douyin promotion skills, teach you how to play Douyin from scratch!

In the past two years, the traffic dividend has b...

Arthritis is not caused by freezing, but I still advise you to wear long johns

gossip In the past, there was a kind of cold call...