0

I wanna add an "Edit" button with an icon in my <Image> component. following is my code for the <View> that contain the image.

<View style={{ flex: 1 }}>
    <View
        style={{
            justifyContent: 'space-evenly',
            alignItems: 'center',
            flexDirection: 'row',
            paddingVertical: 10
        }}
    >
        <Image
            source={{ uri: 'https://api.adorable.io/avatars/285/test@user.i.png' }}
            style={{
                marginLeft: 10, width: 100, height: 100, borderRadius: 50
            }}
        />
    </View>
</View>

it is much better if i can do this, without replacing <ListItem> with <Image>

Andrew
  • 26,706
  • 9
  • 85
  • 101
Ayesh Nipun
  • 568
  • 12
  • 28

3 Answers3

3

You can try it like:

       <View>
        <Image
            source={{ uri: 'https://api.adorable.io/avatars/285/test@user.i.png' }}
            style={{
                marginLeft: 10, width: 100, height: 100, borderRadius: 50
            }}
        />
      <Icon name={'edit'} containerStyle={styles.icon} onPress={console.log('I was clicked')}/>
     </View>

Then the icon style like below: Note the absolute position attribute

icon: {
 backgroundColor: '#ccc',
 position: 'absolute',
 right: 0,
 bottom: 0
}

Tested with Icon from react-native-elements but I guess it should work with any other Icon.

Felix Too
  • 11,614
  • 5
  • 24
  • 25
2

Try to use ImageBackground to wrap your icon inside it and for icon purpose use react-native-vector-icon library.

<ImageBackground source={...} style={{width: '100%', height: '100%'}}>
    <Icon
    name="edit"
    size={18}
    onPress={this.edit}
  >
    Edit
</Icon>
</ImageBackground>
Paras Korat
  • 2,011
  • 2
  • 18
  • 36
0

You can use react-native-vector-icons to add an icon within the view which contains image. You can also add an icon button component using that library which will look something like this

<Icon.Button
    name="edit"
    backgroundColor="#3b5998"
    onPress={this.edit}
  >
    Edit
</Icon.Button>
Ehsan
  • 151
  • 1
  • 7