Android WebView and androidwebview
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:
<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 started to write MainActivity. java:
Public class MainActivity extends Activity {private TextView mTextView; private WebView mWebView; private Button mbreak; private Button mnews; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); init ();} public void init () {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 (new myListener (); mnews. setOnClickListener (new myListener (); mWebView. loadUrl ("http://www.baidu.com/"); // sets the open URL mWebView. setWebChromeClient (new WebChromeClient () {@ Override public void onReceivedTitle (WebView view, String title) {super. onReceivedTitle (view, title); mTextView. setText (title); // display the opened URL Information}); mWebView. setWebViewClient (new WebViewClient () {@ Override public boolean shouldOverrideUrlLoading (WebView view, String url) {view. loadUrl (url); return super. shouldOverrideUrlLoading (view, url) ;}}) ;}// click the event listening class myListener implements View. onClickListener {@ Override public void onClick (View view) {switch (view. getId () {case R. id. quit: finish (); break; case R. id. news: mWebView. reload (); break ;}}}
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.
<Uses-permission android: name = "android. permission. INTERNET"/>