Anu
Anu

Reputation: 1742

How to avoid collapsing of screen when keyboard opens in react native

In my app the landing page is login screen. I've done it using native-base ui kit.The login screen contains a logo and below there will be 2 input fields for username and password also login button. The problem is that whenever i clicked on the input field the login form get collapse under the logo. I will share the corresponding screens for it. I've tried the following method

android:windowSoftInputMode="adjustResize" within activity tag in AndroidManifest.xml. But still getting the same screen. Please have a look at the screens.

  1. Normal screen behavior https://i.sstatic.net/ZFqfI.png
  2. After keyboard opens https://i.sstatic.net/tZr1i.png

I've also tried KeyboardAvoidngView .Following is the code

render() {
    return (
      <KeyboardAvoidingView behavior="padding" style={{flex:1}}>
      <Container>
        <StatusBar barStyle="light-content" />

          <View style={styles.logoContainer}>
            <ImageBackground source={launchscreenLogo} style={styles.logo} />
          </View>
          <View
            style={{
              flex:1,
              marginBottom: 200,
              backgroundColor: "transparent"
            }}
          >
            <Form style={{margin:10}}>
          <Item  style={{margin:10}}>
           <Icon active name='person' />
           <Input
             placeholder='Username'
             style={{marginLeft:18, color:'#000'}}
             value={this.state.username}
             onChangeText={username => this.setState({ username })}
             />
         </Item>
         <Item style={{margin:10}} >
          <Icon active name='mail' />
          <Input
          placeholder='Email'
          style={{marginLeft:18, color:'#000'}}
          value={this.state.email}
          onChangeText={email => this.setState({email})}/>

        </Item>
        <Item  style={{margin:10}}>
         <Icon active name='ios-lock' />
         <Input
         placeholder='Password' style={{marginLeft:18, color:'#000'}}
         value={this.state.password}
         onChangeText={password => this.setState({ password })}
         />

       </Item>
       <Item  style={{margin:10}}>
        <Icon active name='ios-lock' />
        <Input
        placeholder='Repeat Password'
        style={{marginLeft:18, color:'#000'}}
        value={this.state.r_password}
        onChangeText={r_password => this.setState({ r_password })}/>

      </Item>
        </Form>

        <Button
          style={{ backgroundColor: "#809fff", alignSelf: "center", elevation:20}}
          onPress={() => this.props.navigation.navigate("Anatomy")}
        >
          <Text style={{fontWeight:'bold',fontSize:18}}>REGISTER</Text>
        </Button>
        </View>


          <View style={{ marginBottom: 80 }}>

          </View>

      </Container>
      </KeyboardAvoidingView>

    );
  }

Upvotes: 1

Views: 3661

Answers (1)

Abdul Kawee
Abdul Kawee

Reputation: 2727

You can use KeyboardAwareScrollView.

To add use npm i react-native-keyboard-aware-scroll-view --save

And then use it

<KeyboardAwareScrollView 
        keyboardShouldPersistTaps= 'always'
        style= {{ flex:1 }}  
        >
 <View>
  <TextInput />
 </View>
</KeyboardAwareScrollView>

For more info refer this. Hope this helps

Upvotes: 2

Related Questions