I have a page with two inputs and a 'Next' button. I want to move my container up when keyboard is active so that the 'Next' button is also visible when the keyboard is open. I've used KeyboardAvoidingView but the content is not moving when the keyboard is active. What seems to be the issue here?
Here is my code
<View style={{height: '100%'}}>
<LinearGradient colors={['#29aae1', '#4bc1b6']} style={{height: '100%'}}>
<KeyboardAvoidingView
behavior="padding"
keyboardVerticalOffset={0}
style={{padding:10}}
>
<ScrollView style={{height:'100%'}}>
<Text style={styles.signin_info}>Let's get you signed in. Provide your first name and last
name.
Do not
worry, when you comment your real name won't be visible ;)</Text>
<Field label="First name" name="signUpFirstName" component={this.renderInput}/>
<Field label="Last name" name="signUpLastName" component={this.renderInput}/>
<TouchableOpacity block primary style={styles.nextBtn_active}
onPress={Actions.signUp_email}><Text
style={{fontFamily: 'gotham', fontSize: 18, color: 'white'}}
allowFontScaling={false}>Next</Text></TouchableOpacity>
</ScrollView>
</KeyboardAvoidingView>
</LinearGradient>
</View>
If you already ScrollView inside KeyboardAvoidingView, consider using react-native-keyboard-aware-scroll-view instead.
Your KeyboardAvoidingView component is not enabled !!! enabled property is mandatory:
<KeyboardAvoidingView
behavior="padding"
keyboardVerticalOffset={0}
style={{padding:10}}
enabled // <-- this property is required
>
See: enabled
I am new to React Native so this might be a silly question.
What I am trying to get is something like this :
Where I have a parent view with 2 children, Image and Text, aligned vertically to the left. What I want is the parent view to cover only the width of its children. But instead what I get is this :
The parent view stretches to the complete width of the mobile screen.
Here is my code :
render () (<View style={{backgroundColor: '#333333'}}>
<Image source={btnImageSource} />
<Text>Some label</Text>
</View>);
Something similar to Android's 'wrap-content' width value.
You will be able to do this using flex. I found this guide to be very helpful when working with flex.
A good starting point would be to create a parent View that is flexDirection: row, justifyContent: flex-start
render () (
<View style={{justifyContent: 'flex-start', flexDirection: 'row'}}>
<View style={{backgroundColor: '#333333'}}>
<Image source={btnImageSource} />
<Text>Some label</Text>
</View>
</View>
);
New to react native, trying to place text-box above scrollable tab view
export default React.createClass({
render() {
return
<View>
<TextInput
style={{height: 50}}
placeholder="Enter!"
onChangeText={(searchText) => this.setState({searchText})}/>
<ScrollableTabView
style={{marginTop: 10, }}
initialPage={1}
renderTabBar={() => <FacebookTabBar />}
>
<ScrollView tabLabel="ios-search" style={styles.tabView}>
<View style={styles.card}>
<TextInput
style={{height: 50}}
placeholder="Enter!"
onChangeText={(searchText) => this.setState({searchText})}/>
</View>
</ScrollView>
</ScrollableTabView>
</View>
;
},
});
Scrollable tab view doesn't show up with above code, I am unable to understand why, please advice. I am trying to make something like Linkedin app.
<ScrollableTabView> is styled with flex: 1, so you need to explicitly set flex: 1 to its parent like this in order to show it:
return (
<View style={{flex: 1}}>
...
</View>
);
Here is a quote from React Native documentation regarding Flex:
A component can only expand to fill available space if its parent has
dimensions greater than 0. If a parent does not have either a fixed
width and height or flex, the parent will have dimensions of 0 and the
flex children will not be visible.
Here I am trying a simple code but the scroll view is not working if kept inside another view. Code is like this:
return(
<View>
<Toolbar title={this.props.title}>
</Toolbar>
<ScrollView>
<HomeScreenTop />
<HomeScreenBottom navigator={navigator}/>
</ScrollView>
</View>
);
But if scroll view kept as parent view it works perfectly. Code is as below:
return(
<ScrollView>
<Toolbar title={this.props.title}>
</Toolbar>
<HomeScreenTop />
<HomeScreenBottom navigator={navigator}/>
</ScrollView>
);
Now the problem is I don't want my toolbar to scroll up and down, I just want the contents below the toolbar to move. How can I achieve that?
And next question: Is scroll view has to be parent view to be returned to work?
Use the property flexGrow in the style, flex didnt worked for me.
<ScrollView contentContainerStyle={{ flexGrow: 1 }}>
...
</ScrollView>
you should wrap toolbar in view like this:
<View><Toolbar/></View>
Wrap those component which you want to scroll inside scrollview as:
<ScrollView>your expected components...</ScrollView>
And provide flex to root view as:
<View style={{flex:1}}>
Finally your code will run as you expected.
Top <View> must has style flex:1, and also <ScrollView> has too
This was driving me crazy because I added <view> and <ScrollView> like everyone suggested but nothing worked. Then I closed the terminal then rebuild the application again using:
npx react-native run-android
Then, it started working. I hope this saves someone's sanity :)
FYI, just hitting r (or refreshing) was not working either. I had to rebuild it.
For me, Scroll View wrapped with TouchableWithoutFeedback was causing scroll view to not function at times.
I set height property for style . then it's work for me .
My code :
<View style={styles.listWrapper}>
<ScrollView >
<XYZ/>
</ScrollView>
</View>
Styles :
listWrapper:{
// flex:1,
// flexGrow:1,
width:'80%',
height:300,
},
Dont use contentContainerStyle on Scroll View Just use simple style prop for styling of scroll View . You will not face any problem.
None of these solution worked for me, after very long I found that height of a View inside the code is causing the problem so instead of
height:'50%'
I change it to
height:300
and it worked, as show below:
<ScrollView>
...
<View
style={{
flexDirection: 'row',
height: 100,
justifyContent: 'space-evenly',
height:300,
backgroundColor: '#fff',
padding: 20,
margin: 5,
}}>
...
</ScrollView>
If you are using <Form> from native base and you want to scroll
then use KeyboardAwareScrollView
<KeyboardAwaareScrollView enableOnAndroid={true}>
.
.
</KeyboardAwareScrollView>
this will work on Android
I have solved in this way:
const windowWidth = Dimensions.get("window").width;
const windowHeight = Dimensions.get("window").height;
<View
style={{
overflow: "hidden",
flex: 1,
borderTopStartRadius: 40,
borderTopEndRadius: 40,
flexGrow: windowHeight,
}}
>
<ScrollView
contentContainerStyle={{
flexGrow: windowHeight,
backgroundColor: Colors.primary_color,
padding: 10,
}}
>
</View>
...
</ScrollView>
Update 2020-02: It will work with React.Fragment <> and </> or <React.Fragment> and </React.Fragment>. So Try this.
return(
<>
<Toolbar title={this.props.title}>
</Toolbar>
<ScrollView>
<HomeScreenTop />
<HomeScreenBottom navigator={navigator}/>
</ScrollView>
</>
);
Use react fragments instead of a View to wrap your scroll view it worked for me
<>
<ScrollView>
..........
</ScrollView>
</>
if you are using position: "absolute", delete it and try.
You can try
<SafeAreaView>
<ScrollView>
</ScrollView>
</SafeAreaView>
I've been stuck on the following issue for awhile now. I have a ToolbarAndroid component and a TextInput component:
<View style={{flex: 1}}>
<ToolbarAndroid
navIcon={require('./ic_menu_black_24dp.png')}
onIconClicked={() => this.drawer.openDrawer()}
style={styles.titleView}
title="Blah"
titleColor="black"
/>
<ScrollView>
<TextInput
{...this.props}
multiline={true}
onChange={(event) => {
this.setState({
text: event.nativeEvent.text,
height: event.nativeEvent.contentSize.height,
color: 'black'
});
}}
style={{fontSize: 14, textAlignVertical: 'top', height: Math.max(60, this.state.height)}}
value={this.state.text}
placeholder="Spread your message!"
placeholderTextColor='lightslategray'
underlineColorAndroid='white'
enablesReturnKeyAutomatically={true}
returnKeyType="done"
maxLength={1000}
/>
</ScrollView>
</View>
My TextInput box is auto-expanding which means it grows as it types. The input also re-focuses on top of the keyboard as it grows. However, as it grows to be bigger than my screen, it doesn't scroll behind the Toolbar. Instead, it pushes the toolbar off the screen. I'd like it to scroll behind the toolbar but I can't seem to get it to work.
Any help would be greatly appreciated.
Thanks