Home » Android » android – Is it possible to have Chrome custom tabs as View object

android – Is it possible to have Chrome custom tabs as View object

Posted by: admin May 14, 2020 Leave a comment

Questions:

Currently, this is how I implement Chrome custom tabs

String url = "http://www.google.com/";
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(WelcomeFragment.this.getActivity(), Uri.parse(url));

I was wondering, is it possible to have Chrome custom tabs as View object?

The reason I’m asking so that that, previously, I have a fragment, which is having ViewAnimator object. ViewAnimator will in-turn animate between 2 WebViews.

One WevView is displaying mobile version of the web page. Another WebView is displaying desktop version of the web page.

Here’s the code which used to alternate between the 2 WebViews

public void updateWebView() {
    int index = getCurrentWebViewHolderIndex();
    final WebViewHolder webViewHolder = webViewHolders[index];
    if (webViewHolder == null) {
        return;
    }

    final WebView webView = webViewHolder.webView;

    boolean loadUrl = false;
    boolean reload = false;

    synchronized (monitor) {
        if (false == webViewHolder.loadUrl) {
            webViewHolder.loadUrl = true;
            loadUrl = true;
        } else if (webViewHolder.error) {
            webViewHolder.error = false;
            reload = true;
        }
    }

    if (loadUrl) {
        webView.loadUrl(getUrl(index));
    } else if (reload) {
        webView.reload();
    }

    final WebViewFragmentActivity activity = (WebViewFragmentActivity)WebViewFragment.this.getActivity();
    if (activity != null) {
        final int progress = webViewHolder.progress;

        if (progress >= 100) {
            activity.setProgressBarVisibilityEx(false);
        } else {
            activity.setProgressBarVisibilityEx(true);
            activity.setProgressEx(progress);
        }
    }

    if (index == 0) {
        // Slide from left.
        Animation slideInLeftFast = AnimationUtils.loadAnimation(this.getActivity(), R.anim.slide_in_left_fast);
        Animation slideOutRightSlow = AnimationUtils.loadAnimation(this.getActivity(), R.anim.slide_out_right_slow);
        this.webViewViewAnimator.setInAnimation(slideInLeftFast);
        this.webViewViewAnimator.setOutAnimation(slideOutRightSlow);
    } else {
        // Slide from right.
        Animation slideInRightFast = AnimationUtils.loadAnimation(this.getActivity(), R.anim.slide_in_right_fast);
        Animation slideOutLeftSlow = AnimationUtils.loadAnimation(this.getActivity(), R.anim.slide_out_left_slow);
        this.webViewViewAnimator.setInAnimation(slideInRightFast);
        this.webViewViewAnimator.setOutAnimation(slideOutLeftSlow);
    }

    if (webViewViewAnimator.getChildCount() >= 2) {
        webViewViewAnimator.removeViewAt(0);
    }
    webViewViewAnimator.addView(webView);
    webViewViewAnimator.setDisplayedChild(webViewViewAnimator.getChildCount() - 1);
}

Here’s the XML code

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/web_view_linear_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center_horizontal" >

    <ViewAnimator
        android:id="@+id/web_view_view_animator"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
    />

</LinearLayout>

I was told that Chrome Custom Tabs is having a much better performance than WebView.

However, I don’t find a way, to let single fragment holding 2 different Chrome Custom Tabs. As, they are Intent, not View.

But, is there any way, to have Chrome custom tabs as View object?

How to&Answers: