0

I have an application showing a WebView which shows information that includes street addresses like "123 Main St., Citytown, NY". However, when any of these addresses are tapped, it highlights briefly and the usual browser behavior of launching the Google Maps app is triggered.

I would like to prevent that behavior from occurring because some of the addresses aren't meant to be selectable. Is there anything I can do?


Update:

A commenter asked me to paste an example HTML snippet that triggers the behavior.

<hgroup class="unit list_item_body">
  <h2 class="thick truncated heading">
    <a href="/locations/1234">Foobar</a>
  </h2>
  <h3 class="truncated subheading">
    123 East Market St., Charlottesville, VA, 22902
  </h3>
</hgroup>

Notice there's no link on the address. Nevertheless, tapping the address triggers the behavior of launching Maps. This occurs whether I'm accessing the site through the WebView or viewing the site itself.

John Feminella
  • 303,634
  • 46
  • 339
  • 357
  • If it can be done by javascript code, you can try this: http://stackoverflow.com/questions/8533187/add-javascript-into-webview – vortexwolf Apr 25 '12 at 11:54

2 Answers2

2

I figured this out. This meta tag will preclude the browser controls from hijacking address strings:

<meta name="format-detection" content="address=no">
John Feminella
  • 303,634
  • 46
  • 339
  • 357
0

Take a look at WebViewClient.shouldOverrideUrlLoading

webView.setWebViewClient(new WebViewClient() {
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        boolean shouldHandle = true;
        //Check if you want to override the loading of the URL and set shouldHandle accordingly
        return shouldHandle ;
    }
});
Rajesh
  • 15,724
  • 7
  • 46
  • 95
  • Hmm... this doesn't seem right. There isn't a different URL being invoked when one clicks on an address versus the parent container. – John Feminella Apr 25 '12 at 13:14
  • When you say "some of the addresses aren't meant to be selectable" how do you distinguish between what should be selectable and what should not? – Rajesh Apr 25 '12 at 13:18
  • None of them should be "selectable" in the sense that they should go to Google Maps. For example, if there's a list of event venues with their addresses, clicking on any venue or its address takes you to the event venue page. But in this case, if you happen to click the address part of the search result, it takes you to Google Maps, which is the behavior I'd like to avoid. Does that make sense? – John Feminella Apr 25 '12 at 13:21
  • Can you please paste the code of html that is loaded in the WebView? Typically, the url that opens a Google Maps application should be identifiable - for example by the geo: url scheme. – Rajesh Apr 25 '12 at 13:25
  • I added a snippet above that exhibits the behavior. – John Feminella Apr 25 '12 at 14:30