React Native Concentric circles - reactjs

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>

Related

How to achieve the ripple effect in React Native

In some applications, I see when a user is speaking the background of the user profile has these circles increasing and decreasing in radius.
How do I achieve this using React Native and Reanimated-v2?
You can achieve it using react-native-lottie easily
Working example: https://codesandbox.io/s/loving-leakey-0e5j5n
Find the free ripple effect that you want on
https://lottiefiles.com/search?q=ripple&category=animations&type=free for example https://lottiefiles.com/16138-ripple but you can search for specific keywords and use the one that looks like with audio syncing like this one https://lottiefiles.com/31698-audio-power
Download lottie json file (You will need an account for it) and you can change also layers color before download. This
Integrate it using
https://github.com/lottie-react-native/lottie-react-native into your
react-native app.
I have created working example here for you https://codesandbox.io/s/loving-leakey-0e5j5n styles won't be perfect in example but you can update it as per your need.
import React from "react";
import { StyleSheet, Text, View } from "react-native";
import LottieView from 'lottie-react-native';
function App() {
return (
<View style={styles.app}>
<View style={styles.content}>
<Text style={styles.title}>React Native Lottie</Text>
<View style={styles.lottieContainer}>
<LottieView source={require("./assets/ripple.json")} autoPlay loop />
</View>
</View>
</View>
);
}
const styles = StyleSheet.create({
app: {
flex: 1
},
content: {
alignItem: "center",
padding: 20,
position: "relative",
flex: 1,
justifyContent: "center"
},
title: {
fontWeight: "bold",
fontSize: "1.5rem",
marginVertical: "1em",
textAlign: "center",
zIndex: 1
},
lottieContainer: {
backgroundColor: "#5295d1",
position: "absolute",
justifyContent: "center",
top: 0,
bottom: 0,
left: 0,
right: 0,
zIndex: 0
}
});
export default App;

React Native : How can I create two TouchableOpacity components that swap height and width values when pressed?

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
}
});

How to remove spaces between Touchable Opacity components in React Native

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.

Aligning text rows on the right side in react native

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...

How to set 100% width to TextInput in React Native?

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.

Resources