Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transparent backgroundColor for an Image using React Native on Android

I have an asset with transparent background being used on our app. While on iOS, setting backgroundColor: 'transparent' for the Image component works as expected, on Android, it always gets rendered with a light grey background.

In fact, backgroundColor value seems to be completely ignored on Android.

This is how it looks right now:

enter image description here

<View style={styles.cardHeader}>
  <Image
    source={require('../assets/images/greeting.png')}
    style={styles.greetingImage} />
</View>

Styles:

cardHeader: {
  alignSelf: 'stretch',
  backgroundColor: Color.GREY,
  paddingTop: 30,
  borderTopLeftRadius: 5,
  borderTopRightRadius: 5
},

greetingImage: {
  alignSelf: 'center',
  backgroundColor: 'transparent'
}
like image 833
Diego Couto Avatar asked Feb 05 '23 18:02

Diego Couto


2 Answers

I realised that I was importing my Image component from shoutem-ui instead of react-native. This is why I was not being able to set a background color, which seems to be ignored by the former, but not the later. 😉

If you stumble on something similar, be sure you're importing the componentes from the right package:

import {Image} from 'react-native';
like image 182
Diego Couto Avatar answered Feb 07 '23 06:02

Diego Couto


You can also add StyleSheet.absoluteFillObject to your container.

greetingImage: {
  ...StyleSheet.absoluteFillObject
  alignSelf: 'center',
  backgroundColor: 'transparent'
}
like image 26
Keurdange Avatar answered Feb 07 '23 07:02

Keurdange