What is WebViewUsing WebView we can open web page in our Android app, using url or from local file (in SD card etc).
Using WebView we can built a good looking and animated app using HTML, CSS, Javascript or J Query, We save our web pages in apps local storage and use them using WebView.
Create new project in eclipse (Android-First-Programm).
Click on res folder and then layout, now open required xml where you want to use WebView.
We can create any control like WebView using both layouts
1) Graphical Layout
2) Xml Layout
1) Using “Graphical Layout” Drag WebView from “Composite” tab.
Double click on WebView and set its properties in xml layout.
2) In Xml Layout write this code to create WebView.
<WebView android:id="@+id/webView1" android:layout_width="match_parent" android:layout_height="wrap_content" />
Now by reference we can use WebView in code and can use it programmatically.
Open Activity (.java file) to use that WebView.
Activity is located in src folder -> package and choose activity where you want to use WebView.
To access WebView from xml layout we use its reference using this code.
WebView webView=(WebView) findViewById(R.id.webView1);
Using “findViewById();” we can access reference of any control like WebView from xml layout.
“webView1” is id of that WebView that we want to use and it must be exist in xml layout that we have assigned in “setContentView(R.layout.main);”.
Now assign url to open
webView.loadUrl("http://www.wisdomitsol.com");
We must assign “Internet” permission in “AndroidManifest.xml”
<uses-permission android:name="android.permission.INTERNET"/>
Using loadUrl our page will open in device default browser.
So complete code with comments is available here.
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <WebView android:id="@+id/webView1" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
MainActivity.java
import android.app.Activity; import android.os.Bundle; import android.webkit.WebView; import android.webkit.WebViewClient; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); WebView webView=(WebView) findViewById(R.id.webView1); webView.setWebViewClient(new myWebClient()); webView.loadUrl("http://www.wisdomitsol.com"); } public class myWebClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } } }
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.androidwebview" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="3" android:targetSdkVersion="15" /> <uses-permission android:name="android.permission.INTERNET" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/title_activity_main" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
If we want to load WebView from a string of html code or from local file, then we dont need “Internet” permession in “AndroidManifest.xml” and MainActivity.java will be like this.
MainActivity.java
public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); WebView webView=(WebView) findViewById(R.id.webView1); String htmlString="<html><body><h1>Well come in wisdomITSol......</h1></body></html>"; webView.loadData(htmlString, "text/html", "UTF-8"); /*webView.loadUrl("file:///android_res/raw/test.html");//from raw folder webView.loadUrl("file:///android_asset/test.html");//from asset folder*/ } }