Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open link from Android Webview in normal browser as popup

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?

like image 324
MartijnG Avatar asked Dec 16 '11 21:12

MartijnG


People also ask

What is WebView link?

WebView is a view that display web pages inside your application. You can also specify HTML string and can show it inside your application using WebView. WebView makes turns your application to a web application.


2 Answers

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;
        }
    }
}
like image 150
Blundell Avatar answered Sep 25 '22 23:09

Blundell


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;
        }
    }
}
like image 37
praveen s Avatar answered Sep 24 '22 23:09

praveen s