thebylito / react-native-navigation-bar-color

React Native component to change bottom bar/navigation bar color on Android
MIT License
269 stars 49 forks source link

App is crashing if the color is 'transparent' #25

Closed pavermakov closed 4 years ago

pavermakov commented 4 years ago

changeNavigationBarColor('transparent') or changeNavigationBarColor('rgba(0, 0, 0, 0)') causes the app to crash.

thebylito commented 4 years ago

See #19

nikolowry commented 4 years ago

Left this comment in #19 a few days ago, but figured I'd drop it here as well since that issue is already closed:

@thebylito any chance of changeNavigationBarColor programmatically applying the translucent flag if the value of the color arg is set to translucent?

Here's react-native-onscreen-navbar-fork's implementation for reference https://github.com/seahorsepip/react-native-onscreen-navbar/blob/master/android/src/main/java/com/seapip/thomas/navigationbar/NavigationBarModule.java#L100

This is the only react-native Navigation Bar package that is actively maintained, so it would be great if this feature was officially supported

Since writing that comment, I've started to use the following patch via patch-package:

diff --git a/node_modules/react-native-navigation-bar-color/android/src/main/java/com/thebylito/navigationbarcolor/NavigationBarColorModule.java b/node_modules/react-native-navigation-bar-color/android/src/main/java/com/thebylito/navigationbarcolor/NavigationBarColorModule.java
index 822ca90..eea15d0 100644
--- a/node_modules/react-native-navigation-bar-color/android/src/main/java/com/thebylito/navigationbarcolor/NavigationBarColorModule.java
+++ b/node_modules/react-native-navigation-bar-color/android/src/main/java/com/thebylito/navigationbarcolor/NavigationBarColorModule.java
@@ -85,26 +85,38 @@ public class NavigationBarColorModule extends ReactContextBaseJavaModule {
                         if (getCurrentActivity() != null) {
                             final Window window = getCurrentActivity().getWindow();

-                            if (!animated) {
-                              window.setNavigationBarColor(Color.parseColor(String.valueOf(color)));
+                            if (color.equals("translucent")) {
+                                window.setFlags(
+                                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION,
+                                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
                             } else {
-                              Integer colorFrom = window.getNavigationBarColor();
-                              Integer colorTo = Color.parseColor(String.valueOf(color));
-                              //window.setNavigationBarColor(colorTo);
-                              ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo);
-                              colorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
-
-                                  @Override
-                                  public void onAnimationUpdate(ValueAnimator animator) {
-                                      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
-                                          window.setNavigationBarColor((Integer) animator.getAnimatedValue());
-                                      }
-                                  }
-
-                              });
-                              colorAnimation.start();
+                                WindowManager.LayoutParams attrs
+                                    = window.getAttributes();
+                                attrs.flags &= (~WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
+                                window.setAttributes(attrs);
+                                window.clearFlags(
+                                    WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
+
+                                if (!animated) {
+                                    window.setNavigationBarColor(Color.parseColor(String.valueOf(color)));
+                                } else {
+                                    Integer colorFrom = window.getNavigationBarColor();
+                                    Integer colorTo = Color.parseColor(String.valueOf(color));
+                                    //window.setNavigationBarColor(colorTo);
+                                    ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo);
+                                    colorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
+
+                                        @Override
+                                        public void onAnimationUpdate(ValueAnimator animator) {
+                                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
+                                                window.setNavigationBarColor((Integer) animator.getAnimatedValue());
+                                            }
+                                        }
+
+                                    });
+                                    colorAnimation.start();
+                                }
                             }
-
                             setNavigationBarTheme(getCurrentActivity(), light);

                             WritableMap map = Arguments.createMap();
thebylito commented 4 years ago

Hi, in new version 2.0.0 you can use "transparent" and "translucent" colors :)