Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android: Allow custom background color #46

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.app.Activity;
import android.app.Dialog;
import android.content.res.TypedArray;
import android.graphics.Color;

import com.facebook.react.ReactActivity;
Expand Down Expand Up @@ -35,6 +36,10 @@ public void run() {
mSplashDialog.show();
}

// Use color of "splashBackgroundColor" attribute in user theme, fallback to white if not set
TypedArray styledAttributes = activity.getTheme().obtainStyledAttributes(R.styleable.RNSplashScreen_Styleable);
final int backgroundColor = styledAttributes.getColor(R.styleable.RNSplashScreen_Styleable_splashBackgroundColor, Color.WHITE);

// If given an instance manager; ensure that we transition to the stage-2
// splash screen
// TODO: If you think of a better way to do this; PR please
Expand All @@ -43,14 +48,14 @@ public void run() {
// background state and we will not get the context created event
ReactContext ctx = instanceManager.getCurrentReactContext();
if (ctx != null) {
activity.getWindow().getDecorView().setBackgroundColor(Color.WHITE);
activity.getWindow().getDecorView().setBackgroundColor(backgroundColor);
} else {
// Else; wait until react is initialized before we release the native splash
instanceManager.addReactInstanceEventListener(new ReactInstanceManager.ReactInstanceEventListener() {
@Override
public void onReactContextInitialized(ReactContext context) {
// Hide the native splash screen
activity.getWindow().getDecorView().setBackgroundColor(Color.WHITE);
activity.getWindow().getDecorView().setBackgroundColor(backgroundColor);
}
});
}
Expand Down
5 changes: 5 additions & 0 deletions android/src/main/res/values/attr.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<resources>
<declare-styleable name="RNSplashScreen_Styleable">
<attr name="splashBackgroundColor" format="color" />
</declare-styleable>
</resources>
9 changes: 9 additions & 0 deletions docs/android.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,12 @@
// [...]
}
```

5. (OPTIONAL) In `android/app/src/main/res/values/styles.xml`: Add `splashBackgroundColor` attribute with a color you want to see under keyboard while open/close animation is happening (defaults to white).

```xml
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowBackground">@drawable/splash</item>
<item name="splashBackgroundColor">#ff0000</item>
</style>
```