Correct use of Android development WebView

Source: Internet
Author: User

Both Android and iOS systems offer standard browser controls, which are Webview,ios in Android for UIWebView. In iOS you instantiate a UIWebView to invoke loadrequest to load a Web page, but in Android you need not only to create a webview, but also to do something else and suggest that the first time you use WebView's readers follow these steps:

(1) Add a webview to the XML that you want to implement WebView, and make the layout according to your own requirements, as follows:

<framelayout
android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"

android:layout_gravity= "Center" >

Add a WebView control

<webview
android:id= "@+id/webview"
android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"/>
//Place a continuous-spinning loading progress bar in the center of the current activity
<progressbar android:id= "@+id/progressbar"
android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_gravity= "center"

android:visibility= "Gone"/>

</FrameLayout>

(2) Add Network access in Androidmanifest.xml, no access to the network.

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

(3) Add the following code to the activity using WebView:

Get WebView variable

WebView WebView = (WebView) Findviewbyid (R.id.webview);

xxxxxxxx

Remember to add the next line of code, because Android WebView default is not in response to JavaScript control, if not add the following line, there will be a very strange problem, there are many people on the internet is asking why the Web links loaded can click and jump, But the button point didn't respond. Because many Web page controls are written by JS, add the following line of code to respond to the JS control.

Webview.getsettings (). Setjavascriptenabled (True);

xxxxxxxx

Load a Web page

Webview.loadurl ("http://www.baidu.com");

(4) Handle page load status callback, such as Start loading Web page, load success, load timeout, etc.

Add a page load status callback, where Webviewclient's anonymous inner class is used, and if you don't know what callback functions the Webviewclient class can respond to, you can select Webviewclient in the code, right-click the source code--- > Overwrite/implement method, in the popup dialog box contains all the methods that can be overridden by webviewclient. If you do not add this method, when you click on the link in the Web page, a popup box will prompt you to use which browser to open the link.

Webview.setwebviewclient (New Webviewclient () {
Called at the beginning of page load, displays the load prompt rotation progress bar
@Override
public void onpagestarted (WebView view, String URL, Bitmap favicon) {
TODO auto-generated Method Stub
super.onpagestarted (view, URL, favicon);
Progressbar.setvisibility (Android.view.View.VISIBLE);
Toast.maketext (Elechall.this, "onpagestarted", 2). Show ();
}

Called when Web page loading is complete, hide load prompt rotation progress bar
@Override
public void onpagefinished (WebView view, String URL) {
TODO auto-generated Method Stub
super.onpagefinished (view, URL);
Progressbar.setvisibility (Android.view.View.GONE);
Toast.maketext (Elechall.this, "onpagefinished", 2). Show ();
}
Called when page load fails, hide load prompt rotation progress bar
@Override
public void Onreceivederror (WebView view, int errorCode,
String description, String failingurl) {
TODO auto-generated Method Stub
Super.onreceivederror (view, ErrorCode, description, Failingurl);
Progressbar.setvisibility (Android.view.View.GONE);
Toast.maketext (Elechall.this, "Onreceivederror", 2). Show ();
}

});

Correct use of Android development WebView

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.