4

I am trying to make my application button in react-native like below

enter image description here

I am using inbuilt Button view of react native where I see that it does not allow to change the height also. I want to change the height as well rounded like expected image.

This is how my button is looking :

enter image description here

  <Button
    title="CONTINUE"
    color="#FE434C"
    onPress={() => navigate("EnableNotification")}
  />
8
  • style={{borderRadius:10}} Commented Apr 25, 2017 at 9:20
  • What i usually do is avoid using the button view and use a TouchableOpacity implementation. In your instance id fill it with a red view, then set borderRadius = 15 or so. Interested to see if there is a better way. Try setting style = borderRadius 15 and see if that works Commented Apr 25, 2017 at 9:21
  • I have tried <Button title="ENABLE NOTIFICATION" color="#FE434C" onPress={null} style={{ borderRadius: 40 }} /> but it not making buttons rounded Commented Apr 25, 2017 at 9:28
  • Documentation does not have borderRadius attribute facebook.github.io/react-native/docs/button.html Commented Apr 25, 2017 at 9:41
  • Try my method @Williams , i can post some code if you want Commented Apr 25, 2017 at 9:45

7 Answers 7

18

So this is what I usually do:

<TouchableOpacity onPress={() => {/* do this */}}>
  <View style={{
      backgroundColor: 'red',
      alignItems: 'center', 
      justifyContent: 'center',
      borderRadius: 15
    }}
  >
    <Text style={{ color: 'white' }}>Button</Text>
  </View>
</TouchableOpacity>

I find using this method makes buttons much more customisable, but if you do some digging there could be a library which implements something similar (I never really found the need to search for it).

NOTE: Obviously you will have to adjust the height/width of the button to your flavor.

EDIT: My mistake... I had put the onPress prop in the view, woops.

Sign up to request clarification or add additional context in comments.

11 Comments

I find this is good to go. Thanks :) How to set height here ?
Do so in the <View style = {{height: ...}}> area. I'd suggest looking into flex-box however, which is great for multiple screen-sizes.
Now I see click is not working onPress={() => navigate("EnableNotification")}
@Williams im guessing navigate is a function in your component? If so use onPress={() => {this.navigate("EnableNotifcation")}
@stakantin TouchableOpacity has a disabled prop
|
1

Here is my solution to easily style buttons using TouchableOpacity, Text and StyleSheet:

import React, { Component } from 'react';
import { View, StyleSheet, TouchableOpacity, Text } from "react-native";

export default class CustomButton extends Component {
  render(){
    return (
      <View style={styles.container}>

        /* Custom Button */
        <TouchableOpacity
          style={styles.customBtnBG}
          onPress={() => {}}  >
          <Text style={styles.customBtnText}>Button</Text>
        </TouchableOpacity>

      </View>
    )
  }
}


const styles = StyleSheet.create({
  container: {
    flex: 1,
        justifyContent: "center",
    alignItems: "center"
  },

  /* Here, style the text of your button */
    customBtnText: {
        fontSize: 40,
        fontWeight: '400',
        color: "#fff",
    },

  /* Here, style the background of your button */
    customBtnBG: {
    backgroundColor: "#007aff",
    paddingHorizontal: 30,
    paddingVertical: 5,
    borderRadius: 30
    }
});

Comments

1

@Ryan Turnbull, Well, i think a little padding and fontSize will do.

<TouchableOpacity onPress={() => this.onLogin()}>
  <View
    style={{
      backgroundColor: 'black',
      alignItems: 'center',
      justifyContent: 'center',
      borderRadius: 15,
      padding: 15,
    }}>
    <Text style={{color: 'white', fontSize: 20, fontWeight: '800'}}>
      Button
    </Text>
  </View>
</TouchableOpacity>

enter image description here

Comments

1
<TouchableOpacity onPress = {() => {/* do this */}}>
<View style = {{backgroundColor: 'red', alignItems: 'center', 
                justifyContent: 'center', borderRadius: 15}}
       >
    <Text style = {{color: 'white'}}>Button</Text>
</View>

Comments

1

In React-native, we can customise user defined button component and call it from anywhere. So that we can have structured approach throughout the program or application.

import React from "react";
import { StyleSheet, TouchableOpacity, Text, View } from "react-native";

const MyButton = props => {
  return (
    <TouchableOpacity
      onPress={props.onPress}
    >
      <View style={{...styles.buttonStyle, ...props.style,
         backgroundColor:props.buttonColor, 
         borderColor:props.buttonColor}}>
        <Text style={{...styles.TextStyle, ...props.style, 
          color:props.fontColor}}>
          {props.children}
        </Text>
      </View>
    </TouchableOpacity>
  );
};
const styles = StyleSheet.create({
  buttonStyle: {
    paddingVertical: 10,
    paddingHorizontal: 10,
    borderRadius: 10,
    borderWidth: 2,
  },
  TextStyle: {
    textAlign: "center",
  },
});
export default MyButton;

In style ... spread operator is used to integrate all the styles from same components and incoming styles from parent component through props.

props.children is used here to pass button title, so that title text is displayed on the button.

When button is pressed then instead of handling this press event inside of the button but inside of the component where we use this button. So we pass a fitting function reference on the button.

This MyButton button component can be called anywhere inside the program like:

<MyButton onPress={() => onPressFunction()}
 buttonColor="red" fontColor="white">Button Title</MyButton>

Comments

0

You can use Touchable Opacity and it's prop border radius to adjust it's curve.

https://facebook.github.io/react-native/docs/touchableopacity

Comments

-2

You can use this library https://github.com/APSL/react-native-button to create a customizable button in react native.

<View>
    <Button
        style={{
            backgroundColor: "#FE434C",
            borderColor: "transparent",
            borderRadius: 20,
            width: 250,
            height: 50,
        }}
        textStyle={{ color: "#FFFFFF" }}
        onPress={() => { }}
    >
        CONTINUE
  </Button>
</View>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.