Wednesday, 26 October 2011

Creating Web Browser for Android


Designing Part:




Layout file: Main.xml
 

<?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"
    android:weightSum="1" android:padding="5dip"    android:background="@color/White">
<RelativeLayout android:layout_width="match_parent"
    android:id="@+id/relativeLayout1"
    android:layout_height="wrap_content"
    android:layout_weight="1.20"
    android:background="@color/White">
<Button android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/editText1"
    android:layout_alignParentRight="true"    android:background="@drawable/ymbutton"
    android:layout_alignParentTop="true"></Button>
<EditText android:layout_width="204dp"
    android:layout_height="wrap_content"
    android:id="@+id/editText1"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_toLeftOf="@+id/button1" 
    android:hint="Enter URL......"></EditText>
<WebView android:layout_width="match_parent"
    android:id="@+id/webView1"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/editText1"
    android:background="@drawable/internet"></WebView>
   
</RelativeLayout>
   
</LinearLayout>

Coding Part:

package com.murali.webview;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;

public class WebActivity1Activity extends Activity {
    /** Called when the activity is first created. */
        final Activity activity=this;
           
        @Override
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
        setContentView(R.layout.main);
           
        final EditText urltxt=(EditText) findViewById(R.id.editText1);
        Button click=(Button) findViewById(R.id.button1);
           
         click.setOnClickListener(new OnClickListener() {
                 
         @Override
         public void onClick(View v) {
                       
//.................this  part for webview ...........................
                       
        String urlID=urltxt.getText().toString();
        WebView webView = (WebView) findViewById(R.id.webView1);

        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl("http://"+urlID+".com");

//-----------------This part for show the Progress of page loading------ 
 
        webView.setWebChromeClient(new WebChromeClient()
        {
        @Override
        public void onProgressChanged(WebView view, int Progress) {
                             
            activity.setTitle("loading....");
            activity.setProgress(Progress*100);
           
            if(Progress==100)
               {
                activity.setTitle(R.string.app_name);
               }
                         
             }
          });
        }
      });
           
//---------------------------------------------------------------------------
           
  }

}

No comments:

Post a Comment