Search code examples
androidandroid-webviewandroid-toolbar

Android WebView Activity Toolbar


In my app, I have a WebView Activity that loads a URL.

I have a custom Toolbar, and I want it to show the title of the page, the URL and if it is a secure connection or not.

I have observed that several famous apps (Twitter, Youtube, Telegram...) have this exactly same Toolbar model for their WebView activities, and I wonder if Android has a default toolbar for this, or if they have built the same custom toolbar.

And, if it is the second case (I have to build the custom toolbar myself), I can access the page title overriding the onPageFinished method of the WebViewClient, but how can I get if the connection is secure or not?

I include a snapshot of the Telegram toolbar I am talking about (it is the same for the other apps):

enter image description here

Thank you very much and sorry about my English!


Solution

  • To show web url in this kind of view, you don't need WebViewActivity.

    This can be achieved using Chrome's custom tabs.

    To achieve this, follow these steps: (1) add dependency in your build.gradle -> compile 'com.android.support:customtabs:23.3.0'

    (2) write this method in some some utility class

        public static void openUrlInChromeCustomTab(Context context, String url) {
        try {
            CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
            CustomTabsIntent customTabsIntent = builder.build();
            customTabsIntent.intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            customTabsIntent.launchUrl(context, Uri.parse(url));
        } catch (ActivityNotFoundException e) {
            // might not available.
            //openUrlLinkInWebView(context, url);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    

    (3) call this method from where you want to open link like : openUrlInChromeCustomTab(activity, url");

    that's all.

    Also, you can customise it with whatever you needs. learn more at https://developer.chrome.com/multidevice/android/customtabs