React Native component to change bottom bar/navigation bar to be transparent on Android
npm install react-native-immersive-bars
react-native-button-toggle-group
react-native-safe-area-context package in order
npm i --save react-native-immersive-bars
`
Or
`
yarn add react-native-immersive-bars
`
$3
If you're targeting API 29+ in your React Native app, you need to do one more step to enable the fully transparent bars. Add:
`xml
- false
`
To:
`
android\app\src\main\res\values\styles.xml
`
Usage
$3
`jsx
import {changeBarColors} from 'react-native-immersive-bars';
// ...
React.useEffect(() => {
changeBarColors(isDarkMode, '#50000000', 'transparent');
// or changeBarColors(isDarkMode);
}, [isDarkMode]);
`
The changeBarColors function has a single required parameter and two optional ones.
- isDarkMode (Required): If the app is in dark mode or not - will apply proper styling to icons and statusbar/navbar background
- translucentLightStr (Optional): When a translucent bar must be drawn (due to API restrictions), what color it should be drawn in light mode
- translucentDarkStr (Optional): When a translucent bar must be drawn (due to API restrictions), what color it should be drawn in dark mode
Both translucentLightStr and translucentDarkStr accept and color that Color.parseColor) is able to handle as well as the string 'transparent'.
> THIS MEANS THAT THREE DIGIT HEX SHORTHAND LIKE #FFF WILL CAUSE YOUR APP TO CRASH
$3
If you only use the JavaScript code, your app will flash the navbar once the App.tsx code finally renders.
If you want to avoid a jump like that, you can edit your code in:
`
android > app > src > main > java > yourpackagepath > MainActivity.java
`
And change the code to reflect this:
`java
import com.facebook.react.ReactActivity;
import com.rnimmersivebars.ImmersiveBars; // here
public class MainActivity extends ReactActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
boolean isDarkMode = false; // Customize this to match your app's default theme
ImmersiveBars.changeBarColors(this, isDarkMode); // here
super.onCreate(savedInstanceState);
}
// ...other code
}
`
Alternatives
If you don't need to use a fullscreen navbar, then you can simply change the color of the navbar itself with this package:
- react-native-navigation-bar-color
Note that this package does not play nice with react-native-safe-area-context's edge detection.
Otherwise, if you want to hide the navbar and the statusbar in their entirety, I'd suggest taking a look at the following package:
- react-native-immersive`