14

I use this code to open links with Chrome Custom Tabs. But it's showing @Deprecated for setToolbarColor() and setSecondaryToolbarColor(). I haven't found anything for replacement.

Note: Android studio suggests "Use setDefaultColorSchemeParams instead." but haven't found any examples of that.

        Uri uri = Uri.parse(url);
        CustomTabsIntent.Builder intentBuilder = new CustomTabsIntent.Builder();
        intentBuilder.setToolbarColor(ContextCompat.getColor(activity,R.color.background));
        intentBuilder.setSecondaryToolbarColor(ContextCompat.getColor(activity,R.color.background));
        intentBuilder.setStartAnimations(activity,R.anim.slide_in_right,R.anim.slide_out_left);
        intentBuilder.setExitAnimations(activity,android.R.anim.slide_in_left,android.R.anim.slide_out_right);
        CustomTabsIntent customTabsIntent = intentBuilder.build();
        customTabsIntent.launchUrl(activity,uri);

1 Answers1

22

Use CustomTabColorSchemeParams instead: Reference

Uri uri = Uri.parse(url);
CustomTabsIntent.Builder intentBuilder = new CustomTabsIntent.Builder();
CustomTabColorSchemeParams params = new CustomTabColorSchemeParams.Builder()
    .setNavigationBarColor(ContextCompat.getColor(activity,R.color.background))
    .setToolbarColor(ContextCompat.getColor(activity,R.color.background))
    .setSecondaryToolbarColor(ContextCompat.getColor(activity,R.color.background))
    .build();
intentBuilder.setColorSchemeParams(CustomTabsIntent.COLOR_SCHEME_DARK, params);
intentBuilder.setStartAnimations(activity, R.anim.slide_in_right,R.anim.slide_out_left);
intentBuilder.setExitAnimations(activity,android.R.anim.slide_in_left,android.R.anim.slide_out_right);
CustomTabsIntent customTabsIntent = intentBuilder.build();
customTabsIntent.launchUrl(activity,uri);
MLFR2kx
  • 977
  • 7
  • 16
  • Also add `.setToolbarColor(ContextCompat.getColor(activity, R.color.background))` to `params` and set `intentBuilder.setColorScheme(CustomTabsIntent.COLOR_SCHEME_DARK)`. – CoolMind Dec 14 '20 at 09:05
  • @CoolMind Thanks. I edited my answer to support `setToolbarColor`. – MLFR2kx Dec 15 '20 at 10:51
  • Have you checked the answer without `intentBuilder.setColorScheme(CustomTabsIntent.COLOR_SCHEME_DARK)` or default one? In my case it draws white Toolbar. – CoolMind Dec 15 '20 at 12:17
  • 1
    @CoolMind `CustomTabsIntent.COLOR_SCHEME_DARK` doesn't work for me and it causes crash. –  Dec 15 '20 at 13:07
  • @Arghadip, that's strange. On emulator or device (what device) with what exception? In my case it doesn't work with COLOR_SCHEME_SYSTEM. – CoolMind Dec 15 '20 at 13:09
  • @CoolMind It was Pixel 2XL [api 30] emulator. I don't remember the exception. –  Dec 16 '20 at 07:19
  • 5
    @Arghadip, I tested on Nexus 5 (API 30) emulator and device. I think we should test these cases on devices. See https://stackoverflow.com/questions/61295936/cant-change-chrome-custom-tabs-navigation-bar-color-when-running-in-the-android/61297124. Could you try `.setDefaultColorSchemeParams(params)` instead? – CoolMind Dec 16 '20 at 08:10
  • Instead of setColorSchemeParams(CustomTabsIntent.COLOR_SCHEME_DARK, params) you should try with setDefaultColorSchemeParams(params) – Talu Apr 23 '21 at 06:15