Android WebView control displays webpage, androidwebview

Source: Internet
Author: User

Android WebView control displays webpage, androidwebview

Sometimes you need to display web pages in the app without calling other browsers to browse the Web pages, then you need the WebView control. This control is also very powerful. You can zoom in, zoom out, move forward, and move back to the web page.

1. Some methods

// Supports javascript
Web. getSettings (). setJavaScriptEnabled (true );
// Supports scaling.
Web. getSettings (). setsuppzoom zoom (true );
// Set the scaling Tool
Web. getSettings (). setBuiltInZoomControls (true );
// Scale up or down the scale
Web. getSettings (). setUseWideViewPort (true );
// Adaptive screen
Web. getSettings (). setLayoutAlgorithm (LayoutAlgorithm. SINGLE_COLUMN );
Web. getSettings (). setLoadWithOverviewMode (true );

Here we will talk about some webpages that need to be scaled by themselves to scale the webpage. If not, scaling cannot be performed.

2. Layout
 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2     xmlns:tools="http://schemas.android.com/tools" 3     android:layout_width="match_parent" 4     android:layout_height="match_parent" 5     android:orientation="vertical" > 6  7     <ProgressBar 8         android:id="@+id/progressBarLoading" 9         style="?android:attr/progressBarStyleHorizontal"10         android:layout_width="match_parent"11         android:layout_height="3dp" />12 13     <WebView14         android:id="@+id/webView1"15         android:layout_width="match_parent"16         android:layout_height="match_parent" />17 18 </LinearLayout>
3. java code
Package com. example. asynchttpclient; import android. OS. bundle; import android. app. activity; import android. view. keyEvent; import android. view. view; import android. webkit. webChromeClient; import android. webkit. webSettings. zoomDensity; import android. webkit. webView; import android. webkit. webViewClient; import android. widget. progressBar; public class WebActivity extends Activity {private ProgressBar mLoadingPr Ogress; private WebView webView; private String mstrLoginUrl = "http://baidu.com"; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_web); webView = (WebView) findViewById (R. id. webView1); mLoadingProgress = (ProgressBar) findViewById (R. id. progressBarLoading); mLoadingProgress. setMax (100); webView. getSettings (). setUseWi DeViewPort (true); webView. getSettings (). setJavaScriptEnabled (true); webView. getSettings (). setsuppzoom zoom (true); // you can set the parameter to support webView scaling. getSettings (). setDefaultZoom (ZoomDensity. FAR); webView. loadUrl (mstrLoginUrl); // overwrite WebView. By default, a third-party or default browser is used to open a Web page. setWebViewClient (new WebViewClient () {@ Override public boolean shouldOverrideUrlLoading (WebView view, String url) {view. loadUrl); // Set the loading progress bar view. setWebChromeClient (new WebChromeClientProgress (); return true ;}});} private class extends WebChromeClient {@ Override public void onProgressChanged (WebView view, int progress) {if (mLoadingProgress! = Null) {mLoadingProgress. setProgress (progress); if (progress = 100) mLoadingProgress. setVisibility (View. GONE);} super. onProgressChanged (view, progress) ;}/ *** responds with a key. When viewing a webpage in WebView, check whether there are any history records that can be moved forward. * // @ Override public boolean onKeyDown (int keyCode, KeyEvent event) {// Check if the key event was the Back button and if there's history if (keyCode = KeyEvent. KEYCODE_BACK) & webView. canGoBack () {// return the webView key. goBack (); return true;} // If it wasn' t the Back key or there's no web page history, bubble up // to the default // system behavior (probably exit the activity) return super. onKeyDown (keyCode, event );}}
4. Example Image

 

5. Connection Permissions

<Uses-permission android: name = "android. permission. INTERNET"/>

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.