Use webview, webchromeclient, and webviewclient in Android to load webpages

Source: Internet
Author: User

in Android applications, sometimes a webpage needs to be loaded. If a progress bar can be configured, android
provides excellent support, the following is an example of Program . Post it first:

 
<? XML version = "1.0" encoding = "UTF-8"?> <Webview xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Id = "@ + ID/webview" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"/>


Main Program: 

Public class webpageloader extends activity {final activity = This; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); this. getwindow (). requestfeature (window. feature_progress); setcontentview (R. layout. main); webview = (webview) findviewbyid (R. id. webview); webview. getsettings (). setjavascriptenabled (true); webview. getsettings (). setsuppzoom zoom (true); webview. setwebchromeclient (New webchromeclient () {public void onprogresschanged (webview view, int progress) {activity. settitle ("loading... "); activity. setprogress (Progress * 100); If (Progress = 100) activity. settitle (R. string. app_name) ;}}); webview. setwebviewclient (New webviewclient () {public void onreceivederror (webview view, int errorcode, string description, string failingurl) {// handle the error} public Boolean shouldoverrideurlloading (webview view, string URL) {view. loadurl (URL); Return true ;}}); webview. loadurl ("http://www.sohu.com ");}}


 It should be noted that a series of webview usage, such as webview. getsettings (). setjavascriptenabled (true); javscript can be used for setting; 

 webview. getsettings (). setjavascriptenabled (true); webview. setscrollbarstyle (webview. scrollbars_outside_overlay); webview. sethorizontalscrollbarenabled (false); webview. getsettings (). setsuppzoom zoom (true); webview. getsettings (). setbuiltinzoomcontrols (true); webview. setinitialscale (70); webview. sethorizontalscrollbaroverlay (true); 

For more information, see the API 
The progress bar can be written in the internal class after a new setwebchromeclient is generated. 
Onprogresschanged event 

In the design of webview, The webview class is not required to do anything. Some chores are assigned to others, so that webview can focus on parsing and rendering. Webviewclient helps webview process various notifications and request events, including: 

Onloadresource

Onpagestart

Onpagefinish

Onreceiveerror

Onreceivedhttpauthrequest

Webchromeclient is a dialog box that assists webview in processing Javascript, website icons, website titles, loading progress, etc.

Onclosewindow (disable webview)

Oncreatewindow ()

Onjsalert)

Onjsprompt

Onjsconfirm

Onprogresschanged

Onreceivedicon

Onreceivedtitle

Related Article

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.