TextInput got hides behind keyboard in react-native - react-native

I am creating chat UI in react native in which I want the first section (where the messages are shown) should be scrollable.
The TextInput should be at the bottom of screen and keyboard should be after it.
The UI is similar to WhatsApp chat screen. But I'm unable to re-create that UI.
I have also tried KeyboardAvoidingView from react-native but it doesn't works for me like it do.
App.js
import React, { useEffect, useState } from "react";
import {
ScrollView,
View,
Text,
Alert,
Dimensions,
Platform,
KeyboardAvoidingView,
TextInput,
TouchableOpacity,
} from "react-native";
import { Ionicons } from "#expo/vector-icons";
const App = () => {
const [message, setMessage] = useState([]);
return (
<View
style={{
display: "flex",
flex: 1,
justifyContent: "space-evenly",
alignItems: "center",
height: Dimensions.get("window").height,
width: Dimensions.get("window").width,
}}
>
<View
style={{
height: Dimensions.get("window").height * 0.8,
backgroundColor: "lightgrey",
width: Dimensions.get("window").width,
}}
>
<ScrollView></ScrollView>
</View>
<View
style={{
height: Dimensions.get("window").height * 0.2,
width: Dimensions.get("window").width,
display: "flex",
flexDirection: "row",
justifyContent: "space-evenly",
alignItems: "center",
padding: 25,
}}
>
<View style={{ flex: 4 }}>
<TextInput placeholder="Type Message ....." />
</View>
<TouchableOpacity>
<Ionicons name="md-send" size={30} color="#0af" />
</TouchableOpacity>
</View>
</View>
);
};
export default App;
I have added my code on expo snack.

I have run into this issue several times while working on react-native projects. I always find the native KeyboardAvoidingView module glitchy. So I use another package to make it work. I have tested it on your snack and it works fine.
The package is called react-native-keyboard-aware-scroll-view. It's
a lightweight package with an unpacked size of just 10kB.
It has several useful props that you can use to adjust the component. Check it out here.
Here is a link to the snack that I used to test your code.
import React, { useEffect, useState } from "react";
import {
ScrollView,
View,
Text,
Alert,
Dimensions,
Platform,
TextInput,
TouchableOpacity,
} from "react-native";
import { KeyboardAwareScrollView } from 'react-native-keyboard-aware-scroll-view'
import { Ionicons } from "#expo/vector-icons";
const App = () => {
const [message, setMessage] = useState([]);
return (
<KeyboardAwareScrollView
style={{
display: "flex",
flex: 1,
justifyContent: "space-evenly",
alignItems: "center",
height: Dimensions.get("window").height,
width: Dimensions.get("window").width,
}}
>
<View
style={{
height: Dimensions.get("window").height * 0.8,
backgroundColor: "lightgrey",
width: Dimensions.get("window").width,
}}
>
<ScrollView></ScrollView>
</View>
<View
style={{
height: Dimensions.get("window").height * 0.2,
width: Dimensions.get("window").width,
display: "flex",
flexDirection: "row",
justifyContent: "space-evenly",
alignItems: "center",
padding: 25,
}}
>
<View style={{ flex: 4 }}>
<TextInput placeholder="Type Message ....." />
</View>
<TouchableOpacity>
<Ionicons name="md-send" size={30} color="#0af" />
</TouchableOpacity>
</View>
</KeyboardAwareScrollView>
);
};
export default App;

use npm i react-native-keyboard-aware-scroll-view --save
import react-native-keyboard-aware-scroll-view
and then ..
<KeyboardAwareScrollView
contentContainerStyle={{
height: Dimensions.get("window").height * 2.25,
width: '100%'
}}
>
you can change *2.25 to change height

Related

Connecting dApps in embedded browser with custom made wallet in react native

What are the APIs that's available to do this, I've only found Wallet Connect that can do this, are there any other that I've missed ?
I've try integrating with Wallet Connect V1 sdk, but encounter a problem
TypeError: null is not an object (evaluating 'RNRandomBytes.seed')
is there any workaround on this ? did I missing something ?
the error triggers right after I imported Wallet Connect V1 sdk
the code :
import { useState, useEffect, useRef } from 'react';
import { StyleSheet, Text, View, Button, TextInput } from 'react-native';
import { WebView } from 'react-native-webview';
import { SafeAreaView } from "react-native-safe-area-context";
import WalletConnect from "#walletconnect/client";
export default function Login({ navigation }) {
return (
<SafeAreaView style={styles.container}>
<Text style={{ alignSelf: "center", marginTop: 10 }}>{secret?.address}</Text>
<View style={{ padding: 10, flexDirection: "row", alignItems: "center" }}>
<TextInput style={{ padding: 10, borderWidth: 1, borderRadius: 10, flex: 2, margin: 5 }} placeholder='WC Connection...' onChangeText={(e) => setWc(e)} />
<View style={{ margin: 5, flex: 1 }}>
<Button title='Connect'/>
</View>
</View>
<View style={{ margin: 10 }}>
<Button title='Generate Wallet' />
</View>
<WebView
source={{ uri: 'https://app.uniswap.org/#/swap' }}
style={styles.container}
/>
</SafeAreaView>
);
}
const styles = StyleSheet.create({
container: {
flex: 1
},
});

Button Styles not working in react native

I want to change button borderRadius,color, and padding but it doesn't work
this is my view
import * as React from 'react';
import { StatusBar } from 'expo-status-bar';
import { Button, StyleSheet, Text, View, Image} from 'react-native';
import {NavigationContainer} from '#react-navigation/native';
import {createStackNavigator} from '#react-navigation/stack';
function HomeScreen({navigation}) {
return(
<View
style={styles.container}>
<Text>Home Screen</Text>
<Button
style={styles.button}
title= "Go To Details"
onPress={()=> navigation.navigate('Details')}
/>
</View>
)
}
and this is my styles
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
button: {
width: 200,
marginTop: 20,
backgroundColor: "green",
padding: 15,
borderRadius: 50,
},
});
this is my screen
are there something that i miss?
I don't think so you can customize like your own with react native Button. But you can change something like title, color, etc. Check their doc https://reactnative.dev/docs/button
A better way is to make your own Button with your custom Style like this :
<TouchableOpacity onPress={()=> navigation.navigate('Details')}>
<View style={styles.button}>
<Text>Go To Details</Text>
</View>
</TouchableOpacity>
I'll suggest you to use View, because button from 'react-native' have some default styling and there can be some difficulty to override them, you can customise View like anything you want. just have a View and give them desired styles.
function HomeScreen({navigation}) {
return(
<View
style={styles.container}>
<Text>Home Screen</Text>
<View
style={styles.button}
title= "Go To Details"
onPress={()=> navigation.navigate('Details')}
/>
</View>
)
}
just give styles you want
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
button: {
width: 200,
marginTop: 20,
backgroundColor: "green",
padding: 15,
borderRadius: 50,
},
});

Icons with gradient colors

Is there any way I can add gradient colors to Icons in react-native / expo?
I want to make icons like those :
Tried this variant using MaskedView and LinearGradient packages https://github.com/react-native-linear-gradient/react-native-linear-gradient/issues/198#issuecomment-590821900. I use this snippet with Icon from native-base and this solution works perfectly for me.
import React from 'react'
import { Text, View, StyleSheet } from 'react-native'
import Icon from 'react-native-vector-icons/MaterialCommunityIcons'
import LinearGradient from 'react-native-linear-gradient'
import MaskedView from '#react-native-community/masked-view'
const size = 40
export function PowerOff({ ...rest }) {
return (
<View style={{ width: size }} {...rest}>
<MaskedView
style={{ flex: 1, flexDirection: 'row', height: size }}
maskElement={
<View
style={{
backgroundColor: 'transparent',
justifyContent: 'center',
alignItems: 'center',
}}>
<Icon
name="power"
size={size}
color="white"
style={styles.shadow}
/>
</View>
}>
<LinearGradient
colors={['#F7C650', 'rgba(247, 198, 80, 0.71)']}
style={{ flex: 1 }}
/>
</MaskedView>
</View>
)
}
const styles = StyleSheet.create({
shadow: {
shadowColor: 'black',
shadowOpacity: 0.5,
shadowRadius: 5,
shadowOffset: {
width: 0,
height: 1,
},
},
})

React Native Modal: Transparent Background & Layout Problem

I am using the React Native Modal, I want the background of the Modal
to be transparent and I want the Modal display to behalf of the
screen
How to achieve the same requirement, where I am going wrong?
Below is the code for the same, please have a look at this:
import React, { Component } from 'react'
import { Modal, View, Text, Dimensions, Platform, TouchableOpacity, Alert, StyleSheet, Button } from 'react-native'
import Icon from 'react-native-vector-icons/Entypo'
const windowWidth = Dimensions.get('window').width;
const windowHeight = Dimensions.get('window').height;
export class MyComponent extends Component {
render = () => {
const message = 'Do you want to upload the video now or wait until you are connected to wi-fi?'
return (
<Modal
animationType='slide'
transparent={true}
style={{backgroundColor: 'black'}}
>
<View style={styles.content}>
<View style={styles.closeBtn}>
<TouchableOpacity onPress={() => this.props.navigation.navigate('PreInspection_VideoPlayer')} style={styles.closeBtn}>
<Icon name="cross" color="#000" size={26} />
</TouchableOpacity>
</View>
<Text style={{
fontSize: 18,
fontFamily: 'Montserrat-Bold',
paddingTop: Platform.OS === 'android' ? 40 : 20,
paddingVertical: 10
}}>Warning! 🚨</Text>
<View style={{ paddingHorizontal: 40 }}>
<Text style={{ fontSize: 18, justifyContent: 'center', alignItems: 'center', textAlign: 'center' }}>{message}</Text>
</View>
<Button
title='Upload My Video'
style={styles.bigButtons}
onPress={() => { Alert.alert('Uploading Video') }}
/>
<Button
title='Upload Video Later'
style={styles.bigButtons}
onPress={() => { Alert.alert('Uploading Video Later') }}
/>
</View>
</Modal>
)
}
}
const styles = StyleSheet.create({
closeBtn: {
padding: 10
},
bigButtons: {
width: 240,
marginTop: 20
},
content: {
backgroundColor: 'red',
width: windowWidth * 0.8,
height: windowHeight * 0.7,
alignSelf: 'center',
top: windowHeight * 0.15,
borderRadius: windowHeight * 0.03,
alignItems: 'center',
justifyContent: 'center'
},
})
Any help would be appreciated. Thanks in advance :)
You can achieve this easily with React Native Community Modal
Here is an example:
import React, { useState } from "react";
import { Text, View, Dimensions } from "react-native";
import Modal from "react-native-modal";
const { width: ScreenWidth, height: ScreenHeight } = Dimensions.get("window");
const ModalExample = props => {
const [visibility, setVisibility] = useState(true);
return (
<View>
<Modal
backdropColor="transparent"
isVisible={visibility}
style={{
alignItems: "center",
justifyContent: "center"
}}
onBackdropPress={() => setVisibility(false)}
>
<View
style={{
borderRadius: 16,
alignItems: "center",
justifyContent: "center",
width: ScreenWidth * 0.7,
backgroundColor: "#fdfdfd",
height: ScreenHeight * 0.5
}}
>
<Text>I am the modal content!</Text>
</View>
</Modal>
</View>
);
};
export default ModalExample;

How put a image with auto size inside of a column using React Native?

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