skip to Main Content

I convert WordPress website to android app, in my website lots of external links i need to open external links browser like telegram, in telegram all external links open in external browser in the app

3

Answers


  1. Chosen as BEST ANSWER

    Problem SOlved guys thanks for your replays

    Source (from sven)

    webView.setWebViewClient(new WebViewClient(){
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url != null && (url.startsWith("http://") || url.startsWith("https://"))) {
            view.getContext().startActivity(
                new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
            return true;
        } else {
            return false;
        }
    }
    

    });


  2. You can add a WebViewClient to your webview and override the shouldOverrideUrlLoading method. This will give you a callback when a link is clicked and you can inspect the url and decide if you want to open in the webview or open it in the external browser.

    For example something like:

    private class MyCustomWebClient : WebViewClient() {
    
            @TargetApi(Build.VERSION_CODES.N)
            override fun shouldOverrideUrlLoading(
                view: WebView?,
                request: WebResourceRequest?
            ) = if (interceptUrlLoading(request?.url?.toString())) {
                true
            } else {
                super.shouldOverrideUrlLoading(view, request)
            }
    
            override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean =
                if (interceptUrlLoading(url)) {
                    true
                } else {
                    super.shouldOverrideUrlLoading(view, url)
                }
    
            private fun interceptUrlLoading(url: String?): Boolean {
                return if (url.equals("telegram url ...")) {
                    startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(url))) 
                    true
                } else {
                   false
                }
            }
    }
    
    
    Login or Signup to reply.
  3. As @peshkira has given a good solution, but I would suggest to use CustomTab.

    With Custom Tabs you can get instance of browser without creating a web view. As it provide navigation awareness, the browser delivers a callback to the application upon an external navigation. You can modify and update following –

    Custom menu

    Color of the address bar

    Custom action button

    Custom enter and exit animations

    Setup for CustomTab –

    dependencies {
        ...
        implementation "androidx.browser:browser:1.2.0"
    }
    

    In your code, Open a CustomTab –

    String url = ¨https://paul.kinlan.me/¨;
    CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
    CustomTabsIntent customTabsIntent = builder.build();
    customTabsIntent.launchUrl(this, Uri.parse(url));
    

    You can save up to 700 ms when opening a link with the Custom Tabs by connecting to the service and pre-loading the browser.

    Most browser do support CustomTab like Chrome, Samsung Mobile Broswer, Microsoft Edge etc.
    Please follow Custom Tabs Best Practices. Here is a gitHub example.


    Edit

    As you mentioned in your comment "every post have 1 external link", then you following options –

    1. Create a WebView to open link
    2. Use intent to open device external browser
    3. Use CustomTab to open instance of browser

    I think behavior of all three is very well explained via this gif.

    Now, to show all the posts in your app, I believe you are using a RecycleView, so you can decide from three choices, you can listen for click on one of the post by using a click listener on that post, something like follows –

    postItem.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            handleClickOnLink(postUrl);
        }
    });
    

    Now in the body of your method handleClickOnLink(String postUrl) you can use your code to decide how you want open the link.

    As I have mentioned above, Using CustomTab will be one of the choices, which I think will be a good option in this case, so you can have an implementation of method as follows –

    public void handleClickOnLink(String postUrl){
        CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
    
        //setting toolbar color 
        builder.setToolbarColor(getContext().getColor(R.color.colorPrimaryDark));
        //can be more modification as builer.setTitle() and more
    
        CustomTabsIntent customTabsIntent = builder.build();
        customTabsIntent.launchUrl(getContext(), Uri.parse(postUrl));
    }
    

    If there is a device with a browser which doesn’t support CustomTab then device default browser will be open.

    Result will be something as follows –

    enter image description here
    Happy Coding !

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search