Force browsing activities to stay in original WebView

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

Force browsing activities to stay in original WebView

Postby obrand » Wed Jul 02, 2008 11:25 pm

Quick question,

I have started to play with the WebView and inserted one in a
LinearLayout just below another LinearLayout containing some other
widgets (buttons, text fields...)
All work fine.
When I pass a URL to the WebView, the content gets correctly
displayed. Now when I click any links in the displayed web page, the
entire view/screen is replaced by the browser, therefore the widgets
in the upper Layout are not visible anymore.
Question: Is there a way to force ths user to browse and stay in the
original WebView rather than having the system passing it to the main
browser? Is that managed via Intents as the activity that listens for
web intents in Android is the main browser?

Thanks for any help

Olivier
obrand
Freshman
Freshman
 
Posts: 2
Joined: Wed Jul 02, 2008 11:16 pm

Top

Force WebView to stay in defined layout

Postby obrand » Thu Jul 03, 2008 5:23 am

Ok, I found the answer to my question on the android google group.
In m5, there is a new class called WebViewClient that seems to replace a callback approach in m3 for handling specific features of a WebView.

So basically, the way to do it in a search scenario would be:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.android.test.search;
  2.  
  3.  
  4.  
  5.  
  6.  
  7. import android.app.Activity;
  8.  
  9. import android.content.Context;
  10.  
  11. import android.os.Bundle;
  12.  
  13. import android.view.View;
  14.  
  15. import android.webkit.WebView;
  16.  
  17. import android.webkit.WebViewClient;
  18.  
  19. import android.widget.Button;
  20.  
  21. import android.widget.EditText;
  22.  
  23.  
  24.  
  25. public class SearchAndroid extends Activity {
  26.  
  27.     /** Called when the activity is first created. */
  28.  
  29.     @Override
  30.  
  31.     public void onCreate(Bundle icicle) {
  32.  
  33.         super.onCreate(icicle);
  34.  
  35.         setContentView(R.layout.main);
  36.  
  37.         Button convert = (Button) findViewById(R.id.search);
  38.  
  39.         convert.setOnClickListener(new Clicker(this));
  40.  
  41.         WebView webView = (WebView) findViewById(R.id.webView);
  42.  
  43.         webView.loadData(
  44.  
  45.                 "<html><body>Please click on search after entering a criteria</body></html>",
  46.  
  47.                 "text/html", "utf-8");
  48.  
  49.        
  50.  
  51.         // Force the browser to stay in the original layout
  52.  
  53.         WebViewClient wv = new WebViewClient();
  54.  
  55.         wv.shouldOverrideUrlLoading(webView, "http://www.google.com");
  56.  
  57.  
  58.  
  59.         webView.getSettings().setJavaScriptEnabled(true);
  60.  
  61.         webView.getSettings().setLoadsImagesAutomatically(true);
  62.  
  63.         webView.setWebViewClient(wv);
  64.  
  65.        
  66.  
  67.     }
  68.  
  69.  
  70.  
  71.     class Clicker implements Button.OnClickListener
  72.  
  73.  
  74.  
  75.     {
  76.  
  77.         Context context;
  78.  
  79.        
  80.  
  81.         public Clicker(Context context)
  82.  
  83.         {
  84.  
  85.             this.context = context;
  86.  
  87.         }
  88.  
  89.        
  90.  
  91.         public void onClick(View v)
  92.  
  93.         {
  94.  
  95.            
  96.  
  97.             EditText searchText = (EditText)findViewById(R.id.searchtext);
  98.  
  99.             WebView webView = (WebView) findViewById(R.id.webView);
  100.  
  101.             //searchText.setText(Integer.toString(webView.getHeight()) + "x" + Integer.toString(webView.getWidth()));
  102.  
  103.             if(searchText.length()==0)
  104.  
  105.             {
  106.  
  107.                 context.showAlert("Android Search", 0, "No search criteria", "Close", false);
  108.  
  109.             }
  110.  
  111.             else
  112.  
  113.             {
  114.  
  115.                 webView.loadUrl("http://www.google.com/search?q=" + searchText.getText().toString());
  116.  
  117.             }
  118.  
  119.         }
  120.  
  121.     }
  122.  
  123. }
Parsed in 0.038 seconds, using GeSHi 1.0.8.4
obrand
Freshman
Freshman
 
Posts: 2
Joined: Wed Jul 02, 2008 11:16 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 3 guests