Code for setting webview in android
/*WebViewActivity.java*/package com.shubham.webviewclientdemo;import android.app.Activity;import android.graphics.Bitmap;import android.os.Bundle;import android.view.KeyEvent;import android.view.View;import android.webkit.WebView;import android.webkit.WebViewClient;import android.widget.ProgressBar;/* * Demo of creating an application to open any URL inside the application and clicking on any link from that URlshould not open Native browser but that URL should open in the same screen.- Load WebView with progress bar */public class WebViewClientDemoActivity extends Activity { /** Called when the activity is first created. */ WebView web; ProgressBar progressBar; StartAppAd startAppAd; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); web = (WebView) findViewById(R.id.webview01); progressBar = (ProgressBar) findViewById(R.id.progressBar1); web.setWebViewClient(new myWebClient()); web.getSettings().setJavaScriptEnabled(true); /* save the files to assets folder of project if want to display local files in webview*/ web.loadUrl("file:///android_asset/www/index.html"); } public class myWebClient extends WebViewClient { @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { // TODO Auto-generated method stub super.onPageStarted(view, url, favicon); } @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { // TODO Auto-generated method stub view.loadUrl(url); return true; } @Override public void onPageFinished(WebView view, String url) { // TODO Auto-generated method stub super.onPageFinished(view, url); progressBar.setVisibility(View.GONE); } } // To handle "Back" key press event for WebView to go back to previous screen. @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) { startAppAd.showAd(); // show the ad startAppAd.loadAd(StartAppAd.AdMode.AUTOMATIC); // load the next ad web.goBack(); return true; } return super.onKeyDown(keyCode, event); } @Override public void onResume(){ super.onResume(); startAppAd.onResume(); } @Override public void onPause() { super.onPause(); startAppAd.onPause(); }}
---------------------------------------------------------------------------------------------------------------------------<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.shubham.webviewclientdemo" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" /> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/> <uses-permission android:name="android.permission.GET_TASKS"/> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:label="@string/app_name" android:name=".WebViewActivity" > <intent-filter > <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>
Comments
Post a Comment