Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want 2 buttons side by side in react native

I am new to react native and I am trying to achieve 2 buttons side by side I have tried it and I couldn't achieve it as I have already inserted 2 buttons it is appearing one below another

Here is my code:

      <View style={styles.buttonStyle}>
        <Button
         title={"Pause"}
         style={styles.buttonStyle}
         onPress={() => {
           this.setState({ paused: true });
         }}
          color="#841584"
        />
        </View>
         <View style={styles.buttonStyle}>
          <Button
             title={"Play"}
            onPress={() => {
            this.setState({ paused: false });
         }}
         color="green"
       />
      </View>
     </View>
     );
    }
   }
  const styles = StyleSheet.create({
     backgroundVideo: {
    position: "absolute",
    top: 0,
    left: 0,
    bottom: 0,
   right: 0
  },
 buttonStyle: {
    marginHorizontal: 20,
    marginTop: 5
  }
 });
 export default VideoPlayer;
like image 487
Akarsh s Avatar asked Mar 18 '19 13:03

Akarsh s


People also ask

How to create a button with text in React Native?

Let’s create one starter react native project. Create one new file CButton.js with the below code : We are using TouchableOpacity for the button. One Text component is added for showing the title. text is passed to this component. width is 46% ,and height is 60. You can change these values to see how it looks.

What is React Native Floating Action Button with multiple option?

This is an example of React Native Floating Action Button with Multiple Option. React Native Action Button is a kind of Floating Action Button with multiple options. You can also see the example of React Native Floating Action Button in which we set an onPress to do some action.

How to combine two cbutton with different text values?

Here, we are adding two CButton with different text values. The wrapper View of these buttons is having flex = 1 with flexDirection = row and justifyContent as space-around. space-around adds paddings of the buttons on each side. Run it, and it will give one output as like below :


2 Answers

The property that defines rendering direction is called flexDirection, it can be set to column (default, render items vertically) or row (renders items horizontally).

So to achieve the desired effect you need to add the style property flexDirection:"row" to the View that contain the buttons. Your code would look something like:

<View style={{ flexDirection:"row" }}>
    <View style={styles.buttonStyle}>
        <Button>Button 1</Button>
    </View>
    <View style={styles.buttonStyle}>
        <Button>Button 2</Button>
    </View>
</View>
like image 142
Diego Garcia Avatar answered Oct 25 '22 14:10

Diego Garcia


Try it out

<View style={{ flexDirection: "row" }}>
    <View style={styles.buttonStyle}>
        <Button title={"Button 11"}/>
        <Button title={"Button 12"}/>
    </View>
    <View style={styles.buttonStyle}>
        <Button title={"Button 21"}/>
        <Button title={"Button 22"}/>
    </View>
</View>
like image 44
Artem Avatar answered Oct 25 '22 13:10

Artem