Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native: Update app layout after the keyboard is shown

I'am working right now on the common problem that the keyboard is pushing the app outside the view.

The android:windowSoftInputMode="adjustResize" setting does not work.

Right now i resize the view according to the keyboard by hand like this:

keyboardWillShow(e) {
    setTimeout(()=> {
        this.keyboardOffset = e.endCoordinates.height;
    }, 500)
}

keyboardWillHide(e) {
    this.keyboardOffset = 0;
}

///...

const resultingHeight = windowHeight - this.keyboardOffset - Navigator.NavigationBar.Styles.General.TotalNavHeight;

viewStyle = {
   height: resultingHeight
};

This almost works. But my problem is that the app is getting pushed outside the view, then the keyboardWillShow is getting fired and resizes the view correct and then nothing happens. Android does not update the layout after the keyboard is shown.

enter image description here

EDIT: The other posts on SO didn't help because the adjustResize setting does not work and i use react-native and not native android.

like image 486
Michael Malura Avatar asked Oct 20 '16 07:10

Michael Malura


People also ask

How do you move a view up when keyboard appears React Native?

How do you control what keyboard pushes up React Native? Use android:windowSoftInputMode="adjustResize". KeyboardAvoidingView and other keyboard-related components don't work quite well if you have "adjustPan" set for your android:windowSoftInputMode in AndroidManifest.

How do you make your React Native app respond gracefully when the keyboard pops up?

The most simple solution, and the easiest to install, is KeyboardAvoidingView. It's a core component but it's also pretty simple in what it does. You can take the base code, which has the keyboard covering the inputs, and update that so that the inputs are no longer covered.


1 Answers

If I recall, keyboardwillShow and keyboardWillHide are not fired on Android.

This behavior should be native to Android, have you tried setting the windowSoftInputMode as follows and not listening for the events?

android:windowSoftInputMode="adjustPan"

If that's not working for you, you could take a look into KeyboardAvoidingView, new with 0.34. it seems to be solving what you are attempting to accomplish.

It is a component to solve the common problem of views that need to move out of the way of the virtual keyboard. It can automatically adjust either its position or bottom padding based on the position of the keyboard.

KeyboardAvoidingView Docs

like image 189
BradByte Avatar answered Sep 28 '22 07:09

BradByte