0

I am writing an application to launch webView in my activity. But it was launched in web browser.

I refer the links and this tutorial too

But When i used shouldOverrideUrlLoading, i can get only blank screen.

For that issue i refered this link

But no one help in my case..

I have given my code.

Please note what mistake I have done?

My code is:

webview.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:gravity="center_horizontal|center_vertical"
    android:orientation="vertical" >

    <WebView
        android:id="@+id/webView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center_horizontal|center_vertical" >
    </WebView>

</LinearLayout>

WebViewActivity.java

public class webViewActivity extends Activity {

    private WebView webView;
    private String strUrl;
    public final String TAG = "webViewActivity";
    ProgressBar progBar;

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        getWindow().requestFeature(Window.FEATURE_PROGRESS);
        setContentView(R.layout.webview);
        Bundle bundle = getIntent().getExtras();
        strUrl = bundle.getString("URL");
        Log.d(TAG, "link is " + strUrl);
        // strUrl = "www.google.com";
        webView = new WebView(this);
        webView.loadUrl(strUrl);

        // progBar = (ProgressBar)findViewById(R.id.progWeb);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // TODO Auto-generated method stub
                view.loadUrl(strUrl);
                return true;
            }
        });
}

Please help to corect my mistake.

Thank you in Advance!!

Community
  • 1
  • 1
Dhasneem
  • 4,037
  • 4
  • 33
  • 47

2 Answers2

3
    <WebView
    android:id="@+id/webView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center_horizontal|center_vertical" >
   </WebView>

In your activity you need to find the id of the webview.

webView = new WebView(this);

You are creating a webview but you are not adding the same to your layout

You have already defined webview in your xml so there is no need to create the same again. Find the id of the webview and load the url accordingly.

   setContentView(R.layout.webview);
   webView = (WebView) findViewById(R.id.webView);// you need to find the id of webview
   Bundle bundle = getIntent().getExtras();
   strUrl = bundle.getString("URL");      
   webView.loadUrl(strUrl);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // TODO Auto-generated method stub
            view.loadUrl(strUrl);
            return true;
        }
    }); 

Make sure you have added internet permission in manifest file

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

Check the Edit 2 in the below link for the working sample

Enable back button in WebView

Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • Thank you for your worthy reply.. It works fine.. Actually, I already tried with "webView = (WebView) findViewById(R.id.webView);", It didn't work. But i used your code. I have recognized that the change is, putting tools:context=".webViewActivity" this line in .xml file. Thank you so much. – Dhasneem Apr 23 '13 at 09:10
  • Sure.. I have a doubt. If i want progress bar to show loading means, Have i use progressbar in xml itself or using "requestWindowFeature(Window.FEATURE_PROGRESS);" this is enough? – Dhasneem Apr 23 '13 at 09:20
  • http://stackoverflow.com/questions/4331094/how-to-add-a-progress-bar-in-webview. Check the link and the accepted answer. Also check the developer site http://developer.android.com/reference/android/webkit/WebView.html – Raghunandan Apr 23 '13 at 09:28
0

I tried your code the problem is with dynamic calling of webview check that. You haven't used

          setContentView(webView);

Try this ...

kavya
  • 468
  • 2
  • 4
  • 10
  • I already used setContentView(R.layout.webview) na? Where will i put this? – Dhasneem Apr 23 '13 at 09:23
  • @kavya there is no need to create a webview like webview wv= new WebView(this); he already has a webview in xml and he has set the content of the same. – Raghunandan Apr 23 '13 at 09:26