How to make StatusBar transparent? - android

Does anyone know a way to make Android Status Bar transparent with React Native?
NOT TRANSLUCENT, Transparent.
I am using react-navigation too.

Just use it like this:
Tested with: "react-native": "0.60.4" and "0.61.5"
<StatusBar translucent backgroundColor="transparent" />

This is working for me on android
<StatusBar
backgroundColor="transparent"
translucent={true}
/>

Unless you are using Statusbar as a component, try this method.
useFocusEffect(
useCallback(() => {
StatusBar.setBarStyle("dark-content");
Platform.OS === 'android' && StatusBar.setBackgroundColor('transparent');
StatusBar.setTranslucent(true);
}, []),
);
Do not forget to give some paddingTop to the most outer View.

<StatusBar translucent backgroundColor="transparent" /> is the way to go, thx to #Felipe Rugai
However, 2 things to know:
If you are using <Header /> component from react-native-elements, it already have <StatusBar /> included, using its statusBarProps instead.
If you are using WIX react-native-navigation, they have a separate way to dealing with the status bar, refer to this and this. They said it is incompatible with React Native's , however, looks like they work well together for me.
Also android native code/config discussed in another stackoverflow topic solution will override by <StatusBar /> hence doesn't work well.

If you're talking about the status bar of the OS (the one you pull down to access wifi/bluetooth/settings etc), try adding this to you MainActivity.java:
private void hideNavigationBar() {
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
}
And you can call that^ function in this function from the same MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
hideNavigationBar();
}
However, if you're talking about the StatusBar of the app, try adding this to your App.js file
static navigationOptions = {
header: null
}

This solution can solve the issue if you're working with expo
in your package JSON /expo configuration: you can add a property to overwrite the default StatusBar for android
"androidStatusBar":{
"translucent":false
}

you can set it usingStatusBar.setBackgroundColor(Colors.TRANSPARENT);

Try this to make transparent statusbar in android
container: {
flex:1,
paddingTop: 20
},
add display flex and paddingTop to your main View component

In react native, if you are using expo you can go to the app.json file and add status bar color. After this background color of the status bar for the complete app will change.
"androidStatusBar": {
"backgroundColor": "#105846"
},
Check the linked page.

Related

react native android keyboard empty space

I am trying to fix a problem we are having with the keyboard on android. Due to react-native-gifted-chat, we have to use android:windowSoftInputMode="adjustResize" instead of adjustPan. The problem is, that the chat breaks without adjustResize and all the other stuff (e.g. some textfields in a form) break without adjustPan. I also tried adjustResize|adjustPan, adjustPan|adjustResize and tried to use KeyboardAvoidingView on the Form components, but nothing seems to work. Here is how it looks like when using adjustResize without any KeyboardAvoidingView. It creates some not-clickable grey area above the keyboard. Note that there is no way around adjustResize due to the chat...
Thanks in advance!
For anyone struggling with the same:
The package react-native-set-soft-input-mode allows you to change the softInputMode, e.g. for the chat, the following works fine:
useEffect(() => {
if (Platform.OS === 'android') {
SoftInputMode.set(SoftInputMode.ADJUST_RESIZE);
}
return () => {
if (Platform.OS === 'android') {
SoftInputMode.set(SoftInputMode.ADJUST_PAN);
}
};
}, []);

When removing a View from render, it's background continues there on Android

I'm having a serious problem with React Native on Android.
Let's say I have the following render method:
render() {
let stuff = this.state.showStuff ? <Text style={styles.stuff}>Stuff</Text> : null
return (
<View>
{ stuff }
</View>
)
}
Really simple. If I want to hide the "Stuff", I just call:
this.setState({ showStuff: false })
This works fine on iOS, but on Android, if styles.stuff define a backgroundColor, the component will be re-rendered without the "stuff" content, but with it's background!
Now I have no idea on how to remove elements from my component, since this weird behavior broke how I used to think react native works.
I found out that react-native-searchbar was causing the issue on non-related views. Removing that component solved this and other non-related issues on my project.

React-Native: Show loading screen till the webview is loaded

I have at the moment a component SplashScreen which I'm rendering first till my state is set. I would like somehow to find a way how to still show this component while my webview is loaded. I added the onLoadEnd to my webview and looks like I get my message back when its finished loading, the problem is that if I load first the splashscreen and wait for the state to be changed onLoadEnd actually will never be changed because the webview is not yet rendered. Is there a good method how to do this?
My solution was actually quite simple, the WebView component can have the param renderLoading which for me was not working, I figured out it was because also startInLoadingState needed to be defined.
So my WebView looks somehow like this:
<WebView
ref={MY_REF}
source={source}
renderLoading={this.renderLoading}
startInLoadingState
/>
This would be my approach:
constructor(props){
super(props);
this.state = { webviewLoaded: false };
}
_onLoadEnd() {
this.setState({ webviewLoaded: true });
}
render() {
return(
<View>
{(this.state.webviewLoaded) ? null : <SplashScreen />}
<WebView onLoadEnd={this._onLoadEnd.bind(this)} />
</View>
)
}
This way, the webview is rendered but it is placed behind the SplashScreen. So the webview starts loading while the SplashScreen is being displayed.
I had a similar problem and I managed to solve it temporarily with this:
loadEnd () {
this.setState({ webViewLoaded: true }):
}
render () {
const { webViewLoaded } = this.state;
return (<View>
{!webViewLoaded && <LoadingComponent /> } -- or spinner, whatever
<WebView
style={(webViewLoaded) ? styles.webView : styles.loading}
onLoadEnd={() => this.loadEnd.bind(this)} />
</View);
}
const styles = StyleSheet.create({
webView: { -- your styles ---},
loading: {
width: 0,
heigt: 0
}
});
not sure if exactly this helps you but you can try similar approach. I will probably change this to something more convenient. Not sure if there are possibilities to animate these changes because Im still pretty newbie in React Native.
edit: added hiding the spinner/loading element

React Native - Android PullToRefresh ListView

Does anyone know how to implement a listview refresher for android in React-Native?
I think this is a common problem a lot of people have.
There is react-native-refreshable-listview , but it does not support android.
You should be able to accomplish this behavior by using the PullToRefreshViewAndroid (https://facebook.github.io/react-native/docs/pulltorefreshviewandroid.html#content) launched with React Native 0.16.
As mentioned in this issue: https://github.com/facebook/react-native/issues/4793, you might have to use style={{flex: 1}} on the PullToRefreshView to achieve the result you want, something like this:
<PullToRefreshViewAndroid
refreshing={isRefreshing}
onRefresh={this.onRefresh}
style={{ flex: 1 }}>
<ListView
...listViewProps />
</PullToRefreshViewAndroid>
Then you should get the same behaviour as ReactNativeRefreshableListView gives you, but on Android.
React-native has an out of the box solution for pull to refresh
<ListView
refreshControl={
<RefreshControl
refreshing={this.state.refreshing}
onRefresh={this.onRefresh.bind(this)}
/>
}
...
dataSource={this.state.dataSource}
/>
and elsewhere in your class, an onRefresh() function...
onRefresh() {
this.setState({refreshing:true});
//refresh data and set refreshing to false as appropriate for your app...
}

Cordova Android status Bar set to transparent

I am trying to use this plugin below to set the statusbar to transparent. But i can not achieve it, i can change it to different colours, but not transparent.
https://github.com/apache/cordova-plugin-statusbar
Also it works on my Android 5.0.2, but not 5.0.
I tried just leaving out the hexcode value like they suggested but doesnt work, i tried all those below, none of those set my statusbar to transparent.
<preference name="StatusBarBackgroundColor"/>
<preference name="StatusBarStyle" value="lightcontent" />
if (cordova.platformId == 'android') {
StatusBar.styleBlackTranslucent();
}
I tried so long to fix this issue. There is no documentation how to make status bar transparent for Android included paddings for the <ion-header>
So here is how i fixed it. After platform ready in app.component.ts:
if (this.platform.is('android')) {
Plugins.StatusBar.setOverlaysWebView({overlay: true});
Plugins.StatusBar.setBackgroundColor({color: '#33000000'});
}
Don't set background color if you want your status bar transparent. In my case it will be a black status bar with 20% opacity.
And DONT'T FORGET to force Ionic for the statusbar padding when you import its modules in app.module.ts. Otherways your header will be sticky to the status bar:
IonicModule.forRoot({_forceStatusbarPadding: true})
Versions:
ionic-native/status-bar: ^5.0.0
capacitor/android: ^2.1.0
Using
IONIC Native Status Bar Plugin
app.component.ts:
import { Platform } from '#ionic/angular';
import { StatusBar } from '#ionic-native/status-bar/ngx';
...
...
constructor(private platform: Platform, private statusBar: StatusBar){
this.initializeApp();
}
initializeApp() {
this.platform.ready().then(() => {
this.statusBar.overlaysWebView(true);
this.statusBar.backgroundColorByHexString('#33000000');
});
}
app.module.ts:
IonicModule.forRoot({_forceStatusbarPadding: true})
I just found it, its not mention in their documentation, but its simple as that:
<preference name="StatusBarBackgroundColor" value="transparent" />
And voilĂ , Its working :)

Categories

Resources