Wednesday 30 October 2013

WebView Example

A View that displays web pages. This class is the basis upon which you can roll your own web browser or simply display some online content within your Activity. It uses the WebKit rendering engine to display web pages and includes methods to navigate forward and backward through a history, zoom in and out, perform text searches and more.

Example:






Code: 

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

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

        <EditText
            android:id="@+id/editText1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_weight="1"
            android:ems="10"
            android:hint="eg. www.google.com" >

            <requestFocus />
        </EditText>

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dp"
            android:layout_weight="1"
            android:text="Go"
            android:textStyle="bold" />

    </LinearLayout>

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

</LinearLayout>


MainActivity.java

package in.androiddevelopmentanddiscussion.webviewexample;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.annotation.SuppressLint;
import android.app.Activity;

public class MainActivity extends Activity {

WebView webView;
String url;
EditText weburl;
Button go;


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

weburl = (EditText)findViewById(R.id.editText1);
webView = (WebView) findViewById(R.id.webView1);
go = (Button)findViewById(R.id.button1);


go.setOnClickListener(new OnClickListener() {

@SuppressLint("SetJavaScriptEnabled")
@Override
public void onClick(View v) {

url = weburl.getText().toString();

  webView.setWebViewClient(new callback());
webView.getSettings().setJavaScriptEnabled(true);
WebSettings webSetting = webView.getSettings();
webSetting.setBuiltInZoomControls(true);
webView.loadUrl("http://"+url);
}
});
}

private class callback extends WebViewClient
{

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

}


}




Manifest

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


Download full project from here



Check Android Apps on Google Play

https://play.google.com/store/apps/developer?id=Metro%20App%20Solution&hl=en

 

No comments:

Post a Comment