Create Simple Browser Using WebView in Android simple browser

posted Jan 25, 2011, 1:32 AM by Thiyagaraaj M   [ updated Feb 1, 2011, 7:16 AM by Thiyagaraaj Mp ]
 full example code

XML File

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

             android:text="www.google.com"/>
    <Button android:id="@+id/Go"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:text="Go"/>
    <WebView android:id="@+id/WebEngine"
             android:layout_width="fill_parent"
             android:layout_height="fill_parent"/>
</LinearLayout>

Java Code

package com.example.browserapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;

public class Browser extends Activity {
    /** Called when the activity is first created. */
EditText URLText;
Button GoButton;
WebView Browser;
private class MyWebViewClient extends WebViewClient {
   @Override
   public boolean shouldOverrideUrlLoading(WebView view, String url) {
       view.loadUrl(url);
       return true;
   }
}
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
         
        URLText = (EditText)findViewById(R.id.URL);
        GoButton = (Button)findViewById(R.id.Go);
        Browser = (WebView) findViewById(R.id.WebEngine);
        Browser.loadUrl("http://www.google.co.in/");
      
         GoButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Browser.setWebViewClient(new MyWebViewClient());
Browser.loadUrl("http://"+URLText.getText().toString());
}
});
    }
}

Add in Manifest File

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

Screen shot



List Of Articles


Comments