React Native floating image - react-native

I want to make a floating image that looks like this, float on a carousel
1
I've done this
const styles = StyleSheet.create({
manualHeader: {
backgroundColor: color.light_blue,
height:150,
width:150,
borderRadius: 150/2,
alignItems: 'center',
},
manualImage: {
height: normalize(60),
width: normalize(60),
resizeMode: 'contain',
marginVertical: normalize(15),
},
});
<View style={styles.manualContainer}>
<View style={styles.manualHeader}>
<Image style={styles.manualImage} source={item.image} />
</View>
<View style={styles.manualBody}>
</View>
</View>
and it looks like this
!2

This is a simple example using margin. Also, we can use absolute positioning.
Also, I've created the below example in the snack.
https://snack.expo.io/Sk882TV1r
In the example below, just add your image source.
import * as React from 'react';
import { Text, View, StyleSheet, Image } from 'react-native';
// or any pure javascript modules available in npm
export default class App extends React.Component {
render() {
return (
<View style={styles.container}>
<Text style={styles.paragraph}>
Change code in the editor and watch it change on your phone!.
</Text>
<View style={styles.mybody}>
<View style={styles.imageContainer}>
<Image style={styles.imageStyle} source={{}}/>
</View>
<View style={styles.bodyContainer}>
<Text style={styles.bodyParagraph}>
Local files and assets can be imported by dragging and dropping them into the editor
Local files and assets can be imported by dragging and dropping them into the editor
Local files and assets can be imported by dragging and dropping them into the editor
Local files and assets can be imported by dragging and dropping them into the editor
</Text>
</View>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
backgroundColor: '#ecf0f1',
padding: 8,
},
imageContainer:{
marginTop: -70,
justifyContent: 'center',
alignItems: 'center'
},
imageStyle:{
height: 150,
width:150,
backgroundColor:'yellow',
borderRadius:150/2,
alignItems: 'center',
},
paragraph: {
margin: 24,
marginBottom:100,
fontSize: 18,
fontWeight: 'bold',
textAlign: 'center',
},
mybody:{
backgroundColor:'red'
},
bodyContainer: {
alignItems: 'center',
justifyContent: 'center',
padding: 24,
// backgroundColor:'red'
},
bodyParagraph:{
margin: 10,
marginBottom:100,
fontSize: 18,
fontWeight: 'bold',
textAlign: 'center',
}
});
The final output looks like this.
Thanks!

Related

How do I set my text like this in React Native?

I have tried textAlign="center" but it only aligns the text in the center but I want to align the whole text in the center including the last line
<Text
style={{
marginTop: 16,
alignSelf: 'center',
fontSize: 15,
width: '75%',
textAlignVertical: 'center',
justifyContent: 'center',
}}>
Making a luxurious lifestyle accessible for a generous group of
women is our daily drive.
</Text>
Here is the full example. please read about textAlign property.
import React, {useState} from 'react';
import { Text, View, StyleSheet, Button } from 'react-native';
const App = () => {
return (
<View style={styles.container}>
<Text
style={styles.text}>
Making a luxurious lifestyle accessible for a generous group of
women is our daily drive.
</Text>
</View>
);
};
export default App;
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems:"center",
padding: 8,
backgroundColor: 'white',
},
text:{
marginTop: 16,
alignSelf: 'center',
fontSize: 15,
textAlign:"center",
width: '75%',
textAlignVertical: 'center',
justifyContent: 'center',
}
});

ReactNative - Button Not Visible

I'm having an issue where I'm creating a simple button that isn't visible (but it is there because I'm able to trigger the onPress method) within this view, but I am able to create other components within this View that appear. My code in App.js is below, any help would be greatly appreciated!
import React from 'react';
import {
View,
StyleSheet,
Text,
TouchableOpacity,
} from 'react-native';
export default function App() {
return (
<View style={{
flex: 1,
justifyContent: "center",
alignItems: "center",
}}>
<TouchableOpacity style={styles.button} onPress={() => console.log("Clicked")}>
<Text style={styles.text}>Login</Text>
</TouchableOpacity>
</View>
);
}
const styles = StyleSheet.create({
button: {
color: "tomato",
borderRadius: 25,
justifyContent: "center",
alignItems: "center",
padding: 15,
height: 50,
width: "100%",
},
text: {
color: "#fff",
fontSize: 18,
fontWeight: "bold",
textTransform: "uppercase",
}
})
You have a white button with white text in front of a white background so it all blends in. Look at this expo snack and the code below to see why (https://snack.expo.dev/#heytony01/thankful-juice-box)
import React from 'react';
import {
View,
StyleSheet,
Text,
TouchableOpacity,
} from 'react-native';
export default function App() {
return (
<View style={{
flex: 1,
justifyContent: "center",
alignItems: "center",
}}>
<TouchableOpacity style={styles.button} onPress={() => console.log("Clicked")}>
<Text style={styles.text}>Login</Text>
</TouchableOpacity>
</View>
);
}
const styles = StyleSheet.create({
button: {
borderColor:"red", // give border color
borderWidth:2, // give border width
color: "tomato",
borderRadius: 25,
justifyContent: "center",
alignItems: "center",
padding: 15,
height: 50,
width: "100%",
},
text: {
color: "black", // change font to black
fontSize: 18,
fontWeight: "bold",
textTransform: "uppercase",
}
})
I was setting the color of the button instead of the backgroundColor

Flex is not splitting components equally in react native

I'm trying to make a layout like this:
In order to do so, I've made two components named HalfWidthFullHeightCard and HalfWithHalfHeightCard.
I've created the HalfWidthFullHeightCell component as?
<TouchableOpacity onPress={pressEvent}>
<ImageBackground
source={sourcePath}
imageStyle={{ borderRadius: 8, resizeMode: 'cover', width: '100%' }}
style={styles.halfWidthCard}>
<Text style={styles.halfWidthCardHeading}>{heading}</Text>
<Text style={styles.halfWidthCardText}>{cardText}</Text>
</ImageBackground>
</TouchableOpacity>
...
halfWidthCard: {
backgroundColor: colors.brightYellow,
marginBottom: 10,
borderRadius: 8,
},
Based on the cardText the width of the card is calculated automatically and in the halfWidthCardText StyleSheet I've only had padding: 10
Next for HalfWithHalfHeightCard everything is the same except for the styling which is:
...
smallHalfWidthCard: {
backgroundColor: colors.brightYellow,
borderRadius: 8,
marginBottom: 10
},
smallHalfWidthCardHeading: {
padding: 10,
},
smallHalfWidthCardText: {
padding: 10,
},
Where I'm putting both of these components together I'm doing:
<ScrollView contentContainerStyle={{padding: 15}}>
<View style={{flexDirection: 'row',}}>
<HalfWidthFullHeightCell />
<View>
<HalfWithHalfHeightCell />
<HalfWithHalfHeightCell />
</View>
</View>
</ScrollView>
Now there are two problems:
Consider the gray area as the width of the device. The HalfWidthFullHeightCard takes 100% of the space and
The HalfWithHalfHeightCard are outside of the screen and also not of the same height as HalfWidthFullHeightCard.
So, how can I make these components flexible so that they adapt to the layout as screen size changes?
I would have made it like this
import * as React from 'react';
import { Text, View, StyleSheet, Dimensions } from 'react-native';
const ScreenWidth = Dimensions.get('window').width;
export default function App() {
return (
<View style={styles.container}>
<View style={styles.WholeBox}>
<View style={styles.Block}></View>
<View style={{ flex: 1 }}>
<View style={styles.Block}></View>
<View style={styles.Block}></View>
</View>
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
backgroundColor: '#ecf0f1',
},
WholeBox: {
width: ScreenWidth,
height: 300,
flexDirection: 'row',
},
Block: {
flex: 1,
backgroundColor: '#DDA73A',
margin: 6,
borderRadius: 8,
},
});
Working Example Here

TextInput full view in row direction

I ma new to React native development. In my application I have login screen. In laogin screen I have two text inputs along with images. I have taken image and Text input in one view and given flex direction as row. And I have given text input as alignSelf stretch. So here my issue is I need full length of text input along with image. But if I removed flex direction then will get full length of the screen. The following is the code
import React, {Component} from 'react';
import {StyleSheet, Text, View,TextInput,TouchableOpacity,StatusBar,Image} from 'react-native';
export default class App extends Component {
static navigationOptions = {
title: "Welcome",
header: null,
}
render() {
// const { navigate } = this.props.navigation
return (
<View style={styles.container}>
<StatusBar
barStyle="light-content"
backgroundColor="#2f5597"
/>
<Image source={require('../../app/images/ic_accountlist.png')} style={styles.logo}/>
<View style={styles.loginView}>
<View >
<Image source={require('../../app/images/ic_userid.png')} />
<TextInput placeholder="Acct No/User Id" style={styles.textInput} underlineColorAndroid={'rgb(0,0,0)'}></TextInput>
</View>
<View style={styles.user}>
<Image source={require('../../app/images/ic_password.png')} />
<TextInput placeholder="Password" style={styles.textInput} underlineColorAndroid={'rgb(0,0,0)'}></TextInput>
</View>
</View>
<TouchableOpacity style={styles.btnLogin} onPress={this.login}><Text style={{color: 'white'}}>Log In</Text></TouchableOpacity>
</View>
);
}
login=()=>{
// alert("testing......");
this.props.navigation.navigate('Profile');
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#F5FCF0',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
color:'#FF0000',
},
textInput: {
alignSelf: 'stretch',
padding: 10,
marginLeft: 5,
marginRight:0,
},
btnLogin:{
borderRadius: 10,
backgroundColor: '#2f5597',
padding: 10,
marginTop: 20,
paddingLeft: 50,
paddingRight: 50,
alignSelf: 'center',
},
user:{
flexDirection: 'row',
},
logo :{
marginTop: 100,
alignSelf: 'center',
},
loginView:{
marginTop: 60,
}
});
And the following is the output I am getting
And here I need the text input full along with image. Like the below
There is no need to use the alignSelf property. Instead, you have to use flex: 1:
textInput: {
flex: 1,
padding: 10,
marginLeft: 5,
marginRight: 0,
},
flex: 1 is a shorthand notation, so you could also use explicitly flexGrow: 1 to achieve the same effect:
textInput: {
flexGrow: 1,
padding: 10,
marginLeft: 5,
marginRight: 0,
},
You can read more about flex on MDN.
Result (ignore the different icon):
You need to add flex: 1 to textInput style
textInput: {
alignSelf: 'stretch',
padding: 10,
marginLeft: 5,
marginRight:0,
flex: 1
}

Invalid prop types: Prop.style key fontSize supplied to view

I've built a screen which just contains content in the center of the screen and includes background image. I checked the code but haven't found something that wrong. Here is the code:
import React, {Component} from "react";
import {Image} from "react-native";
import {
Content,
Button,
Text,
Icon,
} from "native-base";
class ThankyouScreen extends Component {
render() {
return (
<Image style={styles.image} source={require("../img/cover.jpg")}>
<Content
contentContainerStyle={{
alignSelf: "center",
justifyContent: "center",
flex: 1,
}}
>
<Icon name="checkmark" style={styles.icon} />
<Text style={styles.text}> Thank You For Joining Us!!</Text>
<Button
bordered
style={styles.button}
onPress={() => this.props.navigation.navigate("Home")}
>
<Text style={{color: "#42e5f4"}}> Back To Home</Text>
</Button>
</Content>
</Image>
);
}
}
const styles = {
image: {
flex: 1,
alignSelf: "stretch",
width: undefined,
height: undefined,
},
icon: {
justifyContent: "center",
alignSelf: "center",
fontSize: 90,
fontWeight: "bold",
color: "#42e5f4",
},
text: {
justifyContent: "center",
alignSelf: "center",
fontSize: 20,
color: "#42e5f4",
},
button: {
alignSelf: "center",
marginTop: 15,
fontSize: 10,
fontWeight: "bold",
color: "#0dc49d",
borderColor: "#42e5f4",
},
};
export default ThankyouScreen;
Error looks strange and its difficult to understand why it causes. What's wrong on this code and how it can be solved?
This might be a problem with NativeBase. There is a discussion for this in github