Building Web Apps in WebView
If you want to deliver a web application (or just a web page) as a part of a client application,
you can do it using WebView
. The WebView
class is an
extension of Android's View
class that allows you to display web pages as a
part of your activity layout.It does not include any features of a fully developed web
browser, such as navigation controls or an address bar. All that WebView
does, by default, is show a web page.
Create your layout: activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<WebView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
within your Activity: MainActivity.java
import android.os.Bundle;import android.annotation.SuppressLint;
import android.app.Activity;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends Activity {
WebView webView;
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView)findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
// Handling page navigation
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("http://ashfaquedl.blogspot.in/");
webView.getSettings().setSupportZoom(true);
webView.getSettings().setBuiltInZoomControls(true);
}
// Navigating web page history
@Override
public void onBackPressed() {
if (webView.copyBackForwardList().getCurrentIndex() > 0) {
webView.goBack();
}
else {
// Your exit alert code, or alternatively line below to finish
super.onBackPressed(); // finishes activity
}
}
}
Note: And most important thing don't forget to add below uses-permission in AndroidMenifest.xml.
<uses-permission android:name="android.permission.INTERNET"/>
now your app is rocking.