13

I have a simple webview which loads a page. This page has a few links that opens within the webview. That's what it supposed to do, so it's working all fine.

But there is one single link from that page which should load as a popup, so I want it to open in the normal browser when people click it. But as I stated, all links are opening in the webview, so that link does it also.

My question is, how can I make this link open in the normal browser as a kind of a popup? Is it even possible? The link is variable so it's changing always, it cannot be hardcoded within the application to open in a new browser browser.

Is it possible and how can I do it?

Jason Aller
  • 3,391
  • 28
  • 37
  • 36
MartijnG
  • 795
  • 3
  • 11
  • 24
  • Use intent; please see: http://stackoverflow.com/questions/2201917/how-can-i-open-a-url-in-androids-web-browser-from-my-application – Ozzy Dec 16 '11 at 21:28
  • 1
    I know you say you can't hard code it, but whats changing about the link surely something is coded that you can rely on? Then just override onUrlLoading and pass that url to the super but deal with the others. I can give you an example if needed? – Blundell Dec 16 '11 at 21:31
  • @user1031312 Haha of course not, I'm not stupid! Thank you for the answer, I will take a look at it tomorrow! – MartijnG Dec 16 '11 at 22:15
  • @Blundell It's a link to share my app on facebook etc but every link has an own id to give me an impression how many times the app gets shared. It's hard to explain but it isn't advertising etc it's free to click or not for the user. If you have time I would like to see an example! Thank you!! – MartijnG Dec 16 '11 at 22:18
  • Make a HttpRequest instead to achieve this. Bind some user data to the request to your php page, the php page can then store the information into a database. I'll post you an example of the Android-side – Ozzy Dec 16 '11 at 22:27
  • @MartijnG this still sounds pretty simple, what is the url as the unique id will just be at the end? I'll post the answer then – Blundell Dec 16 '11 at 23:57
  • The page does not exist yet but lets say it will be share.php?id=randid so lets say http://www.site.com/share.php?id=randid – MartijnG Dec 17 '11 at 00:08

2 Answers2

28

Here's an example of overriding webview loading to stay within your webview or to leave:

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class TestWebViewActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        WebView webView = (WebView) findViewById(R.id.webview);
        webView.setWebViewClient(new MyWebViewClient());
    }
}


class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if(url.contains("somePartOfYourUniqueUrl")){ // Could be cleverer and use a regex
            return super.shouldOverrideUrlLoading(view, url); // Leave webview and use browser
        } else {
            view.loadUrl(url); // Stay within this webview and load url
            return true;
        }
    }
}
Blundell
  • 69,653
  • 29
  • 191
  • 215
  • Thank you very much! I think this is exactly what I'm looking for. I will test this tomorrow, first I have to get some sleep as it's 1.15 am here right now ;) – MartijnG Dec 17 '11 at 00:14
  • Yeah me too, addicts :p haha . Yeah so you could put "share.php" within that contains if statement. Just be careful nothing else uses it. – Blundell Dec 17 '11 at 00:22
5
public class WebViewActivity extends Activity {

    private WebView webView;
    private ProgressDialog progress;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webview);
        WebView myWebView = (WebView) findViewById(R.id.webView1);
        myWebView.setWebViewClient(new MyWebViewClient());
        myWebView.loadUrl("https://www.example.com");
    }

    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (Uri.parse(url).getHost().equals("https://www.example.com")) {
                // This is my web site, so do not override; let my WebView load the page
                return false;
            }
            // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(intent);
            return true;
        }
    }
}
Satan Pandeya
  • 3,460
  • 4
  • 22
  • 47
praveen s
  • 190
  • 2
  • 8