Thursday 12 December 2013

Android Browser

Android Browser 

Hi Viewer
         Toady i am going to explain how to create own browser in android.
Android Web view facilitate you to open any URL or customize html page in own application window.
Android support web kit engine for web view.

Below i have explain use of web view and its related method with the help of very simple example.


Android Java srouce code:-


package in.AndroidShivendra.webviewex;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {
WebView wv;

Button btn1, btn2, btn3, btn4,btn5;
EditText et ;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


btn1 = (Button) findViewById(R.id.button1);

et = (EditText)findViewById(R.id.editText1);


wv = (WebView) findViewById(R.id.webView1);
wv.loadUrl("file:///android_asset/web.html");// opening local html file from assests folder
wv.setWebViewClient(new webCont());

btn1.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String url = et.getText().toString();
wv.loadUrl("http://"+url );
}
});

btn5= (Button) findViewById(R.id.button5);
btn5.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish(); // Exit from Applcation
}
});


btn4 = (Button) findViewById(R.id.button4);
btn4.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
wv.reload(); // To page reload
}
});

btn3 = (Button) findViewById(R.id.button3);
btn3.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
wv.goForward(); // Forward traverse

}
});

btn2 = (Button) findViewById(R.id.button2);
btn2.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
wv.goBack(); //backward traverse
}
});

wv.requestFocus(View.FOCUS_DOWN);
        wv.setOnTouchListener(new View.OnTouchListener() {
     

@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                case MotionEvent.ACTION_UP:
                    if (!v.hasFocus()) {
                        v.requestFocus();
                    }
                    break;
            }
return false;
}
        });

}

class webCont extends WebViewClient
{

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);

return true;


}



}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

Android Layout file Code:-


    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

            android:id="@+id/linearLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="43dp"
        android:orientation="vertical" >
   


            android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button1"
        android:layout_alignBottom="@+id/button1"
        android:layout_alignParentRight="true"
        android:ems="10"
        android:hint="www.google.com" />

   


OututScreen :-






You Can Download source code here

No comments:

Today's Pageviews