Search code examples
androidtrusted-web-activity

Android Change StatusBarColor of Trusted Web Activity


I build based on this repository my first TWA / PWA App. Everything is working fine, but i can´t change the color of the statusbar.

I modify this file and add this line in the <style> tag:

<item name="android:statusBarColor">@color/ic_launcher_background</item>

The thing is... it works on the first init of the App well... But 500ms after first init it starts the webview and the statusBarColor is white again.

Any idea how i can fix this?


Solution

  • Updated: The latest version of the Support Library (e849e45c90) has been updated to make it easier to change the status bar color.

    The SVGOMG sample has been updated to use it, and the necessary changes for apps to make it work can be seen in this pull request.

    The section below is outdated, but leaving here for historical context

    It is possible to change the status bar color by customising it when opening the Custom Tabs Intent.

    This is not currently configurable in the manifest, and the main way to do it is:

    1. Copy the LauncherActivity from the support library repo into your project.
    2. change the reference in the AndroidManifest.xml to your copy of the implementation.
    3. Tweak your LauncherActivity code to setup the status bar, by replacing the getCustomTabsIntent method with something like the code below:
        protected CustomTabsIntent getCustomTabsIntent(CustomTabsSession session) {
            return new CustomTabsIntent.Builder(session)
              .setToolbarColor(Color.parseColor("#FF0000"))
              .build();
        }
    

    The code above will create a red status bar. Replace #FF0000 with the desire color.