Handling External Links in android WebView like Gmail App does

前端 未结 1 1950
余生分开走
余生分开走 2021-02-14 16:35

I\'m a web developer. I\'m currently developing android application on Android Studio using WebView which access my website as an android application. One of my webpage contains

1条回答
  •  深忆病人
    2021-02-14 16:55

    It is pretty simple. You have to use Chrome Custom Tabs as suggested by Gergely as well in comment. Below is the small functional code that will help you to achieve this.

    First add this dependency to your build.gradle(Module:app)

    compile 'com.android.support:customtabs:23.4.0'
    

    Second add below function to your code and simply pass string URL to it.

    private void redirectUsingCustomTab(String url)
    {
        Uri uri = Uri.parse(url);
    
        CustomTabsIntent.Builder intentBuilder = new CustomTabsIntent.Builder();
    
        // set desired toolbar colors
        intentBuilder.setToolbarColor(ContextCompat.getColor(this, R.color.colorPrimary));
        intentBuilder.setSecondaryToolbarColor(ContextCompat.getColor(this, R.color.colorPrimaryDark));
    
        // add start and exit animations if you want(optional)
        /*intentBuilder.setStartAnimations(this, android.R.anim.slide_in_left, android.R.anim.slide_out_right);
        intentBuilder.setExitAnimations(this, android.R.anim.slide_in_left,
                android.R.anim.slide_out_right);*/
    
        CustomTabsIntent customTabsIntent = intentBuilder.build();
    
        customTabsIntent.launchUrl(activity, uri);
    }
    

    Rest it will take care itself. Since Chrome Custom Tabs can customised so lot can be done like you can add menu to toolbar. For detailed information you can visit official documentation from Google itself here.

    Hope it will help you to start with :)

    0 讨论(0)
提交回复
热议问题