Several common functions used in WebView _android

Source: Internet
Author: User
Tags set background

Android's WebView component can be said to be quite powerful, and now summarizes several features often used in the project as follows:

First, background settings

Webview.setbackgroundcolor (0);//Set background color to transparent first 

Second, get WebView Web page load initialization and completion events

Steps:

1 Create one's own webviewclient (inheriting Webviewclient Class) such as Webviewclient

2 overload inside the onpagefinished (webview view, String url) method, (WebView load completion will call this method), this method put what you want to do, after the WebView load complete

3 Associate your own webviewclient with WebView through this method: Webview.setwebviewclient (new Webviewclient ();

Mwebview.setwebviewclient (New Webviewclient () 
{   
 @Override public 
void onpagefinished (WebView view, String URL)  
{ 
//end 
super.onpagefinished (view, URL); 
} 
 @Override public 
 void onpagestarted (webview view, String URL, Bitmap favicon)  
 { 
 //start 
 super.onpagestarted (view, URL, favicon); 
 } 
); 

If you need to monitor load progress, you need to create a webchromeclient class of your own, overload the method onprogresschanged, and then

Webview.setwebchromeclient (New Mywebchromeclient ()): Class Mywebchromeclient extends Webchromeclient {@Override Pub LIC void onprogresschanged (webview view, int newprogress) {//TODO auto-generated Method stub super.onprogresschanged (v 
Iew, newprogress); 
 
 
  For example: public class Webpageloader extends activity {final activity activity = this; 
    @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
    This.getwindow (). Requestfeature (window.feature_progress); 
    Setcontentview (R.layout.main); 
    WebView WebView = (webview) Findviewbyid (R.id.webview); 
    Webview.getsettings (). Setjavascriptenabled (True); 
    Webview.getsettings (). Setsupportzoom (True); 
        Webview.setwebchromeclient (New Webchromeclient () {public void onprogresschanged (WebView view, int progress) { 
        Activity.settitle ("Loading ..."); 
        Activity.setprogress (Progress * 100); if (progress = =) 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 (W 
        Ebview view, String URL) {view.loadurl (URL); 
      return true; 
    } 
    }); 
  Webview.loadurl ("http://www.sohu.com"); 
 } 
}

Layout file:

<?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 "/>  

Note that a series of usages of the webview, such as Webview.getsettings (). Setjavascriptenabled (True), are set to use Javscript;

Webview.getsettings (). Setjavascriptenabled (true);  
Webview.setscrollbarstyle (webview.scrollbars_outside_overlay);  
Webview.sethorizontalscrollbarenabled (false);  
Webview.getsettings (). Setsupportzoom (true);  
Webview.getsettings (). Setbuiltinzoomcontrols (true);  
Webview.setinitialscale ();  
Webview.sethorizontalscrollbaroverlay (TRUE); 

And so on, the specific reference API
The use of the progress bar is to write the Onprogresschanged event in the inner class after new one is setwebchromeclient

Summarize:

In the design of webview, not everything should be WebView class dry, some chores are divided to other people, so webview concentrate on their own interpretation, rendering work on the line. Webviewclient is to help WebView handle various notices and request events, specifically including:

    • Onloadresource
    • Onpagestart
    • Onpagefinish
    • Onreceiveerror
    • Onreceivedhttpauthrequest

Webchromeclient is the Auxiliary webview Processing JavaScript dialog box, website icon, website title, loading progress, etc.

    • Onclosewindow (Close WebView)
    • Oncreatewindow ()
    • Onjsalert (WebView on alert is not going to come out, need to customize your webchromeclient processing pop-up)
    • Onjsprompt
    • Onjsconfirm
    • Onprogresschanged
    • Onreceivedicon
    • Onreceivedtitle

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.