Android: Embedded browser in view

Source: Internet
Author: User

One thing I want to do is embed a browser in the middle of a page. At first I don't know where to start, because textveiw or editveiw were used in the past, but they cannot be used to display webpage content. What should they do?

The first thought was: Is there a view used to display webpages? So I entered <web, haha in the XML editor of eclipse. The following prompt is: webveiw, so can we use it? So I searched for the information on the Internet. That's OK. Let's start.

 

Create a webview in XML and use findviewbyid () in the code to obtain it. My code is:

 

Webview = (webview) This. findviewbyid ("@/ID/webview ");

 

Then I used the content I found from the Internet. Let's take a look.

 

 

How to Create a webview:

1. add permissions: The permission "android. Permission. Internet" must be used in androidmanifest. xml. Otherwise, the web page not available error may occur.
2. Generate a webview component in the activity: webview = new webview (this );
3. Set Basic webview information:
If the accessed page contains JavaScript, webview must be set to support JavaScript.
Webview. getsettings (). setjavascriptenabled (true );
Touch focus Function
Requestfocus (); // if this parameter is not set, When you click the webpage text input box, the keyboard cannot be displayed and other events are not responded.
Cancel scroll bar
This. setscrollbarstyle (scrollbars_outside_overlay );
4. Set the web page to be displayed in wevview:
For Internet: webview. loadurl ("http://www.google.com ");
Local file: webview. loadurl ("file: // android_asset/xx.html"); local files are stored in the assets file.
5. If you want to click the link, you can handle it by yourself, instead of clicking the link in the browser of the new Android system.
Add an event listening object (webviewclient) to webview)

And rewrite some of the methods.
Shouldoverrideurlloading: Response to the hyperlink button on the webpage.
When you press a connection, webviewclient will call this method and pass the parameter: URL
Onloadresource
Onpagestart
Onpagefinish
Onreceiveerror
Onreceivedhttpauthrequest

6. If you use the webview link to view many pages without any processing, click the system "back" key and the entire browser will call finish () to end itself, if you want to roll back the web page instead of exiting the browser, you need to process and consume the back event in the current activity.
Override the onkeydown (INT keycoder, keyevent event) method of the activity class.
Public Boolean onkeydown (INT keycoder, keyevent event ){
If (webview. cangoback () & keycoder = keyevent. keycode_back ){
Webview. Goback (); // Goback () indicates that the previous page of webview is returned.

Return true;
}
Return false;
}

Webview problems:

Android webview is very powerful. It is actually a browser. You can embed it into your desired location. I have two problems here: How to know the webpage loading progress and how to load the webpage,
Click the link in the webpage or jump to the current webview. You do not want to jump to the browser. The solution is as follows:

// This method can handle some webview operations when loading and loading is completed
Webview. setwebchromeclient (New webchromeclient (){
@ Override
Public void onprogresschanged (webview view, int newprogress ){
If (newprogress = 100) {// set the title of the activity. You can also perform other operations as needed.
Title. settext ("loaded ");
} Else {
Title. settext ("loading .......");

}
}
});

Webview. setwebviewclient (New webviewclient (){
@ Override
Public Boolean shouldoverrideurlloading (webview view, string URL) {// rewrite this method to indicate that clicking the link in the webpage is still redirected to the current webview, not to the browser.
View. loadurl (URL );
Return true;
}

@ Override
Public void onreceivedsslerror (webview view, sslerrorhandler handler, android.net. http. sslerror error) {// rewrite this method to allow webview to process HTTPS requests
Handler. Proceed ();
}

});

 

 

 

The following article describes how to add progress steps to webview.

 

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.