React Native - how to change style and image of a view onPress

John

I have three TouchableHighlight elements wrapping three views (aligned next to each other). Onpress I would like to change the style (backgroundColor) and image of the view (the pressed view would become active).

  • active view - backgroundColor <View style={styles.circle}> should become 'red' and image source should be 'arrow-win-active.png' <Image source={require('../images/arrow-win.png')} style={styles.arrowWin}></Image>
  • the two other views remain the same

What would be the best way to do it?

Here is a screenshot:

enter image description here

Here is my code so far:

import React from 'react'
import {
    View,
    ListView,
    ScrollView,
    StyleSheet,
    Image,
    TouchableHighlight,
} from 'react-native'

const changeStyle = () => {
    console.log('change style')
}

const appView = (game, date) =>
<ScrollView style={styles.container}>
    <View style={styles.step}>
        <View style={{flex:1}}>
            <View style={styles.pickContainer}>
                <TouchableHighlight onPress={() => changeStyle()} style={{flex:1}}>
                    <View style={styles.pickWrapper}>
                        <View style={styles.circle}>
                            <Image source={require('../images/arrow-win.png')} style={styles.arrowWin}></Image>
                        </View>
                    </View>
                </TouchableHighlight>

                <TouchableHighlight style={{flex:1}}>
                    <View style={styles.pickWrapper}>
                        <View style={styles.circle}>
                            <Image source={require('../images/arrow-draw.png')} style={styles.arrowDraw}></Image>
                        </View>
                    </View>
                </TouchableHighlight>

                <TouchableHighlight style={{flex:1}}>
                    <View style={styles.pickWrapper}>
                        <View style={styles.circle}>
                            <Image source={require('../images/arrow-win.png')} style={styles.arrowWin}></Image>
                        </View>
                    </View>
                </TouchableHighlight>
            </View>
        </View>
    </View>
</ScrollView>

const styles = StyleSheet.create({
container: {
    flex: 1,
    backgroundColor: '#e1e1e1'
},
step: {
    backgroundColor: '#ffffff',
    borderRadius: 4,
    borderLeftWidth: 5,
    flex: 1,
    marginLeft: 10,
    marginRight: 10,
    marginBottom: 10,
    paddingLeft: 15,
    paddingRight: 10,
    paddingTop: 15,
    paddingBottom: 15,
    shadowOffset: {
        width: 0,
        height: 2,
    },
    shadowRadius: 2,
    shadowOpacity: 0.2,
    shadowColor: 'black',
    textAlign: 'center',
},
heading: {
    textAlign: 'center',
    fontWeight: 'bold',
    fontSize: 15,
    color: '#333333',
},
pickContainer: {
    flex:1,
    flexDirection: 'row',
    justifyContent: 'space-between',
    alignItems: 'center',
},
pickWrapper: {
    flex: 1,
    flexDirection: 'row',
    justifyContent: 'space-around',
    alignItems: 'center',
    marginTop: 10,
},
circle: {
    height: 60,
    borderRadius: 30,
    width: 60,
    backgroundColor: '#eeeeee',
    alignItems: 'center',
    justifyContent: 'center',
},
arrowWin: {
    width: 34,
    height: 28,
},
arrowDraw: {
    width: 18,
    height: 8,
},
})

export default appView
Henrik R

You have to change AppView to class based component because you have to access state.

import React. {Component} from 'react'
import {
  View,
  ListView,
  ScrollView,
  StyleSheet,
  Image,
  TouchableHighlight,
} from 'react-native'

class AppView extends Component {
  state = {
    isPlayer1ButtonActive: false,
    isDrawButtonActive: false,
    isPlayer2ButtonActive: false,
  }

  activateButton = buttonToActivate => {
    const newState = Object.assign(
      {}, 
      {
        isPlayer1ButtonActive: false,
        isDrawButtonActive: false,
        isPlayer2ButtonActive: false,
      }, 
      {[buttonToActivate]: true},
    )
    this.setState(newState);
  }

  render() {
    const {isPlayer1ButtonActive, isDrawButtonActive, isPlayer2ButtonActive} = this.state

    return (
      <ScrollView style={styles.container}>
        <View style={styles.step}>
          <View style={{flex:1}}>
            <View style={styles.pickContainer}>
              <TouchableHighlight onPress={() => activateButton('isPlayer1ButtonActive')} style={{flex:1}}>
                <View style={styles.pickWrapper}>
                  <View style={[styles.circle, isPlayer1ButtonActive && styles.circleActive]}>
                    <Image 
                      source={isPlayer1ButtonActive ? require('../images/arrow-win-active.png') : require('../images/arrow-win.png')} 
                      style={styles.arrowWin} 
                    />
                  </View>
                </View>
              </TouchableHighlight>

              <TouchableHighlight onPress={() => activateButton('isDrawButtonActive')}  style={{flex:1}}>
                <View style={styles.pickWrapper}>
                  <View style={[styles.circle, isDrawButtonActive && styles.circleActive]}>
                    <Image 
                      source={isDrawButtonActive ? require('../images/arrow-draw-active.png') : require('../images/arrow-draw.png')} 
                      style={styles.arrowDraw} 
                    />
                  </View>
                </View>
              </TouchableHighlight>

              <TouchableHighlight onPress={() => activateButton('isPlayer2ButtonActive')}  style={{flex:1}}>
                <View style={styles.pickWrapper}>
                  <View style={[styles.circle, isPlayer2ButtonActive && styles.circleActive]}>
                    <Image 
                      source={isPlayer2ButtonActive ? require('../images/arrow-win-active.png') : require('../images/arrow-win.png')} 
                      style={styles.arrowWin} 
                    />
                  </View>
                </View>
              </TouchableHighlight>
            </View>
          </View>
        </View>
      </ScrollView>
    )
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#e1e1e1'
  },
  step: {
    backgroundColor: '#ffffff',
    borderRadius: 4,
    borderLeftWidth: 5,
    flex: 1,
    marginLeft: 10,
    marginRight: 10,
    marginBottom: 10,
    paddingLeft: 15,
    paddingRight: 10,
    paddingTop: 15,
    paddingBottom: 15,
    shadowOffset: {
      width: 0,
      height: 2,
    },
    shadowRadius: 2,
    shadowOpacity: 0.2,
    shadowColor: 'black',
    textAlign: 'center',
  },
  heading: {
    textAlign: 'center',
    fontWeight: 'bold',
    fontSize: 15,
    color: '#333333',
  },
  pickContainer: {
    flex:1,
    flexDirection: 'row',
    justifyContent: 'space-between',
    alignItems: 'center',
  },
  pickWrapper: {
    flex: 1,
    flexDirection: 'row',
    justifyContent: 'space-around',
    alignItems: 'center',
    marginTop: 10,
  },
  circle: {
    height: 60,
    borderRadius: 30,
    width: 60,
    backgroundColor: '#eeeeee',
    alignItems: 'center',
    justifyContent: 'center',
  },
  circleActive: {
    backgroundColor: 'red',
  },
  arrowWin: {
    width: 34,
    height: 28,
  },
  arrowDraw: {
    width: 18,
    height: 8,
  },
})

export default AppView

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

React Native : how to change view on onPress event

From Dev

Button change style after onPress with React Native

From Java

React Native: View onPress does not work

From Java

How to change style for an particular column in React Native?

From Dev

How to export View to image data in React Native

From Dev

how to centre the image inside a view in react native

From Dev

how to style legend.png on a react-native map view

From Dev

onPress change fragment view

From Dev

how to change style of selected text from a list in react native

From Dev

Change Button Color onPress (toggle functionality) React Native

From Dev

Change view in Navigator in React Native

From Java

How to change to color of react-native-tab-view?

From Dev

How to view multiple capture image horizontally in react native?

From Dev

Change the Style of an Image View on Pressed State in Android

From Dev

Change button style on press in React Native

From Dev

React native: change style of ListView item on touch

From Dev

change style and property onTextChange - React Native

From Java

react native how to call multiple functions when onPress is clicked

From Dev

How to get onPress event from ScrollView Component in React Native

From Dev

How do I bind the storeId to an onPress event in React-Native?

From Dev

React Native Hamburger onPress Issue

From Dev

react native passing onPress with value

From Dev

React Native,not correct working onPress

From Dev

how to get style of TouchableHighlight onPress

From Dev

Change props of parent view in react native

From Dev

React Native - Handle onPress event to show image while specific ListView Pressed using Timeline ListView

From Dev

How to change Image view in collection view cell

From Dev

How to optimise an Image in React Native

From Dev

How to optimise an Image in React Native

Related Related

  1. 1

    React Native : how to change view on onPress event

  2. 2

    Button change style after onPress with React Native

  3. 3

    React Native: View onPress does not work

  4. 4

    How to change style for an particular column in React Native?

  5. 5

    How to export View to image data in React Native

  6. 6

    how to centre the image inside a view in react native

  7. 7

    how to style legend.png on a react-native map view

  8. 8

    onPress change fragment view

  9. 9

    how to change style of selected text from a list in react native

  10. 10

    Change Button Color onPress (toggle functionality) React Native

  11. 11

    Change view in Navigator in React Native

  12. 12

    How to change to color of react-native-tab-view?

  13. 13

    How to view multiple capture image horizontally in react native?

  14. 14

    Change the Style of an Image View on Pressed State in Android

  15. 15

    Change button style on press in React Native

  16. 16

    React native: change style of ListView item on touch

  17. 17

    change style and property onTextChange - React Native

  18. 18

    react native how to call multiple functions when onPress is clicked

  19. 19

    How to get onPress event from ScrollView Component in React Native

  20. 20

    How do I bind the storeId to an onPress event in React-Native?

  21. 21

    React Native Hamburger onPress Issue

  22. 22

    react native passing onPress with value

  23. 23

    React Native,not correct working onPress

  24. 24

    how to get style of TouchableHighlight onPress

  25. 25

    Change props of parent view in react native

  26. 26

    React Native - Handle onPress event to show image while specific ListView Pressed using Timeline ListView

  27. 27

    How to change Image view in collection view cell

  28. 28

    How to optimise an Image in React Native

  29. 29

    How to optimise an Image in React Native

HotTag

Archive