I am new to react-native and I want to design such a button, I tried and searched but could not find an answer
any help would be appreciated
Look at this aproach, with radiusBorder you can achieve something close to you're looking for:
import React from 'react';
import { View, StyleSheet, TouchableOpacity, Text } from 'react-native';
export default function NAME() {
return (
<TouchableOpacity>
<View style={styles.wrapperContainer}>
<View style={styles.Container}>
<Text>Foo</Text>
</View>
</View>
</TouchableOpacity>
);
}
const styles = StyleSheet.create({
Container: {
height: 50,
width: 100,
alignItems: 'center',
background: '#1f90f0',
borderRadius: 5,
justifyContent: 'center',
borderTopLeftRadius: 50,
borderLeftWidth: 2,
borderLeftColor: 'lightgray'
},
wrapperContainer: {
background: 'lightgray',
width: 100,
}
});
You can create a custom button in react-native by using TouchableOpacity and if you want to perform any function when the user press that button you can use onPress prop.
import React from 'react';
import {TouchableOpacity, View, Text} from 'react-native';
const FunctionName = () => {
return(
<View>
<TouchableOpacity style={styles.buttonStyling}>
<Text>Button Name</Text>
</TouchableOpacity>
</View>
);
}
export defaultFunctionName;
Use TouchableOpacity instead. That way you can customize the button however you want it to be.
https://reactnative.dev/docs/touchableopacity
import React from 'react';
import {TouchableOpacity, View, Text} from 'react-native';
export default function NAME() {
return(
<View>
<TouchableOpacity style={{padding: 10, borderWidth: 1, borderColor: '#000'}}>
<Text>Text goes here</Text>
</TouchableOpacity>
</View>
);
}
Edit: This is the closest I got for what you are looking for.
import React from 'react';
import { View, StyleSheet, TouchableOpacity } from 'react-native';
export default function NAME() {
return (
<TouchableOpacity>
<View style={styles.Container}>
<View style={styles.ButtonShape} />
</View>
</TouchableOpacity>
);
}
const styles = StyleSheet.create({
Container: {
backgroundColor: 'transparent',
},
ButtonShape: {
borderLeftWidth: 20,
width: 120,
borderLeftColor: 'transparent',
borderRightColor: 'transparent',
borderTopColor: 'blue',
borderTopWidth: 50,
},
});
Related
when using fontAwesome 5 in react-native : 0.64.2 it shows crossed out box
import React from 'react'
import { Text, View, StyleSheet, TouchableOpacity } from 'react-native';
import FontAwesome5 from 'react-native-vector-icons/FontAwesome5';
function TodoItem({ item, pressHandler }) {
return (
<TouchableOpacity onPress={() => pressHandler(item.key)}>
<View style={styles.text}>
<FontAwesome5 name={'comments-dollar'} size={50} color="red"/>
<Text style={styles.text}>{item.text}</Text>
</View>
</TouchableOpacity>
)
}
const styles = StyleSheet.create({
text: {
padding: 16,
marginTop: 16,
borderColor: '#bbb',
borderWidth: 1,
borderStyle: 'dashed',
borderRadius: 10
}
})
export default TodoItem
I have a keyboardavoidingview with flex 1 with an onPress function that should dismiss the keyboard but nothing happens.
import { StatusBar } from 'expo-status-bar';
import React from 'react';
import { StyleSheet, Text, View, useWindowDimensions, KeyboardAvoidingView, Keyboard } from 'react-native';
import { TouchableOpacity, ScrollView, TextInput } from 'react-native-gesture-handler';
import { AntDesign } from '#expo/vector-icons';
const Home = ({ navigation }) => {
const width = useWindowDimensions().width;
const height = useWindowDimensions().height;
return (
<KeyboardAvoidingView onPress={Keyboard.dismiss} style={styles.container}>
<View style={styles.header}>
<View style={styles.headerTop}>
<TouchableOpacity>
<AntDesign name="pluscircleo" size={42} color="#fff" />
</TouchableOpacity>
</View>
<View style={{justifyContent: 'center', alignItems: 'center'}}>
<Text style={styles.title}>Restaurant</Text>
</View>
<View style={styles.inputContainer}>
<TextInput style={styles.input} placeholder="Find Restuarants" />
</View>
</View>
<StatusBar backgroundColor="#fff" />
</KeyboardAvoidingView>
)
};
const styles = StyleSheet.create({
container: {
flex: 1
},
header: {
backgroundColor: 'red',
},
headerTop: {
paddingTop: 30,
},
title: {
fontSize: 22,
color: '#fff',
},
inputContainer: {
justifyContent: 'center',
alignItems: 'center',
margin: 10
},
input: {
color: '#333',
borderColor: '#fff',
borderRadius: 5,
padding: 6,
backgroundColor: '#fff',
width: 290
}
});
export default Home;
if I delete keyboard.dismiss and add console.log('test') then again nothing happens.
Where is my issue?
thanks for your help!
You should wrap the KeyboardAvoidingView with TouchableWithoutFeedback and remove onPress from the KeyboardAvoidingView and put it in the TouchableWithoutFeedback
import { StyleSheet, Text, View, useWindowDimensions, KeyboardAvoidingView, Keyboard, Platform, Alert, TouchableWithoutFeedback } from 'react-native';
<TouchableWithoutFeedback onPress={Keyboard.dismiss}>
<KeyboardAvoidingView style={styles.container}>
</KeybaordAvoidingView />
</TouchableWithoutFeedback>
Link here
I create a form but if I add touchablewithoutfeedback then it shows nothing more. If I use View its show but why?
import { StatusBar } from 'expo-status-bar';
import React, { useState, useEffect } from 'react';
import { StyleSheet, Text, View, ImageBackground, TextInput, Dimensions, KeyboardAvoidingView } from 'react-native';
import { TouchableOpacity, ScrollView, TouchableWithoutFeedback } from 'react-native-gesture-handler';
import { Ionicons } from '#expo/vector-icons';
const width = Dimensions.get('window').width;
const height = Dimensions.get('window').height;
const createRoom = () => {
return (
<TouchableWithoutFeedback style={{flex: 1,alignItems: 'center', justifyContent: 'center'}}>
<KeyboardAvoidingView behavior="position">
<View style={{backgroundColor: 'red', width: width * 0.8, padding: 20, borderRadius: 8, elevation: 2}}>
<View style={{marginTop: 20, marginBottom: 20}}>
<TextInput placeholder="Username" style={styles.input} />
</View>
<View style={styles.locationContainer}>
<TouchableOpacity>
<Text>geolocation</Text>
</TouchableOpacity>
<View>
<Text>Test</Text>
</View>
</View>
<TouchableOpacity style={styles.button}>
<Text>Gruppe erstellen</Text>
</TouchableOpacity>
</View>
</KeyboardAvoidingView>
</TouchableWithoutFeedback>
)
};
const styles = StyleSheet.create({
container: {
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#fff'
},
input: {
padding: 4,
backgroundColor: '#fff',
},
image: {
height: 150,
borderRadius: 12,
},
locationContainer: {
flexDirection: 'row',
justifyContent: 'space-between'
},
button: {
color: '#fff',
justifyContent: 'center',
alignItems: 'center',
padding: 10
}
});
export default createRoom;
i am very thankful for your help. I dont know the issue.......................................................................................................................................
You can change the TouchableWithoutFeedback import from react-native-gesture-handler to react-native.
import {StyleSheet, Text, View, ImageBackground, TextInput, Dimensions, KeyboardAvoidingView, TouchableOpacity, ScrollView, TouchableWithoutFeedback} from 'react-native';
Or you can go for a new component Pressable
I'am new in React Native & I'am trying to create separated Button component file (reusable component), and when I press the button it will navigate to another page (react-navigation). Below is my code:
GlobalButton.js
import React from 'react'
import { StyleSheet, TouchableOpacity, Text} from 'react-native'
import { useNavigation, NavigationContainer } from '#react-navigation/native';
const GlobalButton = (props, {screenName}) => {
const navigation = useNavigation();
return
<TouchableOpacity style={[styles.btn, props.style]} onPress= {() => navigation.navigate(screenName)}>
{props.children}
</TouchableOpacity>
}
const styles = StyleSheet.create({
btn: {
alignItems: 'center',
textAlign: 'center',
borderRadius: 25,
height: 50,
}
})
export default GlobalButton;
LoginPage.js
import React from 'react'
import { StyleSheet, Text, View, Image, TextInput, Button} from 'react-native'
import ButtonLogin from '../components/GlobalButton'
// import { useNavigation, NavigationContainer } from '#react-navigation/native';
export default function LoginPage() {
return (
<View style={styles.penampung}>
<Image source= {require('../assets/beetle.png')} style={styles.gambar}/>
<TextInput style={styles.textField} placeholder='username' />
<TextInput style={styles.textField} placeholder='password' secureTextEntry={true} />
<ButtonLogin style={styles.btnStyle} screenName="MainPage"><Text style={styles.text}>LOGIN</Text></ButtonLogin>
</View>
)
}
const styles = StyleSheet.create({
penampung: {
flex: 1,
alignItems: 'center',
backgroundColor: '#00688B',
justifyContent: 'center'
},
gambar: {
height: 100,
width: 100,
},
textField: {
marginTop: 25,
backgroundColor: '#cafafe',
borderRadius: 25,
width: '80%',
height: 50,
paddingLeft: 20
},
btnStyle: {
padding: 10,
position: 'absolute',
bottom: 20,
backgroundColor: "#fc4445",
width: '80%'
},
text:{
fontSize: 20,
fontWeight: 'bold',
color: 'white'
}
})
What I'am expecting is that when I click the login button it should navigate to the MainPage.js, but instead it gives error message ->
Error: You need to specify name or key when calling navigate with an object as the argument.
Please anyone help me in this matter, Thanks...
You should do like below as screenName is part of your props,
<TouchableOpacity style={[styles.btn, props.style]} onPress= {() => navigation.navigate(props.screenName)}>
I am having problems when adding the TouchableHighlight component from react-native, and I am given the following error:
Element type is invalid: expected a string
Everything works fine if I delete this
The code is the following:
import React from 'react';
import {View, Text, Image, ScrollView} from 'react-native';
import axios from 'axios';
import {Card, CardItem, Right, Left, TouchableHighlight} from 'native-base';
import { Icon } from 'react-native-elements'
import HeaderMenu from '../Header/Header';
class homeScreen extends React.Component {
state = {
noticias: []
}
componentDidMount() {
axios.get('URL')
.then(res => this.setState({
noticias: res.data
}));
}
render() {
const {navigate} = this.props.navigation;
return (
<View style={{flex: 1}}>
<HeaderMenu title='Noticias'/>
<ScrollView>
<View style={{borderTopColor: 'white', borderTopWidth: 2}}>
{
this.state.noticias.map((noticia, index) => (
<TouchableHighlight onPress={() => navigate('SingleScreen')}>
<CardItem key={index} style={{height: 100, padding: 5, borderBottomColor: '#dee0e2', borderBottomWidth: 1}} >
<View>
<Image style={{width: 120, height: 80, alignSelf: 'stretch'}} source={{uri: noticia.imagen}} />
</View>
<View style={{height: 80, flex: 1, paddingLeft: 10}}>
<Text style={{fontSize: 12, marginBottom: 8}} >{noticia.titulo}</Text>
</View>
<Icon style={{marginTop: 40 ,width: 20, height: '100%', color: '#343537', justifyContent: 'center', paddingLeft: 5}} name="chevron-right" />
</CardItem>
</TouchableHighlight>
))
}
</View>
</ScrollView>
</View>
);
}
}
export default homeScreen;
Already checked other answers but none of them solve my problem.
I found the problem. You are doing this.
import {Card, CardItem, Right, Left, TouchableHighlight} from 'native-base';
You are importing TouchableHighlight from native-base which is wrong. You have to remove it.
You have to import TouchableHighlight from react-native
import {View, Text, Image, ScrollView,TouchableHighlight} from 'react-native';