React-native: Height adjustment when keyboard appears is wrong

Created on 17 Nov 2016  路  10Comments  路  Source: facebook/react-native

Hi, I am trying to shorten the screen when the keyboard is active. But it always adjust it weirdly in android, it works fine in iOS.

Before keyboard is active:
whatsapp image 2016-11-17 at 6 10 39 pm

After keyboard is active:
whatsapp image 2016-11-17 at 6 07 27 pm

I am using RN 0.37, this also happens when i am starting from scratch:

This is my code:

import React from 'react';

import {
  Keyboard,
  Dimensions,
  ScrollView,
  StyleSheet,
  Text,
  TextInput,
  AppRegistry
} from 'react-native';

var {height, width} = Dimensions.get('window');
var _keyboardWillShowSubscription;
var _keyboardWillHideSubscription;
height = height - 20;

class awesomeproject extends React.Component {
  constructor(props) {
        super(props);
        this.state = {
            height: height,
        };
    }
    componentDidMount() {
        _keyboardWillShowSubscription = Keyboard.addListener('keyboardDidShow', (e) => this._keyboardWillShow(e));
        _keyboardWillHideSubscription = Keyboard.addListener('keyboardDidHide', (e) => this._keyboardWillHide(e));
    }
    componentWillUnmount() {
        _keyboardWillShowSubscription.remove();
        _keyboardWillHideSubscription.remove();
    }
    _keyboardWillShow(e) {
        this.setState({height: height - e.endCoordinates.height});
    }
    _keyboardWillHide(e) {
        this.setState({height: height});
    }
  render() {
    return (
        <View style={{height: this.state.height}}>  
            <View style={{backgroundColor: 'blue', flex: 1}} />
            <View style={styles.fieldBox}>
                <TextInput 
                    style={styles.field}
                    placeholder={'hello'}
                    autoCapitalize={'none'}
                    placeholderTextColor={'#afbccc'}
                    blurOnSubmit={false}
                    autoCorrect={false}
                    ref={'textinput'}
                    onChangeText={(data) => this.setState({message: data})} />
            </View>
        </View>
    );
  }
}

const styles = StyleSheet.create({
  field: {
        color:'black',
        fontWeight:'bold',
        fontSize:18,
        flex: 1,
        marginHorizontal: 10
    },
});

AppRegistry.registerComponent('awesomeproject', () => awesomeproject);

Locked

Most helpful comment

kelvinaliyanto's advice isn't too far off. If that doesn't work, try replacing

android:windowSoftInputMode="adjustResize"

with

android:windowSoftInputMode="adjustPan"

in your android/app/src/main/Android.Manifest.xml.

All 10 comments

try the keyboardavoidingview

@vonovak I have tried using KeyboardAvoidingView, and it still failed. the problem is that the <View style={{backgroundColor: 'blue', flex: 1}} /> will be replaced with ScrollView component and I want the ScrollView container's height to be the one is adjusted, instead the whole main container to be shifted up. Can it be caused by my android sdk version? My build tools is v 23.0.3. Thanks.

I meet this problem too. It is not related to the android version.

please锛寉ou solved this problem?

@MingYe-dz try adding android:windowSoftInputMode="adjustResize" to your Android Manifest android/app/src/main/AndroidManifest.xml

Did you get any solution to this?

Seeing the same behavior; there's a visible gap between the keyboard and the view. @vonovak KeyboardAvoidingView does not work for me either. There are numerous open issues describing the same bugs I'm having (padding not being removed after keyboard is dismissed, etc.)

It's quite frustrating when I can't build my own when the out-of-the-box solution doesn't work. You guys are really tying my hands here.

Hi there! This issue is being closed because it has been inactive for a while. Maybe the issue has been fixed in a recent release, or perhaps it is not affecting a lot of people. Either way, we're automatically closing issues after a period of inactivity. Please do not take it personally!

If you think this issue should definitely remain open, please let us know. The following information is helpful when it comes to determining if the issue should be re-opened:

  • Does the issue still reproduce on the latest release candidate? Post a comment with the version you tested.
  • If so, is there any information missing from the bug report? Post a comment with all the information required by the issue template.
  • Is there a pull request that addresses this issue? Post a comment with the PR number so we can follow up.

If you would like to work on a patch to fix the issue, contributions are very welcome! Read through the contribution guide, and feel free to hop into #react-native if you need help planning your contribution.

kelvinaliyanto's advice isn't too far off. If that doesn't work, try replacing

android:windowSoftInputMode="adjustResize"

with

android:windowSoftInputMode="adjustPan"

in your android/app/src/main/Android.Manifest.xml.

Was this page helpful?
0 / 5 - 0 ratings