3

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?

andreban
  • 4,621
  • 1
  • 20
  • 49
Budi
  • 678
  • 2
  • 6
  • 27

3 Answers3

4

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.

andreban
  • 4,621
  • 1
  • 20
  • 49
  • i need to ask this because its the first time i work with java / android studio. Is there any common place where i copy the `LauncherActivity`? i should place it in `app/src/main/LauncherActivity.java`? And i cant find any path in the `AndroidManifest.xml` how i can change the reference to the copy? – Budi Feb 14 '19 at 12:07
  • If your application package name is com.example, the source would be under `app/src/main/java/com/example/LauncherActivity.java`. – andreban Feb 14 '19 at 12:10
  • On the `activity` tag inside AndroidManifest.xml, you'd have to change: `android:name=com.example.LauncherActivity` – andreban Feb 14 '19 at 12:13
  • It is normal that `com.devMonkeys.expenser` gets automaticly refactored by android studio to `com.expenser`? Also edit your post please and explain that people need to also change the `package` name on Line `15` in `LauncherActivity.java` :) And in the function you posted i get the error: `cannot resolve symbol Color` other stuff seems looks fine. – Budi Feb 14 '19 at 12:48
  • re: Color, did you add `import android.graphics.Color;` below the package declaration, at the top of the file? – andreban Feb 14 '19 at 13:37
  • That helps :) Last problem i see in LauncherActivity: `error: cannnot find symbol variable TrustedWebActivityService`. – Budi Feb 14 '19 at 13:43
  • Ok i got it. This import was missing: `import android.support.customtabs.trusted.TrustedWebActivityService;` Now it works well :) Thank you alot andreban for you awesome very extended help and support! I am sure this topic will help alot people :) Have you maybe a link to a documentation with customization options for the Launcher Activity? – Budi Feb 14 '19 at 20:02
4

You should add new meta-data on your AndroidManifest where have you declared Trusted Web Activity (android.support.customtabs.trusted.STATUS_BAR_COLOR)

 <activity android:name="android.support.customtabs.trusted.LauncherActivity">
    <meta-data
        android:name="android.support.customtabs.trusted.DEFAULT_URL"
        android:value="https://your-host.com/" />
    <meta-data
        android:name="android.support.customtabs.trusted.STATUS_BAR_COLOR"
        android:resource="@color/colorPrimaryDark" />
    <intent-filter>
        <action android:name="android.intent.action.VIEW"/>
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE"/>
        <data
            android:scheme="https"
            android:host="your-host.com"/>
    </intent-filter>
</activity>
Montse Garcia
  • 327
  • 1
  • 3
  • 7
  • This is not needed any more. @andreban has added a config entry for this in the TWA Repository – Budi Mar 28 '19 at 14:38
0

try to add this code on onResume()

public void setStatusBarColor(int color) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().setStatusBarColor(color);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }
}
Imran khan
  • 123
  • 1
  • 12