Loading HTML text
In programmatically code, we locate it from layout, declaring a sample html string and load it to the WebView by calling loadData():
After running, this screen will be:
Loading Online Website
private class CustomWebViewClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } }Set it to WebView by calling setWebViewClient(), so our Activity code will become:
package devexchanges.info.webviewexample; import android.annotation.SuppressLint; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.webkit.WebView; import android.webkit.WebViewClient; public class MainActivity extends AppCompatActivity { private WebView webView; @SuppressLint("SetJavaScriptEnabled") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); webView = (WebView)findViewById(R.id.web_view); webView.setWebViewClient(new CustomWebViewClient()); webView.getSettings().setJavaScriptEnabled(true); webView.loadUrl("http://www.devexchanges.info/"); } private class CustomWebViewClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } } }Running app again, we have this output screen:
Common Error:
- URL String don't contains "http://" prefix usually cause crashed or cannot load data!
- Remember adding INTERNET permission if you want your app connect with online resource, please add this line above <application> tag:
Declaring more options to support Web browsing
- reload(): reload the web page you're viewing.
- goBack(): go back one step in the browsing history
- canGoBack(): determine whether there is history to be back or not.
- goForward(): a step forward in the browser history.
- canGoForward(): determine whether to proceed to history or not.
- goBackOrForward(): go back or go forward in the browser history, in which a negative number as a parameter representation is behind many steps, a positive number indicating a need to move beyond How many steps before.
- canGoBackOrForward(): determine if the browser can forward or backward with the step is passed on or not (value passed into positive / negative has been explained above).
- clearCache(): delete the browser cache resources.
- clearHistory(): delete the browser history.
- Control the font size through setDefaultFontSize() (to use a exactly size) or setTextSize() (to use the constants specified relative sizes like LARGER or SMALLEST).
- Control JavaScript through setJavaScriptEnabled() (true or false) (I used in example above).
- Control web content creation through methods setUserAgent(), so that you can know which web host you're using any device (desktop, mobile phone or whatever ).
Conclusions
References to official docs page:
- WebView: http://developer.android.com/reference/android/webkit/WebView.html
- WebSettings: http://developer.android.com/reference/android/webkit/WebSettings.html
- Webkit package: http://developer.android.com/reference/android/webkit/package-summary.html
Previous Chapter
|