Skip to content

Instantly share code, notes, and snippets.

@andreban
Created February 10, 2019 12:07
Show Gist options
  • Save andreban/e0e10588b8cfe7931993822291ef3245 to your computer and use it in GitHub Desktop.
Save andreban/e0e10588b8cfe7931993822291ef3245 to your computer and use it in GitHub Desktop.
Changing Status Bar colour when launching a TWA
// Copy LauncherActivity from the support library repo
// (https://github.com/GoogleChrome/custom-tabs-client/blob/master/customtabs/src/android/support/customtabs/trusted/LauncherActivity.java#L153-L155)
// and replace the getCustomTabsIntent method, as below:
protected CustomTabsIntent getCustomTabsIntent(CustomTabsSession session) {
return new CustomTabsIntent.Builder(session)
.setToolbarColor(Color.parseColor("#000000"))
.build();
}
@rejhgadellaa
Copy link

rejhgadellaa commented Feb 11, 2019

Thanks, this works and sets the status bar color. It does leave the navigation bar white but there doesn't seem to be an API for that so I guess I'll file a bug/feature request.

That said. I ran into an(other) issue.

Not sure if this is the place and I may be hijacking your gist here but for some reason it now launches the splash activity (that's the one I have set as Launcher Activity), then shoves a Custom Tab (with UI) in front of my app with www.example.com. If I close that (press X or Back) I get my TWA.

I had a look at the android.support.customtabs.trusted.LauncherActivity class and figured it was returning the fallback value for some reason (See getLaunchingUrl(), return Uri.parse("https://www.example.com/");). But when I override the method and print super.getLaunchingUrl() it turns out it is returning the correct URL.

Any clue what is going on? Thanks!

Manifest:

<!-- Splash activity -->
<activity
	android:name=".Splash"
	android:theme="@style/AppTheme.NoActionBar.Splash">
	<!-- Launcher activity -->
	<intent-filter>
		<action android:name="android.intent.action.MAIN" />
		<category android:name="android.intent.category.LAUNCHER" />
	</intent-filter>
</activity>

<!-- TWA Activity -->
<activity
	android:name=".WebActivity"
	android:theme="@style/AppTheme.NoActionBar">
	<!-- The url opened by the TWA -->
	<meta-data
		android:name="android.support.customtabs.trusted.DEFAULT_URL"
		android:value="https://icerrr2.rejh.nl/?" />
	<!-- Allows the TWA to handle Intents to open the domain -->
	<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:host="icerrr2.rejh.nl"
			android:scheme="https" />
	</intent-filter>
</activity>

Splash.java:

public class Splash extends LauncherActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        Intent webActivityIntent = new Intent(this, WebActivity.class);
        webActivityIntent.setData(Uri.parse("https://icerrr2.rejh.nl/?"));
        startActivity(webActivityIntent);
        finish();

    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(0, R.transition.activity_fade_out);
    }

}

WebActivity.java:

public class WebActivity extends LauncherActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected CustomTabsIntent getCustomTabsIntent(CustomTabsSession session) {
        return new CustomTabsIntent.Builder(session)
            .setToolbarColor(getResources().getColor(R.color.colorPrimary))
            .build();
    }

    @Override
    protected Uri getLaunchingUrl() {
        Uri uri = super.getLaunchingUrl();
        Log.d(Constants.APPTAG, " -> super.getLaunchingUrl(): "+ uri.toString());
        return uri;
    }

}

@andreban
Copy link
Author

You could override the getLaunchingUrl to return Uri.parse("https://www.example.com/");

Do you mind posting the question on StackOverflow? (https://stackoverflow.com/questions/tagged/trusted-web-activity/). It will make easer for other developers with the same issue to find the answer (and other people can help with the answer too).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment