Android WebView Example

Android WebView Example

android webview example.

You can develop your own webclient to access web sites in Android. WebView activity is used to load content of the web.

As your application is trying to access network resources, you need ad below line inside “AndroidManifest.xml”

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

Put the above line inside manifest tag of androidmanifest.xml.

GUI of android webview example.

<?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
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/webview"
        android:layout_width="fill_parent"
        android:layout_height="171dp" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <EditText
            android:id="@+id/editText1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10" android:text="http://">

            <requestFocus />
        </EditText>


        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="go" />

    </LinearLayout>

</LinearLayout>

Open the package inside src folder and write the code.

package Satalaj.Web.Demo;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;

public class AccessWebSiteDemoActivity extends Activity {
    /** Called when the activity is first created. */
    
    WebView mwebview;
    
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Button go = (Button) findViewById(R.id.button1);
        go.setOnClickListener( new View.OnClickListener() {
            
            public void onClick(View v) {
                
                EditText editText = (EditText) findViewById(R.id.editText1);
                
                String url = editText.getText().toString();
                
                
                 mwebview  = (WebView) findViewById(R.id.webview);
                 mwebview.getSettings().setJavaScriptEnabled(true);
                    
                    
                 SatWebViewClient x = new SatWebViewClient();
                 x.LoadContent(mwebview, url);
                
                
                
            }
        });
                        
        
    }    
   
}

class SatWebViewClient extends WebViewClient {
    
    public void LoadContent(WebView view, String url) {
        view.loadUrl(url);
        
    }
}

Description:

Instance of WebView has method called loadUrl which accepts URL of website as string. It loads the content of webpage specified at given url.

mwebview.getSettings().setJavaScriptEnabled(true);

Enables the JavaScript at client side.

String url = editText.getText().toString();

Returns text string.

Tags:

Android screen navigation example using Activity and IntentAndroid Button onClickListener ExampleAndroid EditText TutorialAndroid Button ClickAndroid Tutorial For BeginnersBest Android ApplicationsC# MemoryStream ExampleHow to earn money with android phoneC# HtmlTextWriter ExampleAndroid install APK from computer applicationHow to remove android applications

Author

My name is Satalaj, but people call me Sat. Here is my homepage: . I live in Pune, PN and work as a Software Engineer. I'm former MVP in ASP.net year 2010.
Disclaimer: Views or opinion expressed here are my personal research and it has nothing to do with my employer. You are free to use the code, ideas/hints in your projects. However, you should not copy and paste my original content to other web sites. Feel free to copy or extend the code.
If you want to fight with me, this website is not for you.
 

I'm Satalaj.