I am new at developing React Native application. I want to align multiple(maybe three) React Native Element 'Tile' in a single row, something like 2X3 matrix style. I am trying to use flexbox but is not able to achieve the result.
Here's my code:
import React, { Component } from 'react';
import { Text, View, TextInput, StyleSheet } from 'react-native';
import { Constants } from 'expo';
import { Tile } from 'react-native-elements';
export default class Tel extends Component {
render() {
return (
<View style={styles.tileStyle}>
<View style={{width: 20, height: 20,}}>
<Tile
featured
caption="Text"
/>
</View>
<View style={{width: 20, height: 20,}}>
<Tile
featured
caption="Some Caption Text"
/>
</View>
<View style={{width: 20, height: 20,}}>
<Tile
featured
caption="Some"
/>
</View>
<View style={{width: 20, height: 20,}}>
<Tile
featured
caption="Text"
/>
</View>
<View style={{width: 20, height: 20,}}>
<Tile
featured
caption="Some Caption Text"
/>
</View>
<View style={{width: 20, height: 20,}}>
<Tile
featured
caption="Some"
/>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
tileStyle: {
flex: 1,
flexDirection: 'row',
// alignItems: 'center',
// justifyContent: 'center',
paddingTop: Constants.statusBarHeight,
backgroundColor: '#ecf0f1',
}
});
All I can get three tiles in column wise and no width or height of them.
Please help out with the solution.
Use flex-wrap. Get width of the window in the constructor and use it in width prop of the Tile to get the proportional width you want.
import {
View,
Dimensions,
StyleSheet
} from "react-native";
import { Tile } from 'react-native-elements';
export class Screen extends Component {
constructor() {
super()
this.state = {
width: Dimensions.get('window').width
}
}
render() {
const { width } = this.state;
return (
<View style={{
flex: 1,
flexWrap: 'wrap',
flexDirection: 'row',
backgroundColor: '#ecf0f1'
}}>
<Tile
width={width/3}
featured
caption="1"
/>
<Tile
width={width/3}
featured
caption="2"
/>
<Tile
width={width/3}
featured
caption="3"
/>
<Tile
width={width/3}
featured
caption="4"
/>
</View>
);
}
}
Related
I'm creating a react native app and I need to create a curved screen design. How to create a curve design like the below screen.
This is what I tried here,
My sample codes,
import React from 'react';
import {
StyleSheet,
Text,
View,
StatusBar,
} from 'react-native';
import {
widthPercentageToDP as wp,
heightPercentageToDP as hp,
} from 'react-native-responsive-screen';
export default class Home extends React.Component {
render() {
return (
<View style={styles.container}>
<StatusBar hidden={true} />
<View style={styles.container1}>
<Text style={styles.name}>Container1</Text>
</View>
<View style={styles.container2}>
<Text style={styles.name}>Container1</Text>
</View>
<View style={styles.container3}>
<Text style={styles.name}>Container1</Text>
</View>
<View style={styles.container4}>
<Text style={styles.name}>Container1</Text>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
},
container1: {
backgroundColor: '#3d06d6',
height: hp('25%'),
padding: 10,
borderBottomLeftRadius:100,
},
container2: {
backgroundColor: '#0cad95',
height: hp('25%'),
padding: 10,
borderBottomLeftRadius:100,
},
container3: {
backgroundColor: '#d6a606',
height: hp('25%'),
padding: 10,
borderBottomLeftRadius:100,
},
container4: {
backgroundColor: '#06bad6',
height: hp('25%'),
padding: 10,
borderBottomLeftRadius:100,
},
});
I want to curve my container edges like the first picture. I am able to make curves of the bottom edges. And How can I make curve designs like the first image? and also how can I add some animations into the container when I touch the container to open another screen? Is there any way to do this design using any style with "borderRadius" or any other way? And I don't want to use any library like react-native-svg.
If you need a Linear Gradient as the background. Just giving the color of the next box will not be the best option.
Assume "value" as the border-radius. ie the curve
Give a marginTop and paddingTop for each container.
marginTop : -value,
paddingTop : value,
Also assign zIndex in decending order for each container.
You can set zIndex with respect to the index, if you are looping through an array.
Result
Sample code
import React from 'react';
import {View, StyleSheet, Text} from 'react-native';
import LinearGradient from 'react-native-linear-gradient';
const data = ['', '', ''];
const App = () => {
return (
<View style={styles.container}>
{data.map((item, index) => (
<View style={[styles.item, {zIndex: data.length - index}]}> // setting zIndex here
<LinearGradient
style={StyleSheet.absoluteFill}
colors={['#4c669f', '#3b5998', '#192f6a']}
/>
</View>
))}
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
zIndex: 1,
},
item: {
height: 250,
borderBottomLeftRadius: 100, // logic goes here
marginTop: -100, // move container
paddingTop: 100, // move inner item down
overflow: 'hidden',
},
});
export default App;
You can simply wrap your view inside a normal view and set the background colour of the next box which will give the output you need.
<View style={{ backgroundColor: '#0cad95' }}>
<View style={styles.container1}>
<Text style={styles.name}>Container1</Text>
</View>
</View>
<View style={{ backgroundColor: '#d6a606' }}>
<View style={styles.container2}>
<Text style={styles.name}>Container1</Text>
</View>
</View>
I have a row with two-column and want to put an image in one of the columns with full size.
import React from 'react';
import { mapping, light as lightTheme } from '#eva-design/eva';
import { View, ImageBackground, Image, StyleSheet } from 'react-native';
import { ApplicationProvider, Layout } from 'react-native-ui-kitten';
import { Input, InputProps } from 'react-native-ui-kitten';
import { Text, Button } from 'react-native-ui-kitten';
const App = () => (
<ApplicationProvider
mapping={mapping}
theme={lightTheme}>
<View style={styles.box}>
<View style={{width: '50%', backgroundColor: 'powderblue'}}>
<Image
resizeMode="contain"
source={require('../ProjectName/assets/image.jpg')}
style={styles.canvas} />
</View>
<View style={{width: '50%', backgroundColor: 'skyblue'}}>
<Input />
<Button />
</View>
</View>
</ApplicationProvider>
);
const styles = StyleSheet.create({
box: {
flex: 1,
flexDirection: 'row',
width: 'auto',
maxHeight: 200,
backgroundColor: 'red',
},
canvas: {
width: '100%',
},
});
export default App;
Structure:
ApplicationProvider
View
View
Image
View
Input
Button
My image is out of the column and box
!
Please guide me to fix it.
import React, { Component } from 'react';
import {
Platform,
StyleSheet,
Image,
Text,
View,
ImageBackground
} from 'react-native';
export default class App extends Component<{}> {
render() {
return (
<View style={{flex: 1, flexDirection: 'column', backgroundColor: 'black'}}>
<View style={{flex: 1/3, height: 100, flexDirection: 'row', backgroundColor: 'red'}}>
<View style={{flex: 1/2, backgroundColor: 'yellow'}}>
<Image style={{flex: 1, width: '100%', height: 'auto'}} source={require('./assets/image.png')}/>
</View>
<View style={{flex: 1/2, backgroundColor: 'pink'}}>
</View>
</View>
</View>
);
}
}
import React, { Component } from 'react';
import { AppRegistry, StyleSheet, Text, View, ScrollView } from 'react-native';
export default class App extends Component {
render() {
return (
<View style={styles.container}>
<ScrollView>
<View style={styles.top} />
<View style={styles.contentContainer}>
<View style={styles.content}>
<Text>Item1</Text>
</View>
<View style={styles.content}>
<Text>Item2</Text>
</View>
<View style={styles.content}>
<Text>Item3</Text>
</View>
</View>
</ScrollView>
<View style={[styles.tabbar]} />
</View>
);
}
}
var styles = StyleSheet.create({
container: {
flex: 1,
},
top: {
height: 346,
backgroundColor: '#0066cc',
},
contentContainer: {
flex: 1,
justifyContent: 'space-around',
},
content: {
padding: 20,
borderWidth: 1,
borderColor: 'red',
},
tabbar: {
height: 50,
backgroundColor: '#eee',
},
});
Please see attached screenshot for my issue. I'm providing a Snack link with all the code.
Note that when I remove ScrollView the items align as I need them to be but removing ScrollView is not an option.
https://snack.expo.io/SJQnkXGS7
Either give the ScrollView or the contentContainer a predefined height:
<ScrollView contentContainerStyle={styles.container}>
or
<View style={[styles.contentContainer, {height: Dimensions.get('window').height - 396}]}>
I use react-native-swiper and i want to add a text above my image.
I try to set style container image paragraph, it is still not working.
What should i do about the style setting ?
Any help would be appreciated. Thanks in advance.
import React, { Component } from 'react';
import { View, Text, Image, Dimensions } from 'react-native';
import Swiper from 'react-native-swiper';
const dimensions = Dimensions.get('window').width;
class About extends Component {
render() {
return (
<View style={{ flex: 1 }}>
<Swiper
style={styles.wrapper}
height={200}
paginationStyle={{bottom: 10}}
>
<View style={styles.container}>
<Image style={styles.image} source={require('../img/home_service_bg1.jpg')} />
<Text style={styles.paragraph}>How to let me show above the img.</Text>
</View>
<View style={{ flex: 1 }}>
<Image source={require('../img/home_service_bg2.jpg')} style={styles.img}/>
</View>
</Swiper>
<View style={{ flex: 2 }}>
<Text>Hi</Text>
</View>
</View>
);
}
}
const styles = {
wrapper: {
},
container: {
flex: 1,
alignItems: 'stretch',
justifyContent: 'center',
},
image: {
flexGrow:1,
height:null,
width:null,
alignItems: 'center',
justifyContent:'center',
},
paragraph: {
textAlign: 'center',
},
};
export default About;
Here is my situation now:
I hope it becomes like this:
position="absolute" will make it work as Android RelativeLayout.
If you want set view on top set marginTop to 0.
After seeing your requirement. You can simply use ImageBackground
<ImageBackground source={...} style={{width: '100%', height: '100%'}}>
<Text>Inside</Text>
</ImageBackground>
See facebook docs
How to show two views per rows in scollview in React native?
It is difficult to change the large framework since I made a view by pulling the json with module.
I would like to show views in the scrollview in the form shown below.
enter image description here <-- image link
I’d be glad if you could help me.
** If it have no idea in current method, you can give me a new idea.
this is code (const styles skipped)
import React, { Component } from 'react';
import { StyleSheet, View, Text, Image, StatusBar, FlatList, ScrollView, TouchableOpacity, Button, Dimensions } from 'react-native';
import logoImg from '../../images/logo.png';
import SearchInput, { createFilter } from 'react-native-search-filter';
import Icon from 'react-native-vector-icons/FontAwesome';
import Icon2 from 'react-native-vector-icons/Feather';
import promotion_list from '../../data/market_list.js';
const KEYS_TO_FILTERS = ['name', 'subject'];
const myIcon = (<Icon name="times" size={25} color='#949494' />)
export default class Market extends React.Component {
constructor(props) {
super(props);
this.state = {
searchTerm: ''
}
}
searchUpdated(term) {
this.setState({ searchTerm: term })
}
render() {
const filteredlists = promotion_list.filter(createFilter(this.state.searchTerm, KEYS_TO_FILTERS))
return (
<View style={styles.SearchList}>
<View style={{ flexDirection: 'row', margin: 10, padding: 10, height: 40, borderRadius: 100, backgroundColor: '#f5f5f5' }}>
<Icon name="search" size={20} color='#949494' style={{ flex: 0, marginRight: 10 }} />
<SearchInput
clearIcon={myIcon}
clearIconViewStyles={{ position: 'absolute', right: 6 }}
onChangeText={(term) => { this.searchUpdated(term) }}
placeholder="Search"
inputViewStyles={{ flex: 1 }}
/>
</View>
<View style={{justifyContent: 'center', alignItems: 'center'}}>
<Image style={{width:390, height:180}} source={require("../../images/market/topview.png")} />
</View>
<View>
<Text style={{marginLeft:15, marginTop:10, marginBottom:10, fontWeight:'bold', fontSize:20, color: '#494a51'}}>Your Partners</Text>
</View>
<ScrollView style={styles.ScrollView}>
{filteredlists.map(plist => {
function getImage(img_name) {
switch (img_name) {
case "1.png": return require("../../images/par_logo/1.png");
case "2.png": return require("../../images/par_logo/2.png");
case "3.png": return require("../../images/par_logo/3.png");
case "4.png": return require("../../images/par_logo/4.png");
case "5.png": return require("../../images/par_logo/5.png");
case "6.png": return require("../../images/par_logo/6.png");
case "7.png": return require("../../images/par_logo/7.png");
case "p1.png": return require("../../images/promotion_feed/1.png");
case "p2.png": return require("../../images/promotion_feed/2.png");
case "p3.png": return require("../../images/promotion_feed/3.png");
case "p4.png": return require("../../images/promotion_feed/4.png");
case "p5.png": return require("../../images/promotion_feed/5.png");
}
}
return (
<TouchableOpacity activeOpacity={1} key={plist.id} style={styles.ListItem}>
<View style={{ paddingRight: 10, paddingLeft: 10, height: 50, flexDirection: 'row', alignItems: 'center' }}>
<Image style={{ marginRight: 10, width: 30, height: 30, resizeMode: Image.resizeMode.contain }} source={getImage(plist.src1)} />
<Text style={{ fontWeight: 'bold' }}>{plist.name}</Text>
</View>
<View style={{margin:0}}>
<TouchableOpacity onPress={() => { alert("you clicked me") }}>
<Image style={{}} source={getImage(plist.src2)} />
</TouchableOpacity>
</View>
</TouchableOpacity>
)
})}
</ScrollView>
</View>
)
}
}
One possible solution is to use a FlatList which is inherited from ScrollView and use the numColumns prop. FlatList