WebView of Android controls

Source: Internet
Author: User

How do I open a Web site in an Android app? Google has provided us with a solution, so let's take a look at the WebView control now.

In order to facilitate the summary, to achieve the following effect as the main line, to summarize:

First we look at its layout file, the entire interface is divided into two parts, the upper is a similar to the title bar effect, it is composed of two button buttons and a TextView, the lower part is a WebView control, Remove the title of the system by Androidmanifest.xml (if you do not understand, please check my previous blog: Android Common Properties), has achieved the effect. For the convenience of self-study, the following code:

<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "Vertical"Tools:context=". Mainactivity "> <LinearLayout android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:weightsum= "1" > <Button Android:id= "@+id/quit"android:layout_gravity= "Left"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Back"/> <TextView Android:id= "@+id/web"android:layout_gravity= "Center"android:gravity= "Center"Android:layout_width= "222DP"Android:layout_height= "Wrap_content"Android:layout_weight= "1.13"/> <Button Android:id= "@+id/news"android:layout_gravity= "Right"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Refresh"/> </LinearLayout> <WebView Android:id= "@+id/webview"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"/></linearlayout>

Finally we begin to write our Mainactivity.java:

 Public classMainactivityextendsActivity {PrivateTextView Mtextview; PrivateWebView Mwebview; PrivateButton Mbreak; PrivateButton mnews; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main);    Init (); }     Public voidinit () {Mtextview=(TextView) Findviewbyid (R.id.web); Mwebview=(WebView) Findviewbyid (R.id.webview); Mbreak=(Button) Findviewbyid (r.id.quit); Mnews=(Button) Findviewbyid (r.id.news); Mbreak.setonclicklistener (NewMyListener ()); Mnews.setonclicklistener (NewMyListener ()); Mwebview.loadurl ("http://www.baidu.com/");//set the Open URLMwebview.setwebchromeclient (Newwebchromeclient () {@Override Public voidOnreceivedtitle (WebView view, String title) {Super. Onreceivedtitle (view, title); Mtextview.settext (title);//Show open URL information            }        }); Mwebview.setwebviewclient (Newwebviewclient () {@Override Public Booleanshouldoverrideurlloading (WebView view, String URL) {view.loadurl (URL); return Super. shouldoverrideurlloading (view, URL);    }        }); }    //button click event Listener    classMyListenerImplementsview.onclicklistener{@Override Public voidOnClick (view view) {Switch(View.getid ()) { CaseR.id.quit:finish ();  Break;  Caser.id.news:mwebview.reload ();  Break; }        }    }

Finally, do not forget to add the Use network declaration in Androidmanifest.xml: <uses-permission android:name= "Android.permission.INTERNET"/>

This concludes our initial introduction to the WebView.

<uses-permission android:name= "Android.permission.INTERNET"/>

WebView of Android controls

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.