Cdacians

Cdacians
Cdacians

Tuesday 14 August 2012

Override URL loading in Webview – Android Tutorial

We have an app that loads some web pages using Webview, but we want to aim the user only to specific links (or limit the user to specific links). 
I want the people using this tutorial to be able lo load onlyFirstdroid (that is us, in case you did not notice) webpages. And if they try to use links outside our homepage to do nothing in this case. For this tutorial you will need little background of how to use Webview layout, you can check that in our previous tutorials: “Using WebView with Multi-touch – Android Tutorial”

Let’s Start 

The override is very simple, you just need to add some code into the class you are using to load the URL’s into the Webview. For the purpose of giving a complete example, I’ll first show the code we need to add, and then add this code to the previous example (Click here to see example) 

Java Code 
?
WebViewClient yourWebClient = new WebViewClient()
{
   // Override page so it's load on my view only
   @Override
   public boolean shouldOverrideUrlLoading(WebView  view, String  url)
   {
         // This line we let me load only pages inside Firstdroid Webpage
         if ( url.contains("firstdroid") == true )
           //Load new URL Don't override URL Link
        return false;
             
   // Return true to override url loading (In this case do nothing).
   return true;
    }
};

and also this line 
?
mWebView.setWebViewClient(yourWebClient);

Now let’s see this the whole source file of our example: 
UsingMyWebview.java 

?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package firsrdroid.tutorial.mywebview;
 
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
 
public class UsingMyWebview extends Activity {
    
   WebView mWebView;
    
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState)
   {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main );
       
       WebViewClient yourWebClient = new WebViewClient()
       {
           // Override page so it's load on my view only
           @Override
           public boolean shouldOverrideUrlLoading(WebView  view, String  url)
           {
            // This line we let me load only pages inside Firstdroid Webpage
            if ( url.contains("firstdroid") == true )
               // Load new URL Don't override URL Link
               return false;
             
            // Return true to override url loading (In this case do nothing).
            return true;
           }
       };
       
       
       // Get Web view
       mWebView = (WebView) findViewById( R.id.MyWebview ); //This is the id you gave
       mWebView.getSettings().setJavaScriptEnabled(true);  
       mWebView.getSettings().setSupportZoom(true);       //Zoom Control on web (You don't need this
                                              //if ROM supports Multi-Touch     
       mWebView.getSettings().setBuiltInZoomControls(true); //Enable Multitouch if supported by ROM
       mWebView.setWebViewClient(yourWebClient);
        
       // Load URL
       mWebView.loadUrl("http://www.firstdroid.com/advertisement.htm");
        
   }//End of Method onCreate
}

That’s it, we finished. Now the user of your App will go only where you want them to go. :) :) :) 
Any Question or Remark, please comment 


Thanks
akm
www.cdacians.com

No comments:

Post a Comment