shouldOverrideUrlLoading not being called from webview

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

shouldOverrideUrlLoading not being called from webview

Postby JamieBennett » Tue Aug 18, 2009 9:54 pm

I'm trying to intercept links in a webview that start with http://null (which is what I'm using for local stuff) with shouldOverrideUrlLoading. Now I'm looking at sample code, topics online e.t.c and I cant for the life of me see what I am doing wrong.

The web page displays correctly but if I click a link in there which has the http://null address it says Web page not available and the Log.v printout never happens.

I have the following code:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public class WebviewHandler extends WebViewClient {
  3.  
  4.                
  5.  
  6.     @Override
  7.  
  8.     public boolean shouldOverrideUrlLoading(WebView view, String url)
  9.  
  10.     {
  11.  
  12.             Log.v("CALLED SHOULD OVERRIDE URL LOADING", url.toString());
  13.  
  14.             if (url.contains("http://null")) {
  15.  
  16.             // do something local
  17.  
  18.             return true;
  19.  
  20.         }
  21.  
  22.         else {
  23.  
  24.                 view.loadUrl(url);
  25.  
  26.             return false;
  27.  
  28.           }
  29.  
  30.     }
  31.  
  32.    
  33.  
  34.     public void DisplayWebPage(String page, WebView web) {
  35.  
  36.         web.setBackgroundColor(0);
  37.  
  38.         web.getSettings().setJavaScriptEnabled(true);
  39.  
  40.         web.getSettings().setJavaScriptEnabled(true);
  41.  
  42.         web.getSettings().setJavaScriptCanOpenWindowsAutomatically(false);
  43.  
  44.         web.getSettings().setPluginsEnabled(false);
  45.  
  46.         web.getSettings().setSupportMultipleWindows(false);
  47.  
  48.         web.getSettings().setSupportZoom(false);
  49.  
  50.         web.setVerticalScrollBarEnabled(false);
  51.  
  52.         web.setHorizontalScrollBarEnabled(false);
  53.  
  54.         web.loadData(page, "text/html", "UTF-8");
  55.  
  56.         web.setWebViewClient(new WebviewHandler());
  57.  
  58.     }
  59.  
  60. }
  61.  
  62.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


And this xml

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2.     <LinearLayout
  3.  
  4.        android:orientation="vertical"
  5.  
  6.        android:layout_width="fill_parent"
  7.  
  8.        android:layout_height="fill_parent"
  9.  
  10.        android:background="@+drawable/backgroundmain">
  11.  
  12.         <WebView
  13.  
  14.            android:id="@+id/webkit"
  15.  
  16.            android:layout_width="fill_parent"
  17.  
  18.            android:layout_height="fill_parent"
  19.  
  20.        />
  21.  
  22.     </LinearLayout>    
  23.  
  24.  
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


Idea's on what I'm doing wrong?
Last edited by JamieBennett on Wed Aug 19, 2009 7:38 am, edited 1 time in total.
JamieBennett
Freshman
Freshman
 
Posts: 4
Joined: Tue Aug 18, 2009 9:22 pm

Top

Postby sudheer1437 » Tue Aug 18, 2009 10:37 pm

inter change the return boolean values and see.

i.e. in else statement u need return "true"
and in if statement return "false".
sudheer1437
Experienced Developer
Experienced Developer
 
Posts: 79
Joined: Sat Aug 08, 2009 7:42 am
Location: India

Postby JamieBennett » Wed Aug 19, 2009 7:37 am

sudheer1437 wrote:inter change the return boolean values and see.

i.e. in else statement u need return "true"
and in if statement return "false".


But the Log.v before the if/else isn't being printed indicating that the method isn't being called at all.
JamieBennett
Freshman
Freshman
 
Posts: 4
Joined: Tue Aug 18, 2009 9:22 pm

Postby JamieBennett » Wed Aug 19, 2009 1:13 pm

Looks like this is a loadData() vs a loadUrl() problem. When I do a loadUrl() the debug code is printed out, a loadData() doesn't do it.

I need to send headers to the server and display what comes back in a webview. Not sure how to work around this one.
JamieBennett
Freshman
Freshman
 
Posts: 4
Joined: Tue Aug 18, 2009 9:22 pm

Postby sudheer1437 » Thu Aug 20, 2009 9:02 am

This is a sample code.. try it out. it might solve ur problem



Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. import android.app.Activity;
  2. import android.content.Intent;
  3. import android.os.Bundle;
  4. import android.view.KeyEvent;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. import android.webkit.WebView;
  8. import android.webkit.WebViewClient;
  9. import android.widget.Button;
  10.  
  11. public class linkView extends Activity
  12. {
  13.    
  14.     WebView webview;
  15.    
  16.    
  17.    
  18.    
  19.     @Override
  20.     public void onCreate(Bundle savedInstanceState)
  21.     {
  22.         super.onCreate(savedInstanceState);
  23.         setContentView(R.layout.webview);
  24.         webview = (WebView) findViewById(R.id.webview);
  25.         webview.setWebViewClient(new LinkWebViewClient());
  26.         webview.getSettings().setJavaScriptEnabled(true);
  27.         webview.loadUrl("http://www.google.com");
  28.    
  29.         Button next = (Button)findViewById(R.id.Button01);
  30.         next.setOnClickListener(new OnClickListener()
  31.         {
  32.                 public void onClick(View v)
  33.                 {
  34.                         Intent newIntent = new Intent();
  35.                         setResult(RESULT_OK,newIntent);
  36.                         finish();
  37.                 }
  38.        });
  39.     }
  40.     private class LinkWebViewClient extends WebViewClient
  41.     {
  42.         @Override
  43.         public boolean shouldOverrideUrlLoading(WebView view, String url)
  44.         {
  45.             view.loadUrl(url);
  46.             return true;
  47.         }
  48.     }
  49.    
  50.     public boolean onKeyDown(int keyCode, KeyEvent event)
  51.     {
  52.         if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack())
  53.         {
  54.             webview.goBack();
  55.             return true;
  56.         }
  57.         return super.onKeyDown(keyCode, event);
  58.     }
  59. }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4
sudheer1437
Experienced Developer
Experienced Developer
 
Posts: 79
Joined: Sat Aug 08, 2009 7:42 am
Location: India

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 5 guests