How would a layout like this be done in react native? I understand that I can use justifyContent: space-between for the first row, but how would I make the second row fill all the available space, so that 488498 of the top row is aligned on the right side with the string at the bottom row?
The only way I could think of is by using a monospace font, but that's not an alternative in this case as I'm not allowed to change the font.
Thanks in advance.
You need two <View> tags, one that'll have the alignSelf: 'flex-start' style prop (so it has variable width) and one with justifyContent: 'space-between' style prop (so the text fills up the empty space).
Below is an example and here's an Expo Snack for you to fiddle with.
import * as React from 'react';
import { Text, View, StyleSheet } from 'react-native';
import { Constants } from 'expo';
export default class App extends React.Component {
render() {
return (
<View style={styles.mainContainer}>
<View style={styles.container}>
<Text style={styles.paragraph}>
afzender:
</Text>
<Text style={styles.paragraphRight}>
488498
</Text>
</View>
<Text>Very long text omg! This will surely be long.</Text>
</View>
);
}
}
const styles = StyleSheet.create({
mainContainer: {
alignSelf: 'flex-start',
alignContent: 'center',
justifyContent: 'center',
paddingTop: Constants.statusBarHeight,
backgroundColor: '#ecf0f1',
},
container: {
justifyContent: 'space-between',
flexDirection: 'row',
backgroundColor: 'red',
},
paragraph: {
fontSize: 18,
fontWeight: 'bold',
textAlign: 'left',
},
paragraphRight: {
fontSize: 18,
fontWeight: 'bold',
textAlign: 'right',
}
});
You could wrap 488498 with a div and give it text-align:right OR float:right using CSS...
Related
I want one of the images to be slightly larger than the other when pressed and viceVersa. I am trying to use useState to achieve this but I couldn't do it. I have attached an image below for your reference. Thank you! 😊🙏
Assuming only those 2 options, you can use state to set the value of blurred. and toggle the heights for the buttons base on the value of blurred
import React ,{useState}from 'react';
import { Text, View, StyleSheet, TouchableOpacity } from 'react-native';
import Constants from 'expo-constants';
export default function App() {
let [blur ,setBlur] = useState(true)
return (
<View style={styles.container}>
<TouchableOpacity
onPress={()=>{setBlur(true)}}
style={{ ...styles.button, backgroundColor:"red" , height: blur ? 60 : 40 }}>
<Text>Blurred</Text>
</TouchableOpacity>
<TouchableOpacity
onPress={()=>{setBlur(false)}}
style={{...styles.button, backgroundColor:"blue" , height: blur ? 40 : 60 }} >
<Text>Default</Text>
</TouchableOpacity>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: "row",
justifyContent: 'space-around',
alignItems: "center",
paddingTop: Constants.statusBarHeight,
backgroundColor: '#ecf0f1',
padding: 8,
},
button: {
justifyContent: "center",
alignItems:"center",
paddingVertical: 20,
paddingHorizontal: 30
}
});
Problem:
I have created a react native component with three touchable opacity components. It is showing like this.
This is how my code is looking.
/* eslint-disable prettier/prettier */
import React, {Component} from 'react';
import {View, Text, TouchableOpacity} from 'react-native';
import LangaugeCard from './LanguageCard';
import styles from '_styles/homescreen';
class Localization extends Component {
render() {
return (
<View style={styles.localization_container}>
<TouchableOpacity>
<LangaugeCard language="Hindi" />
</TouchableOpacity>
<TouchableOpacity>
<LangaugeCard language="English" />
</TouchableOpacity>
<TouchableOpacity>
<LangaugeCard language="French" />
</TouchableOpacity>
</View>
);
}
}
export default Localization;
My LanguageCard looks like this.
/* eslint-disable prettier/prettier */
import React from 'react';
import {View, Text} from 'react-native';
import styles from '_styles/homescreen';
const LanguageCard = (props) => {
const {language, instruction} = props;
return (
<View style={styles.langauge_card}>
<View style={styles.langauge_view}>
<Text style={styles.language}>{language}</Text>
</View>
<View style={styles.instruction_view}>
<Text style={styles.instruction}>{instruction}</Text>
</View>
</View>
);
};
export default LanguageCard;
This is my styling file.
/* eslint-disable prettier/prettier */
import {StyleSheet} from 'react-native';
const styles = StyleSheet.create({
container: {
flexDirection: 'column',
flex: 1,
backgroundColor: '#eeeeee',
height: '100%',
justifyContent: 'center',
},
localization_container: {
flex: 1,
},
langauge_card: {
backgroundColor: '#ffffff',
height: '46%',
marginLeft: '5%',
marginRight: '5%',
borderRadius:35,
flexDirection:'row',
elevation: 1,
},
langauge_view:{
backgroundColor:'#007aff',
marginTop: '8%',
marginBottom: '8%',
marginLeft:'8%',
borderRadius: 15,
width:'30%',
justifyContent: 'center',
},
language:{
fontFamily:'IskoolaPota',
fontSize:24,
textAlign:'center',
color:'#ffffff',
justifyContent:'center',
},
instruction_view:{
marginTop: '8%',
marginBottom: '8%',
marginLeft:'5%',
justifyContent: 'center',
},
instruction:{
color:'#444444',
fontSize: 15,
},
});
export default styles;
What I want to do is to reduce the space between touchable components and put all three buttons center of the screen I tried a lot to find a way to do so but It was unable to do so. Can someone help me to achieve this one by modifying the code? Thank you
I don't think its a good idea to use percentages as height in this case. Right now, your TouchableOpacity is going off the screen because you have height: '46%', Since you have 3 of those, its height is 138% + padding and margins.
I prefer to use flex and use justifyContent for aligning horizontally and align-items for aligning vertically
So in your case,
langauge_card:{
flex: 1,
}
langauge_view:{
flex: 1,
alignItems: "center",
justifyContent: "center"
}
instruction_view:{
flex: 1,
alignItems: "center",
justifyContent: "center"
}
You can play around wth flex, margin, and "flex-start", "center", "flex-end" for justify content and alignItem.
Hope this helped a bit.
Put these styles in localization_container
{
flex: 1,
alignItems: 'center',
JustifyContent: 'center'
}
These styles will make sure items inside the child's view will be shown in the center of the screen then remove the hight of your containers (if you have any) one by one, Try to avoid usage of % as this can unpredictable.
I'm trying to mimic a stack navigation header like below:
where the title is perfectly centered while there's a return button in the same row on the left.
I have tried to use justifyContent: 'space-between' with an empty View on the right side but the title will be off center to the right a bit. How should I approach this?
my renderHeader function:
_renderHeader = () => {
return (
<View style={styles.headerAbsolute}>
<View style={styles.bar}>
<ButtonBack
text={"Resumes"}
onPress={() => this._onNavBackHandler()}
/>
{true ? (
<Text style={styles.headingFixed}>{this.state.title}</Text>
) : ( null )}
<View/>
</View>
</View>
)
}
These are the styles:
bar: {
height: 55,
flexDirection: "row",
alignItems: 'flex-end',
justifyContent: 'space-between',
paddingHorizontal: 10,
paddingBottom: 5,
},
headingFixed: {
fontSize: TITLE_TERTIARY,
fontWeight: 'bold',
color: COLOR_DARK_SECONDARY,
backgroundColor: 'transparent',
},
I will make use of Flex here.
Please note that i used borderWidth in the below example only for reference to show how it looks.
I will have flexDirection as row and i will give a flex: 1 to all the views inside
App.js
import React, { Component } from 'react';
import {
View,
Text
}
from 'react-native';
class App extends Component{
render(){
return(
<View>
<View style={styles.navBar}>
<View style={styles.itemStyle}>
<Text style={{fontSize: 18, color: 'blue'}}>Resumes</Text>
</View>
<View style={styles.itemStyle}>
<Text style={{fontSize: 20, color: 'blue'}}>Settings</Text>
</View>
<View style={styles.itemStyle}></View>
</View>
</View>
)
}
}
const styles = {
navBar: {
marginTop: 42,
height: 36,
flexDirection: 'row',
alignItems: 'center'
},
itemStyle: {
flex: 1,
height: 36,
alignItems: 'center',
justifyContent: 'center',
borderWidth: 1
}
}
export default App;
You can play around in the inside views. Move the buttonBack inside one of the child views.
I can think of two ways to accomplish this, but neither seems quite ideal.
The first way is using a transparent element with fixed width and justifyContent: 'space-between', as you indicated:
<View style={{flexDirection: 'row', justifyContent: 'space-between'}}>
<Text style={{width: 80}}>left</Text>
<Text>center</Text>
<Text style={{width: 80, color: 'transparent'}}>right</Text>
</View>
Setting a fixed width to the elements on each side will cause the middle element to center horizontally.
An alternative approach is using absolute styling. Using percentages with left and right requires React Native >= 0.42.
<View>
<Text>Left</Text>
<Text style={{position: 'absolute', width: 50, left: '50%', marginLeft: -25}}>Center</Text>
</View>
I'm making simple To-Do application using React Native, just watching YouTube. The difference video and me is My version is latest RN, and simulating app in Android.
The code looks like same but my TextInput width is not 100%, but very small element as you can see
My code is below, How to fix it?
import React, {Component} from 'react';
import {
StyleSheet,
Text,
View,
TextInput,
} from 'react-native';
export default class Main extends Component {
render() {
return (
<View style={styles.wrapper}>
<View style={styles.topBar}>
<Text style={styles.title}>
To-Do List
</Text>
</View>
<View style={styles.inputWrapper}>
<TextInput
style={styles.input}/>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
wrapper: {
flex: 1,
justifyContent: 'flex-start',
alignItems: 'stretch'
},
topBar: {
padding: 16,
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#2ecc71'
},
title: {
color: 'white',
fontSize: 20
},
inputWrapper: {
padding: 10,
flexDirection: 'row',
alignItems: 'stretch',
backgroundColor: 'green',
borderColor: 'red'
},
input: {
height: 26,
borderRadius: 8,
backgroundColor: 'white'
}
});
Try removing the flexDirection style from inputWrapper.
See it here.
I'm building an app with React Native, and I was wondering how I'd go about coding 3 concentric circles like so:
They would need to be each be touchable.
There are many ways of achieving this. Even though this question isn't the most appropriate to be on StackOverflow, I did some code here to help you.
import React from 'react'
import {
StyleSheet,
TouchableOpacity,
View
} from 'react-native'
export default class AboutScreen extends React.Component {
render() {
return (
<View style={styles.container}>
<View style={styles.circlesContainer}>
<TouchableOpacity style={styles.circle_1} />
<TouchableOpacity style={styles.circle_2} />
<TouchableOpacity style={styles.circle_3} />
</View>
</View>
)
}
}
// Base radius.
const BASE_SIZE = 300
const styles = StyleSheet.create({
container: {
flex:1,
alignItems:'center',
justifyContent: 'center',
backgroundColor: '#E56A00'
},
circlesContainer:{
width: BASE_SIZE,
height: BASE_SIZE,
alignItems: 'center',
},
circle_1:{
top:0,
position: 'absolute',
width:BASE_SIZE,
height:BASE_SIZE,
borderRadius: BASE_SIZE/2,
backgroundColor: '#FF8100'
},
circle_2:{
top:BASE_SIZE*0.1, // The amount remaining
left:BASE_SIZE*0.1,
position: 'absolute',
width:BASE_SIZE*0.8, // 80% of the base size
height:BASE_SIZE*0.8,
borderRadius: BASE_SIZE/2,
backgroundColor: '#FF9D2E'
},
circle_3:{
top:BASE_SIZE*0.2,
left:BASE_SIZE*0.2,
position: 'absolute',
width:BASE_SIZE*0.6,
height:BASE_SIZE*0.6, // 60% of the base size
borderRadius: BASE_SIZE*0.6/2,
backgroundColor: '#FFFFFF'
},
})
The result on my code looked like this:
Be aware that there are a lot of ways to optimise this code, but at least it might be a good start to you.
Good luck!
You can use a View with a borderRadius, surrounded by another View, also with a borderRadius.
<View style={styles.borderExternal}>
<View style={styles.myCircle} />
</View>