Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to realize a circle image button with react native

Tags:

react-native

There. When I was trying to make a circle shaped button component with React Native. I set the borderRadius of an Image half the value of its height and width to make it looks like the circle button and apply gesture responder to it.As a result when i tapped outside the circle but inside some rectangular area around the Image view, the responder event dispatched which is unexpected.

I just cannot figure it out:

Is there any possibility to determine the touch area of Touchable* and how? Does the gesture responder system support certain area gesture detection? Any help would be appreciated!

like image 409
Alrightlook Avatar asked Feb 23 '16 08:02

Alrightlook


People also ask

How do you make a button circle in React Native?

Replace the default code in App. js with the following: // App. js import React from 'react'; import {View, Text, StyleSheet, TouchableOpacity} from 'react-native'; function App() { const buttonClickedHandler = () => { console. log('You have been clicked a button!

How do I show circular images in React Native?

To add a rounded image with a border with React Native, we can set the borderRadius and overflow styles. to set borderRadius to '50%' to make the Image round. And we set the width and height of the Image to set the dimensions. We set overflow to 'hidden' so the Image stays in the circle.

How do you make a rounded corner button in React Native?

Add one rounded corner Button : So, react-native doesn't provide this option to change the corner. Instead, we can create one TouchableOpacity with a Text in it to make it looks like a button. Import TouchableOpacity and Text from react-native.

How do I add an image to React Native button?

So let us import an image component and attach to the button (which is TouchableOpacity). Attach this image component to our button. The full program becomes as this: import React from 'react'; import { StyleSheet, Text, View, TouchableOpacity, Image } from 'react-native'; export default class App extends React.


2 Answers

try this:

 <TouchableOpacity
   style={{
       borderWidth:1,
       borderColor:'rgba(0,0,0,0.2)',
       alignItems:'center',
       justifyContent:'center',
       width:100,
       height:100,
       backgroundColor:'#fff',
       borderRadius:50,
     }}
 >
   <Icon name={"chevron-right"}  size={30} color="#01a699" />
 </TouchableOpacity>
like image 180
garrettmac Avatar answered Oct 22 '22 17:10

garrettmac


You need to apply styling to the Touchable area as well as the image if you do not want the outside of the image to be touchable.

The first image has only the image Touchable, while the second only styles the image, leaving the entire rectangle touchable.

'use strict';

var React = require('react-native');
var {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  Image,
  TouchableHighlight
} = React;

var SampleApp = React.createClass({
  render: function() {
    return (
      <View style={styles.container}>
       <Text style={{ fontSize:22 }}>Only image clickable</Text>
       <TouchableHighlight style={ styles.imageContainer }>
            <Image style={ styles.image } source={{ uri: 'http://www.free-avatars.com/data/media/37/cat_avatar_0597.jpg' }} />
       </TouchableHighlight> 
       <Text style={{ fontSize:22 }}>Entire Row Clickable</Text>
       <TouchableHighlight style={ styles.imageContainer2 }>
            <Image style={ styles.image } source={{ uri: 'http://www.free-avatars.com/data/media/37/cat_avatar_0597.jpg' }} />
       </TouchableHighlight> 
      </View>  
    );
  }
}); 

var styles = StyleSheet.create({
  container: {
    flex: 1,
    marginTop:60
  },
  imageContainer: {
    height:128,
    width: 128,
    borderRadius: 64
  },
  image: {
    height:128,
    width: 128,
    borderRadius: 64
  },
  imageContainer2: {

  }
});

AppRegistry.registerComponent('SampleApp', () => SampleApp);
like image 40
Nader Dabit Avatar answered Oct 22 '22 17:10

Nader Dabit