So I am trying to have two buttons on my React Native project and whenever I add text to my code the buttons disappear. Does anyone know why? The text I am trying to add is below
<Text
style={{
fontSize: 55,
fontFamily: 'Futura',
color: '#707070',
textAlign: 'left',
paddingTop: 50,
paddingLeft: 30
}}
>
Feed
</Text>
<Text
style={{
fontSize: 22,
fontFamily: 'Futura',
color: '#BABABA',
textAlign: 'left',
paddingTop: 10,
paddingLeft: 30
}}
>
Find the Circle for your needs!
</Text>
and the code I am trying to add it to is this
<View style={styles.container}>
<View style={styles.buttonContainer}>
<Button title="Button 1"/>
</View>
<View style={styles.buttonContainer}>
<Button title="Button 2"/>
</View>
</View>
All of my code is below but it only shows the text not the buttons.
import React, { Component } from 'react';
import { Button, View, StyleSheet, Text } from 'react-native';
export default class GridView extends Component {
render() {
return (
<View>
<Text
style={{
fontSize: 55,
fontFamily: 'Futura',
color: '#707070',
textAlign: 'left',
paddingTop: 50,
paddingLeft: 30
}}
>
Feed
</Text>
<Text
style={{
fontSize: 22,
fontFamily: 'Futura',
color: '#BABABA',
textAlign: 'left',
paddingTop: 10,
paddingLeft: 30
}}
>
Find the Circle for your needs!
</Text>
<View style={styles.container}>
<View style={styles.buttonContainer}>
<Button title="Button 1"/>
</View>
<View style={styles.buttonContainer}>
<Button title="Button 2"/>
</View>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'center',
},
buttonContainer: {
flex: 1,
},
headers: {
fontSize: 50,
}
});
This should fix your issue: My guess is there is something wrong with a parent component that is cutting of the render. If this is the only view on page also try adding the style {flex:1} to the outer most layer. This can happen sometimes if there is some styling on navigation setup that get passed to childern.
<View style={{flex:1}>
<Text style={{...}}>Feed</Text>
<Text style={{...}}>Find the Circle for your needs!</Text>
<View style={styles.container}>
<View style={styles.buttonContainer}>
<Button title="Button 1"/>
</View>
<View style={styles.buttonContainer}>
<Button title="Button 2"/>
</View>
</View>
</View>
Related
I have 7 buttons on the screen, but 1 button is half visible, one button does not fit at all.
my code:
render() {
return (
<ScrollView style={styles.container}>
<Header />
<View>
<Text style={styles.headerText}>
LC Teknoloji
</Text>
</View>
<View style={styles.btnstyl}>
<Button
onPress={this.Isgr}
title="İş Giriş"
color="#00B0FF"
/>
</View>
<View style={styles.btnstyl}>
<Button
onPress={this.Yapilicak}
title="Yapılacaklar"
color="#00B0FF"
/>
</View>
<View style={styles.btnstyl}>
<Button
onPress={this.gcms}
title="İş Geçmişi"
color="#00B0FF"
/>
</View>
<View style={styles.btnstyl}>
<Button
onPress={this.alnck}
title="Alınıcaklar"
color="#00B0FF"
/>
</View>
<View style={styles.btnstyl}>
<Button
onPress={this.ulist}
title="Ürün Listesi"
color="#00B0FF"
/>
</View>
<View style={styles.btnstyl}>
<Button
onPress={this.Stok}
title="Stoklar"
color="#00B0FF"
/>
</View>
<View style={styles.btnstyl}>
<Button
onPress={this.Musteri}
title="Müşteriler"
color="#00B0FF"
/>
</View>
</ScrollView>
);
}
}
const styles = StyleSheet.create({
container: {
backgroundColor: 'grey',
flex: 1,
backgroundColor: "#F5FCFF",
},
headerText: {
fontSize: 20,
textAlign: "center",
margin: 0,
fontWeight: "bold",
top: 45,
},
btnstyl: {
left: 120,
height: 60,
width: 160,
marginBottom: -20,
top: 55,
},
})
enter image description here
I changed the background color or something, when I pull it up, the buttons come up, it doesn't fit on the screen from the place in the picture
Why do you think the screenshot doesn't fit?
In your styles. Remove the marginBottom and top
const styles = {
.....
btnstyl: {
left: 120,
height: 60,
width: 160
},
}
And add height to the View component of the text above the buttons:
<View style={{ height: 100 }}>
<Text style={styles.headerText}>
LC Teknoloji
</Text>
</View>
I'm learning react native and decided to make a list of my games, for that I use a FlatList (code below), but for some reason the FlatList just scroll a little and I can't see the entire content. I've tryed to add 'flex: 1' in a style (like some awnsers in the internet) but when I do this, all my content turn blank. Here are a screenshot of the maximum I can scroll.
My code (The function don't change nothing in layout, I just pick some data from a API, so I don't paste here):
import React from 'react';
import { Button, FlatList, Image, SafeAreaView, StatusBar, StyleSheet, Text, TextInput, View } from 'react-native'
export default function App() {
const [id, setId] = React.useState('');
const [apps, setApps] = React.useState([]);
return (
<SafeAreaView style={{padding: 20}}>
<StatusBar hidden={false} backgroundColor={"white"} barStyle={'dark-content'} />
<View>
<TextInput style={{ height: 40, borderColor: 'gray', borderWidth: 1 }} onChangeText={text => setId(text)} keyboardType={'numeric'} value={id} placeholder={"Insira o ID do jogo aqui"} />
<Button onPress={e => addApp()} title="Adicionar" color="#841584" accessibilityLabel="Salvar Id" />
<View style={{marginTop: 32}}>
<FlatList data={apps} scrollEnabled={true}
renderItem={({item}) =>
<View style={{alignItems: 'center', marginBottom: 32}}>
<Image style={{width: "100%", height: 200}} source={{ uri: item.image}} />
<Text style={{fontSize: 20}}>{item.name}</Text>
<View style={{width: '100%', flexDirection: 'row', flex: 4, justifyContent: 'space-around'}}>
<View style={{flexDirection:'row', alignItems: 'center'}}>
<Image style={{width: 15, height: 15, marginRight: 5}} source={require('./assets/disc.png')} />
<Text>{item.disc}</Text>
</View>
<View style={{flexDirection:'row', alignItems: 'center'}}>
<Image style={{width: 15, height: 15, marginRight: 5}} source={require('./assets/calendar.png')} />
<Text>{item.dateLaunch}</Text>
</View>
<View style={{flexDirection:'row', alignItems: 'center'}}>
<Image style={{width: 15, height: 15, marginRight: 5}} source={require('./assets/calendar.png')} />
<Text>{item.dateBuy}</Text>
</View>
<View style={{flexDirection:'row', alignItems: 'center'}}>
<Image style={{width: 15, height: 15, marginRight: 5}} source={require('./assets/coin.png')} />
<Text style={{fontWeight: 'bold'}}>{item.price}</Text>
</View>
</View>
</View>
}
/>
</View>
</View>
</SafeAreaView>
);
}
Try this:
<FlatList style={{ flex: 0 }}
initialNumToRender={apps.length}
Or try:
Try using flex: 1 in the listContainer styling, this should make it stay in the boundaries of your parent view.
When using styles to move buttons where I want them, the touchable area where you can press the button remains in the same place and doesn't follow the button. This makes the button unresponsive and can only be pressed if you find the original location!
I've tried:
using TouchableWithoutFeedback.
Moving the order of the components
Putting all sorts in the style reference.
Here's the render:
render() {
if((this.state.isOn) === true){
return(
<ImageBackground source={require('./assets/water-in-glass.gif')} style={styles.container}>
<Text>Timer:{ms(this.state.time,{verbose: true})}</Text>
<TouchableOpacity
onPressIn={this.onItemMouseDown}
onPressOut={this.onItemMouseUp}
>
<View>
<Image source={require('./assets/MyButton.png')}/>
</View>
</TouchableOpacity>
<TouchableOpacity
onPress={this.resetTimer}
>
<View>
<View style={styles.resetbutton}>
<Text style={styles.text}>Reset Timer</Text>
</View>
</View>
</TouchableOpacity>
<TouchableOpacity
onPress={this.onSubmit}
>
<View>
<View style={styles.submitbutton}>
<Text style={styles.text}>Done!</Text>
</View>
</View>
</TouchableOpacity>
</ImageBackground>
)
} else if ((this.state.isOn) === false){
return(
<ImageBackground source={require('./assets/water-in-glass.gif')} style={styles.container}>
<Text>Timer:{ms(this.state.time)}</Text>
<TouchableOpacity
onPressIn={this.onItemMouseDown}
onPressOut={this.onItemMouseUp}
>
<View>
<Image source={require('./assets/MyButton.png')}/>
</View>
</TouchableOpacity>
<TouchableOpacity
onPress={this.resetTimer}
>
<View>
<View style={styles.resetbutton}>
<Text style={styles.text}>Reset Timer</Text>
</View>
</View>
</TouchableOpacity>
<TouchableOpacity
onPress={this.onSubmit}
>
<View>
<View style={styles.submitbutton}>
<Text style={styles.text}>Done!</Text>
</View>
</View>
</TouchableOpacity>
{this.state.scoreArray.map(function(item,i){
return (i < 5) ? <Text key={i}>{item}</Text> : null}).filter(x=>x)
}
<Text>
{this.state.animalArray.map(function(item,i){
return (i < 5) ? <Text key={i}>{item}</Text> : null}).filter(x=>x)
}
</Text>
</ImageBackground>
here's the styling:
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
pissbutton: {
padding: 10,
borderWidth: 3,
borderColor: '#111'
},
resetbutton: {
padding: 10,
borderWidth: 3,
backgroundColor: '#ed1c42'
},
submitbutton: {
marginBottom: 36,
top: 100,
padding: 10,
borderWidth: 3,
backgroundColor: '#42f471'
}
});
I expect when I use styling to move the buttons, they remain responsive, instead of moving, but the hotspot remaining where it is.
Figured it.
The button needed it's own container:
submitbuttoncontainer:{
position: 'absolute',
bottom: 50,
},
submitbutton: {
color: 'white',
fontWeight: 'bold',
fontSize: 12,
justifyContent: 'center',
width: 100,
alignItems: 'center',
}
Wow. That's totally not clear anywhere. I suspect it's my lack of basic knowledge in coding.
I have a list of products and want to display the text "ITEM UNIT" at the right end of my infobox. It is however affected by the position of the text above it.
How do I solve this and put "ITEM UNIT" to the right end of the display?
<TouchableOpacity
onPress={() => this.props.onItemClicked(this.props.item)}
style={{ marginRight: 130 }}
>
<View style={styles.itemContent}>
<View>
<FadeIn>
<Image
resizeMode="contain"
style={{
height: 100,
width: 100,
marginTop: 10,
marginLeft: 10
}}
source={{ uri: url }}
/>
</FadeIn>
</View>
<View style={{ justifyContent: "space-around" }}>
<Text style={{ fontSize: 16, fontFamily: "System" }}>
{articleName}
</Text>
<View
style={{
flexDirection: "row",
justifyContent: "space-between"
}}
>
<View style={{ flexDirection: "row" }}>
<Text style={styles.itemPrice}>{originalprice} </Text>
<Text style={styles.itemPriceReduced}>{specialprice}€</Text>
</View>
<View>
<Text>ITEMUNIT</Text>
</View>
</View>
</View>
</View>
</TouchableOpacity>;
I crated a small demo for you. Please check it here:
https://snack.expo.io/#tarikfojnica/layout (Click Tap To Play on the right side)
Here are the code parts you should check:
<TouchableOpacity style={styles.container} onPress={() => this.props.onItemClicked(this.props.item)}>
<View style={styles.itemContent}>
<View style={styles.iconContainer}>
<Image source={Icon} style={styles.icon} />
</View>
<View style={styles.textContainer}>
<Text>This is the title </Text>
<View style={styles.downText}>
<View style={styles.priceText}>
<Text style={{marginRight: 10}}>$99.9</Text>
<Text>$99.9</Text>
</View>
<View style={styles.label}>
<Text>Label</Text>
</View>
</View>
</View>
</View>
</TouchableOpacity>
const styles = StyleSheet.create({
container: {
marginTop: 24,
},
itemContent: {
flexDirection: 'row',
borderBottomWidth: 1,
borderColor: 'e5e5e5'
},
iconContainer: {
padding: 10,
flex: 1,
},
icon: {
width: 40,
heigth: 40
},
textContainer: {
backgroundColor: 'whitesmoke',
flex: 7,
padding: 5
},
downText: {
marginTop: 10,
flexDirection: 'row',
justifyContent: 'space-between'
},
priceText: {
flexDirection: 'row',
},
label: {
textAlign: 'right',
backgroundColor: 'yellow',
padding: 3
}
});
For a reference, here is how it looks like:
PS: I would avoid writing inline styles.
I am trying to put a background color for an app I am making but it doesn't fit to all the screen and the bottom of it doesn't take the style.
This is my code:
<ScrollView>
<View style={styles.main}>
<View style={{ marginTop: 10 }}>
<Image style={{ flex: 1, height: 80, width: 80, alignSelf: 'center', marginTop: 23 }}
source={require('./../src/img/profile.png')} />
</View>
<View style={styles.fourblock}>
<TouchableOpacity
style={styles.redbox}
onPress={() => Actions.personal()}>
<Text style={styles.redboxText}>
Personal Detail
</Text>
</TouchableOpacity>
<TouchableOpacity
style={styles.redbox}
onPress={() => Actions.savedschools()}>
<Text style={styles.redboxText}>
Saved Schools
</Text>
</TouchableOpacity>
<TouchableOpacity
style={styles.redbox}
onPress={() => Actions.languages()}>
<Text style={styles.redboxText}>
Your Reviews
</Text>
</TouchableOpacity>
</View>
</View>
</ScrollView>
And this is the style I want to put:
main: {
//marginTop: 52,
backgroundColor: '#F8F8FF',
flex: 1,
},
<ScrollView> must be inside <View style={styles.main}>
so
<View style={styles.main}>
<ScrollView>
<View>
// .........
</View>
</ScrollView>
</View>
<Image style={{ flex: 1, height: 80, width: 80, alignSelf: 'center', marginTop: 23 }}
resizeMode: 'stretch', // or 'cover' . <<------------------
source={require('./../src/img/profile.png')} />
One more way you can fit view on all screen is using positioning absolute and make all left, top, right and bottom 0.
Here is an example.
<View style={{ position : 'absolute', top : 0, left : 0, right : 0,bottom : 0,}}</View>