Sunday, November 04, 2012

How to load web site in native Android application using WebView control

In this post, I am going to explain how to load website in native android web application using webview control. Just three lines of code is enough to make it happen.

Just use the below code to load the website either during oncreate event or button click event.

 webView = (WebView) findViewById(R.id.webView1);  
 webView.getSettings().setJavaScriptEnabled(true);  
 webView.loadUrl("http://ayyanar.blogspot.com");  


And also, you have to set INTERNET PERMISSION in manifest file like below.

  <uses-permission android:name="android.permission.INTERNET" />  


Here is the complete code to do this.

Layout File:

 <RelativeLayout 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" >  
   <TextView  
     android:id="@+id/textView1"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:layout_centerHorizontal="true"  
     android:layout_centerVertical="true"  
     android:padding="@dimen/padding_medium"  
     android:text="@string/hello_world"  
     tools:context=".WebViewActivity" />  
   <WebView  
     android:id="@+id/webView1"  
     android:layout_width="match_parent"  
     android:layout_height="match_parent"  
     android:layout_alignParentBottom="true"  
     android:layout_alignParentRight="true" />  
 </RelativeLayout>  


Activity Class:

 
 import android.os.Bundle;  
 import android.app.Activity;  
 import android.view.Menu;  
 import android.webkit.WebView;  
 public class WebViewActivity extends Activity {  
      WebView webView;  
   @Override  
   public void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.web_view);  
     webView = (WebView) findViewById(R.id.webView1);  
           webView.getSettings().setJavaScriptEnabled(true);  
           webView.loadUrl("http://ayyanar.blogspot.com");  
   }  
   @Override  
   public boolean onCreateOptionsMenu(Menu menu) {  
     getMenuInflater().inflate(R.menu.web_view, menu);  
     return true;  
   }  
 }  


Manifest File:

 <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
   package="com.ayyanar.helloworld"  
   android:versionCode="1"  
   android:versionName="1.0" >  
   <uses-sdk  
     android:minSdkVersion="8"  
     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=".WebViewActivity"  
       android:label="@string/title_activity_web_view" >  
       <intent-filter>  
         <action android:name="android.intent.action.MAIN" />  
         <category android:name="android.intent.category.LAUNCHER" />  
       </intent-filter>  
     </activity>  
   </application>  
 </manifest>  


 Screenshot:

 

No comments: