Search code examples
androidwebviewandroid-webview

Can someone give one exact example of webview implementation in android


Hi I am developing android application using WebView implementation.

I followed the official android tutorial.

I am not getting errors when building my project in eclipse, but when running the application :

the application has stopped working unexpectedly


Solution

  • Your Java file should be like this:

    public class WebViewSampleActivity extends Activity {
         WebView wb;
        private class HelloWebViewClient extends WebViewClient {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return false;
            }
        }
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);      
            wb=(WebView)findViewById(R.id.webView1);        
            wb.getSettings().setJavaScriptEnabled(true);
            wb.getSettings().setLoadWithOverviewMode(true);
            wb.getSettings().setUseWideViewPort(true);
            wb.getSettings().setBuiltInZoomControls(true);
            wb.getSettings().setPluginState(WebSettings.PluginState.ON);
            wb.getSettings().setPluginsEnabled(true);           
            wb.setWebViewClient(new HelloWebViewClient());
            wb.loadUrl("http://www.examplefoo.com");        
        }
    }
    

    Your xml file should be like this:

    <?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:layout_gravity="center">
    
        <WebView
            android:id="@+id/webView1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_gravity="center" />
    
    </LinearLayout>
    

    Use the following in your manifest after <uses-sdk><uses-sdk/>:

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