How to set the textinput box above the Keyboard while entering the input field in react native

49,158

Solution 1

Give your TextInput a position: absolute styling and change its position using the height returned by the keyboardDidShow and keyboardDidHide events.

Here is a modification of the Keyboard example from the React Native documentation for demonstration:

import React, { Component } from 'react';
import { Keyboard, TextInput } from 'react-native';

class Example extends Component {
    state = {
        keyboardOffset: 0,
    };

    componentDidMount() {
        this.keyboardDidShowListener = Keyboard.addListener(
            'keyboardDidShow',
            this._keyboardDidShow,
        );
        this.keyboardDidHideListener = Keyboard.addListener(
            'keyboardDidHide',
            this._keyboardDidHide,
        );
    }

    componentWillUnmount() {
        this.keyboardDidShowListener.remove();
        this.keyboardDidHideListener.remove();
    }

    _keyboardDidShow(event) {
        this.setState({
            keyboardOffset: event.endCoordinates.height,
        })
    }

    _keyboardDidHide() {
        this.setState({
            keyboardOffset: 0,
        })
    }

    render() {
        return <View style={{flex: 1}}>
            <TextInput
                style={{
                    position: 'absolute',
                    width:    '100%',
                    bottom:   this.state.keyboardOffset,
                }}
                onSubmitEditing={Keyboard.dismiss}
            />
        </View>;
    }
}

Solution 2

Hooks version:

const [keyboardOffset, setKeyboardOffset] = useState(0);
const onKeyboardShow = event => setKeyboardOffset(event.endCoordinates.height);
const onKeyboardHide = () => setKeyboardOffset(0);
const keyboardDidShowListener = useRef();
const keyboardDidHideListener = useRef();

useEffect(() => {
  keyboardDidShowListener.current = Keyboard.addListener('keyboardWillShow', onKeyboardShow);
  keyboardDidHideListener.current = Keyboard.addListener('keyboardWillHide', onKeyboardHide);

  return () => {
    keyboardDidShowListener.current.remove();
    keyboardDidHideListener.current.remove();
  };
}, []);

Solution 3

you can use KeyboardAvoidingView as follows

if (Platform.OS === 'ios') {
        return <KeyboardAvoidingView behavior="padding">
            {this.renderChatInputSection()}
        </KeyboardAvoidingView>
    } else {
        return this.renderChatInputSection()
    }

Where this.renderChatInputSection() will return the view like textinput for typing message. Hope this will help you.

Solution 4

First of all, You don't need any extra code for Android platform. Only keep your inputs inside a ScrollView. Just use KeyboardAvoidingView to encapsulate the ScrollView for iOS platform.

Create function such as below which holds all the inputs

renderInputs = () => {
    return (<ScrollView
        showsVerticalScrollIndicator={false}
        style={{
            flex: 1,
        }}
        contentContainerStyle={{
            flexGrow: 1,
        }}>
        <Text>Enter Email</Text>
        <TextInput
            style={styles.text}
            underlineColorAndroid="transparent"
        />
    </ScrollView>)
}

Then render them inside the main view as below

{Platform.OS === 'android' ? (
    this.renderInputs()
) : (
    <KeyboardAvoidingView behavior="padding">
        {this.renderInputs()}
    </KeyboardAvoidingView>
)}

I have used this method and I can assure that it works. If it is not working then there is a chance that you are missing something.

Solution 5

For android you can set android:windowSoftInputMode="adjustResize" for Activity in AndroidManifest file, thus when the keyboard shows, your screen will resize and if you put the TextInput at the bottom of your screen, it will be keep above keyboard

Share:
49,158
sejn
Author by

sejn

Updated on March 30, 2021

Comments

  • sejn
    sejn about 3 years

    I am using react-native TextInput component. Here I need to show the InputBox above the keyboard if the user clicks on the textInput field.

    I have tried below but i am facing the issues

    1. Keyboard avoiding view

     a. Here it shows some empty space below the input box 
     b. Manually I need to scroll up the screen to see the input field which I was given in the text field
     c. Input box section is hiding while placing the mouse inside the input box 
    

    2. react-native-Keyboard-aware-scroll-view

    a.It shows some empty space below the input box
    b.ScrollView is reset to the top of the page after I moving to the next input box
    

    Here I set the Keyboard-aware-scroll-view inside the ScrollView component

    Kindly clarify

    My example code is

    <SafeAreaView>
    <KeyboardAvoidingView>
    <ScrollView>        
            <Text>Name</Text>
                <AutoTags
                //required
                 suggestions={this.state.suggestedName}
                 handleAddition={this.handleAddition}
                 handleDelete={this.handleDelete}
                 multiline={true}
                 placeholder="TYPE IN"
                 blurOnSubmit={true}
                 style= {styles.style}
                 />
    </ScrollView>   
    </KeyboardAvoidingView>
    </SafeAreaView>
    

    [https://github.com/APSL/react-native-keyboard-aware-scroll-view]