Sunday 9 December 2012

Android WebView Tutorial

Android WebView Tutorial

In this tutorial,i am going to teach you how to create an android application to redirect directly to the website which is provided in the project by simply selecting the app shortcuts.


STEP BY STEP

1.Create a new Android Project.



2.Select the main.xml file in the res/layout.
   
   Then select the WebView   
   Component from the Composite as like shown in the bellow figure.






3.Drag and drop the WebView component to the main.xml

   After that the main.xml will looks like as given bellow.



4.Then the main.xml code has been like given bellow.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <WebView
        android:id="@+id/webView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>


5.Select the MainActivity class file.Copy paste the given bellow code.

package com.roney.web;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;

public class SiteviewActivity extends Activity
{
/** Called when the activity is first created. */
    
private WebView webView;
@Override
public void onCreate(Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://www.androidituts.blogspot.com");
}
}


Change the required changes in the package name as well as the web url which you need to view.


6.WebView requires INTERNET Permission



  • To add the permission manually.Take AndroidManifest.xml
  • Then take Permissions-->Add-->Usess Permissions-->Select the required   permissions from the combo box provided.

Then the AndroidManifest.xml  code wiil as given bellow.


<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

  package="com.roney.web"

  android:versionCode="1"

  android:versionName="1.0" >

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

  <application
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name" >
      <activity
          android:name=".SiteviewActivity"
          android:label="@string/app_name" >
          <intent-filter>
           <action android:name="android.intent.action.MAIN" />
   <category android:name="android.intent.category.LAUNCHER" />
          </intent-filter>
      </activity>
  </application>
</manifest>


7.Run the application



If you need to run this application in the android smartphone


Hope you understand how it works.

" Ask your doubts and comments please "

1 comments:

Anonymous said...

very nice sir jee