WebView displays web pages inside your application. It turns your application into a web application. The Android web app is an application that uses the Android WebView component to render part of the Android app's GUI. The WebView component is a fully-fledged browser implemented as a View subclass, therefore, you can embed it in your Android app's GUI.
Setting your own custom WebViewClient to WebView allows you to handle onPageStarted, onPageFinished, shouldOverrideUrlLoading, etc. WebChromeClient permits you to handle Javascript's alert() and other functions. In order to add WebView to your application, you have to add <WebView>element to your xml layout file.
The following syntax is for opening a URL browser in one click using Intent.
Ex:
The following example illustrates how to open a URL in WebView and how to go back to the previous page if the back button is pressed.
Create MainActivity.java under src/<your packagename>.
MainActivity.java:
[java]
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button save=(Button)findViewById(R.id.button1);
//on click save
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i=new Intent(MainActivity.this,Web.class);
startActivity(i);
}
});
}
}
[/java]
Create Web.java(an activity which has WebView) under src/<your packagename>.
Web.java:
[java]
public class Web extends Activity{
//variable declaration
ProgressBar progress;
Context context;
WebView wv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//set screen orientation to portrait mode
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//set activity content to external view
setContentView(R.layout.activity_web);
//find view by Id
progress=(ProgressBar)findViewById(R.id.progressBar1);
wv=(WebView)findViewById(R.id.webView1);
context=this;
//webview settings
wv.getSettings().setBuiltInZoomControls(true);
wv.getSettings().setJavaScriptEnabled(true);
wv.getSettings().setSupportZoom(true);
wv.getSettings().setLoadWithOverviewMode(true);
wv.getSettings().setUseWideViewPort(true);
wv.loadUrl("https://www.google.com");
//handles javascript alert() and functions
wv.setWebChromeClient(new WebChromeClient());
//custom WebViewClient
wv.setWebViewClient(new MyWebClient());
String urlW=wv.getUrl();
}
public class MyWebClient extends WebViewClient
{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//opens a URL in webview
view.loadUrl(url);
return super.shouldOverrideUrlLoading(view, url);
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
//progress bar is visible
super.onPageStarted(view, url, favicon);
progress.setVisibility(View.VISIBLE);
}
@Override
public void onPageFinished(WebView view, String url) {
//invisible the progress bar
super.onPageFinished(view, url);
progress.setVisibility(View.GONE);
}
}
@Override
// Detect when the back button is pressed
public void onBackPressed() {
if(wv.canGoBack()) {
//go back to previous page if back button is pressed.
wv.goBack();
} else {
// Let the system handle the back button
super.onBackPressed();
}
}
}
[/java]
Create activity_main.xml in res/layout folder.
activity_main.xml:
[xml]
[/xml]
Create activity_web.xml under res/layout.
activity_web.xml:
[xml]
[/xml]
AndroidManifest.xml:
You need to add Internet permission to your AndroidManifest.xml.