Add space between button - react-native

I wanted to add space between button but it's not working below is my code. What do I need to add in my code?
render(){
return (
<View>
<Text>Home</Text>
<Button style={styles.button}
title='Add '
/>
<Button style={styles.button}
title='Search/Update'
/>
</View>
)
}
}
const styles = StyleSheet.create({
button: {
marginBottom: 20,
padding: 30
},
})

Easiest way is to add a spacing view between your two buttons:
<View>
<Text>Home</Text>
<Button style={styles.button} title='Add' />
<View style={styles.space} /> // <------------------------- right here
<Button style={styles.button} title='Search/Update' />
</View>
const styles = StyleSheet.create({
button: {
marginBottom: 20,
padding: 30
},
space: {
width: 20, // or whatever size you need
height: 20,
},
})

Or you could use like this:
<View>
<View style={{ flex: 1, marginBottom: 10 }}>
<Button title="Add" />
</View>
<View style={{ flex: 1 }}>
<Button title="Search/Update" />
</View>
</View>

You can apply this styling to View component in order to get space between two buttons.
Also import Dimensions from react-native and adjust the width of View according to your need.
import { Dimensions } from 'react-native';
<View style={{
width:Dimensions.get("window").width * 0.5,
display:"flex",
justifyContent:"space-evenly",
alignItems: "center"
}}>
<Text>Home</Text>
<Button
style={styles.button}
title='Add '
/>
<Button
style={styles.button}
title='Search/Update'
/>
</View>

The code <View style={{marginVertical: 10}} You have to use here.
The Button should come inside View Tag
You can use the below code to add space between button
<View style={{marginVertical: 10}}>
<Button title="Button 1" />
</View>
<View style={{marginVertical: 10}}>
<Button title="Button 2" />
</View>

Related

How to rotate Fontisto icon in react native

I Import Fontisto import { Fontisto } from "#expo/vector-icons";
Add an Icon <Fontisto style={styles.windDirectionArrow} name='arrow-up' />
and the styling to rotate it
const styles = StyleSheet.create({
windDirectionArrow: {
fontSize: 40,
transform: [{ rotate: "90deg" }],
},
});
But the transform is not working, does anyone know any other solutions for this?
The view I am rendering looks like this
return (
<View style={styles.container}>
{error ? (
<View style={styles.centeredContainer}>
<Text style={styles.text}>Unable to load weather data</Text>
<Text style={styles.text}>{error}</Text>
</View>
) : isLoading ? (
<View style={styles.centeredContainer}>
<Text style={styles.text}>Loading Weather Data</Text>
<ActivityIndicator
style={styles.activityIndicator}
size='large'
color='rgb(255,179,25)'
/>
</View>
) : (
<View style={[styles.centeredContainer]}>
<Fontisto style={styles.text} name={icon} />
<Text style={styles.text}>{temperature}˚C</Text>
<Text style={styles.text}>
<Fontisto name='arrow-up' style={styles.windDirectionArrow} />
{windSpeed}mph
</Text>
<Text style={styles.text}>{weatherCondition}</Text>
</View>
)}
</View>
);
It works if I have the Icon element wrapped in its own but I think that is not a clean solution
<Text style={styles.text}>
<View>
<Fontisto name='arrow-up' style={styles.windDirectionArrow} />
</View>
{windSpeed}mph
</Text>
It works perfectly for me
// With Rotation
<Fontisto name="arrow-up" style={styles.windDirectionArrow} />
// Without Rotation
<Fontisto name="arrow-up" style={styles.withoutRottion} />
// When icon is wrapped inside a View and that View is rotated
<View style={styles.windDirectionArrow}>
<Fontisto name="arrow-up" style={{ fontSize: 40 }} />
</View>
My Styles :
windDirectionArrow: {
fontSize: 40,
transform: [{ rotate: '90deg' }],
},
withoutRottion: {
fontSize: 40,
},
Working Example
And also why don't you use arrow-right from Fontisto
And for Text Side by side
<View style={{ justifyContent: 'center', alignItems: 'center', flexDirection: 'row' }}>
<View style={styles.windDirectionArrow}>
<Fontisto name="arrow-up" style={{ fontSize: 40 }} />
</View>
<Text style={styles.text}>{windSpeed}mph</Text>
</View>

React native nested scroll view when horizontal not showing anything

I have looked at most of the question related to mine but haven't found a solution, my code looks as follows:
<View style={{flex: 1}}>
<ScrollView style={{flexGrow: 1}}>
<Component />
<AnotherComponent />
<View style={{flex: 1}}>
<ScrollView
style={{}}
nestedScrollEnabled
horizontal={true}>
<Button />
<Button />
<Button />
</ScrollView>
</View>
</ScrollView>
</View>
When I remove the horizontal attribute and add a specific height to the nested ScrollView than the scroll will work vertically but the moment I set horizontal={true}, the Buttons disappear.
Drawing of the layout:
I created a Custom Buttom for design purpose which look like this
Check this Snack too
import * as React from 'react';
import {
Text,
View,
StyleSheet,
ScrollView,
Button as BTN,
Dimensions,
} from 'react-native';
function Button({ title, style = {} }) {
return (
<View style={{ ...style }}>
<BTN title={title} />
</View>
);
}
export default Button;
And then in the nested scrolls look like this
import Button from './components/Button';
// Watch out here I have imported my custom button and not the Button from "react-native"
<View style={{ flex: 1 }}>
<ScrollView style={{ flexGrow: 1 }}>
<View></View>
<View></View>
<View style={{ flex: 1 }}>
<ScrollView
style={{ flexDirection: 'row' }}
nestedScrollEnabled
horizontal={true}>
<Button title="TEST" style={{ width: 100, margin: 10 }} />
<Button title="TEST" style={{ width: 100, margin: 10 }} />
<Button title="TEST" style={{ width: 100, margin: 10 }} />
</ScrollView>
</View>
</ScrollView>
</View>

When click input, button which is fixed on bottom appears over keyboar in react native android

In ios it is ok however when i click input in android, button appears over keyboard and it seems so bad. how can i handle that ? this is my code:
<>
<SafeAreaView style={styles.container}>
<ScrollView
contentInsetAdjustmentBehavior="automatic"
keyboardShouldPersistTaps="handled"
style={styles.scrollviewStyle}
>
<FormInput label="1" />
<FormInput label="2" />
<FormInput label="3" />
<FormInput label="4" />
<FormInput label="5" icon={true} />
<Text
style={{
color: '#938E8E',
marginLeft: 30,
fontSize: 14,
fontWeight: '400',
}}
>
text
</Text>
<Dropdown />
<View style={styles.agreementContainer}>
<View
style={{
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
}}
>
<CheckBox onPress={chekboxButton} checked={toggleCheckBox} />
<TouchableOpacity>
<Text style={styles.acceptagreementStyle}>
text
</Text>
</TouchableOpacity>
</View>
</View>
</ScrollView>
<View style={styles.buttonContainer}>
<Button buttonText="1" onSubmit={() => console.log('test')} />
</View>
</SafeAreaView>
</>
this button comes over keyboard:
<View style={styles.buttonContainer}>
<Button buttonText="1" onSubmit={() => console.log('test')} />
</View>
If I understood your question correctly, you can easily fix this problem using
<KeyboardAvoidingView> https://reactnative.dev/docs/keyboardavoidingview
and wrapping your component with that.
this is because android:windowSoftInputMode property in AndroidManifest.xml which is set to adjustResize by default
you have 2 solutions
1 : change your android/app/src/main/AndroidManifest.xml
<activity
....
android:windowSoftInputMode="adjustPan">
2 : use react-native-android-keyboard-adjust
import AndroidKeyboardAdjust from 'react-native-android-keyboard-adjust';
.....
AndroidKeyboardAdjust.setAdjustPan();

Align all icon to the right of page regardless of start point React Native

I want to be able to align all reply icons to the far right, where the red line is, regardless of where they start.
Edit: added more information to show how recursion is used in the component. Why I try to use some answers that work without recursion, I receive an undesired effect.
This is the code I have in place so far:
class Comment extends Component {
render() {
return(
<View>
<Header
rounded
style={{
backgroundColor: '#ffffff',
position: 'relative',
}}
>
<View style={{flexDirection: 'row', flexWrap: 'wrap', right: '43%', top: '50%'}}>
<Icon name='chevron-left' size={10} color='#006FFF' style={{top: '6%'}}/>
<NativeText
onPress={() => this.props.history.push('/')}
style ={{color: '#006FFF', fontSize: 12, fontFamily: 'Montserrat-Regular'}}
>
Back
</NativeText>
</View>
</Header>
<View
style={{paddingLeft: '2%', paddingTop: '2%'}}
>
<CommentList
options={this.props.location.state.comments}
currentUser={this.props.location.state.currentUser}
history={this.props.history}
reportId={this.props.location.state.reportId}
optionsForBackButton={this.props.location.state.comments}
/>
</View>
</View>
)
}
}
export default withRouter(Comment)
const CommentList = ({options, currentUser, history, reportId, optionsForBackButton}) => {
return (
<View>
{options.map(option => (
<View
style={{flexDirection: 'row'}}
>
<NativeText
style={{fontSize: 12, fontFamily: 'Montserrat-Regular'}}
>
{option.content}
</NativeText>
<View
style={{flex: 1, alignItems: 'flex-end' }}
>
<Icon
name='reply'
size={12}
// onPress={() => {
// setModalVisible(true)
// changeParentId(option._id)
// }}
onPress={() => history.push({pathname: '/create-comment', state: {content: option.content, currentUser: currentUser, reportId: reportId, parentCommentId: option._id, optionsForBackButton: optionsForBackButton}})}
/>
</View>
{
<View
style={{left: '10%'}}
>
<CommentList
options={option.reply}
optionsForBackButton={optionsForBackButton}
history={history}
currentUser={currentUser}
reportId={reportId}
/>
</View>
}
</View>
))}
</View>
)
}
Set your icon containing view's flex value to 1. This should cause it to fill all remaining space.
See the following snack: https://snack.expo.io/#jrdndncn/playful-churros
class Comment extends React.Component {
render() {
return (
<View
style={{
marginVertical: 2,
flexDirection: 'row',
marginLeft: (this.props.indent || 0) * 20,
}}>
<Text>{this.props.text}</Text>
<View style={{ flex: 1, alignItems: 'flex-end' }}>
<View style={{ width: 20, height: 20, backgroundColor: 'red' }} />
</View>
</View>
);
}
}
<Comment text="hello" indent={0} />
<Comment text="hello" indent={1} />
<Comment text="hello" indent={2} />
Basically marginLeft:'auto' will do the trick. just add style to icon as :
<Icon
name='reply'
size={12}
style={{marginLeft:'auto'}}
// onPress={() => {
// setModalVisible(true)
// changeParentId(option._id)
// }}
onPress={() => history.push({pathname: '/create-comment', state: {content: option.content, currentUser: currentUser, reportId: reportId, parentCommentId: option._id, optionsForBackButton: optionsForBackButton}})}
/>
i added marginLeft:'auto' in style it will automatically shown at the right end of the screen.

Two buttons sharing a row in react-native

I have two buttons that look like this
This is the code
render = () => (
<Image
source={require('../../images/login.jpg')}
style={[AppStyles.containerCentered, AppStyles.container, styles.background]}
>
<Image
source={require('../../images/logo.png')}
style={[styles.logo]}
/>
<Spacer size={200} />
<View style={[AppStyles.row, AppStyles.paddingHorizontal]}>
<View style={[AppStyles.flex1]}>
<Button
title={'Login'}
icon={{ name: 'lock' }}
onPress={Actions.login}
/>
</View>
</View>
<Spacer size={10} />
<View style={[AppStyles.row, AppStyles.paddingHorizontal]}>
<View style={[AppStyles.flex1]}>
<Button
title={'Sign up'}
backgroundColor={'#FB6567'}
icon={{ name: 'face' }}
onPress={Actions.signUp}
/>
</View>
</View>
</Image>
)
I want the buttons to occupy one row and possibly share 40% - 40% of the space with the rest of the 20% going to the padding. How can i have them occupy one row?.
You'd need to define a container with flexDirection set to row and use justifyContent depending on where you want your padding:
render() {
return (
<View style={styles.container}>
<View style={styles.button} />
<View style={styles.button} />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'row',
justifyContent: 'space-between'
},
button: {
backgroundColor: 'green',
width: '40%',
height: 40
}
});
Change space-between to space-around if you want the padding to be distributed to the sides too. (Demo)