Keyboard avoiding: don't type blind
The keyboard takes almost half the screen, right where chat inputs and login buttons live. Watch it cover one, then see what each of KeyboardAvoidingView's behaviours does about it, on iOS and on Android.
What you'll take away
- Pick KeyboardAvoidingView's padding, height or position, and know what each one moves
- Set
keyboardShouldPersistTaps="handled"so Send works on the first tap - Explain why Android moves the view after the keyboard is up, and iOS moves with it
- Use
keyboardVerticalOffsetunder a header, and let a drag of the list close the keyboard
The 1-minute version
Tap a point and the picture below plays that step.
Tap the input on the screen, try every behavior on both systems, then press Send while the keyboard is up and see what never and handled change
On iOS the input sits at the bottom of a chat screen and the keyboard covers it. What's the simplest fix?
The picture: one big phone standing in the night city, a chat on its screen. Behind it stand the iOS and Android halls; a keyboard event travels from the phone's OS to the app as a light: iOS sends keyboardWillShow before the keyboard moves, Android sends keyboardDidShow only once it is up. The dashed box is the KeyboardAvoidingView, its padding shaded; the keyboard is drawn on top, as the system draws it. The motion is slowed down so you can follow it. Keyboard heights (about 336 pt on this iPhone, about 300 dp on Android) are a model's numbers; real ones vary with the keyboard and the language.
Compare them all: Keyboard avoiding
The same chat screen, the input tapped in every tile at once, the same keyboard rising. See what each behaviour keeps in view: the input, the header, the messages.
Use it when Screens with no input in the bottom half
Skip it when Chats, login forms, a search box at the bottom
A model: a keyboard of about 336 pt, an English one with its suggestions bar; real ones vary with the keyboard and the language. Press any tile to see it large above, with when it fits.
Three behaviours
| behavior | Does | Watch out |
|---|---|---|
| padding | adds the covered part as paddingBottom | the usual pick on iOS |
| height | takes the covered part off its own height | looks like padding in a flex: 1 box; often used on Android |
| position | slides the whole content up | the top goes off screen |
Taps while the keyboard is up
A ScrollView or FlatList starts with keyboardShouldPersistTaps="never": a tap outside the input closes the keyboard and nothing else. Use "handled" so the Send button gets its first tap.
A header above
The view measures the overlap from where it thinks it starts. With a navigation header above it, pass that header's height as keyboardVerticalOffset.
Android
Android tells JS only after the keyboard is up (keyboardDidShow), so the view moves afterwards. Its window can also resize for the keyboard (adjustResize in the manifest, as the template sets it): check on a real phone that the two together leave no extra gap.
In real code
// A chat screen whose input stays above the keyboard on both systems.
import { useState } from "react";
import { FlatList, KeyboardAvoidingView, Platform, Pressable, StyleSheet, Text, TextInput, View } from "react-native";
type Message = { id: string; mine: boolean; text: string };
export function Chat({ headerHeight, initial }: { headerHeight: number; initial: Message[] }) {
const [messages, setMessages] = useState(initial);
const [draft, setDraft] = useState("");
const send = () => {
if (!draft.trim()) return;
setMessages((m) => [...m, { id: String(Date.now()), mine: true, text: draft.trim() }]);
setDraft("");
};
return (
<KeyboardAvoidingView
style={styles.screen}
// iOS hears keyboardWillShow and moves with the keyboard; Android hears keyboardDidShow, once it's up.
behavior={Platform.OS === "ios" ? "padding" : "height"}
// A navigation header sits above this view: tell it how far down the screen it starts.
keyboardVerticalOffset={headerHeight}
>
<FlatList
data={messages}
keyExtractor={(m) => m.id}
renderItem={({ item }) => <Text style={[styles.bubble, item.mine ? styles.mine : styles.theirs]}>{item.text}</Text>}
// A tap on a button while the keyboard is up reaches the button ("never", the default, only closes it).
keyboardShouldPersistTaps="handled"
// Drag the list to close the keyboard; "interactive" (the keyboard follows the finger) exists on iOS only.
keyboardDismissMode={Platform.OS === "ios" ? "interactive" : "on-drag"}
/>
<View style={styles.bar}>
<TextInput style={styles.input} value={draft} onChangeText={setDraft} placeholder="Message" returnKeyType="send" onSubmitEditing={send} />
<Pressable accessibilityRole="button" onPress={send} style={styles.send}><Text style={styles.sendText}>Send</Text></Pressable>
</View>
</KeyboardAvoidingView>
);
}
const styles = StyleSheet.create({
screen: { flex: 1 },
bubble: { margin: 6, padding: 10, borderRadius: 14, maxWidth: "75%" },
mine: { alignSelf: "flex-end", backgroundColor: "#1d5fae", color: "white" },
theirs: { alignSelf: "flex-start", backgroundColor: "#e8eaee" },
bar: { flexDirection: "row", gap: 8, padding: 8 },
input: { flex: 1, borderWidth: 1, borderColor: "#c9ced6", borderRadius: 18, paddingHorizontal: 12 },
send: { justifyContent: "center", paddingHorizontal: 14, borderRadius: 18, backgroundColor: "#1d5fae" },
sendText: { color: "white", fontWeight: "600" },
});
tsc against each library's own published types (react-native and the libraries this file imports); type-checked only, never run on a phoneIt printed: tsc 6.0.3 (strict): no errors against react-native 0.88.1 (type-checked only)KeyboardAvoidingView with padding on iOS and height on Android, keyboardVerticalOffset for a header above it, a FlatList whose taps reach the buttons and whose drag closes the keyboard. Type-checked with tsc against the types react-native 0.88 publishes; no phone ran it.
Common questions
Which behavior should I use?
The usual pair is padding on iOS and height on Android: both give up the space the keyboard covers at the bottom. position lifts the whole content instead, which suits a short form.
Why does position make the header disappear?
position shrinks nothing: it moves the whole content up by what the keyboard covers, so the top slides off screen. For a screen whose header must stay, padding or height fits better.
Why doesn't the first tap on a button work while the keyboard is up?
ScrollView and FlatList default to keyboardShouldPersistTaps="never": a tap outside the input while the keyboard is up only closes it, and the children never see the tap. Set "handled" and a button that takes the tap works at once.
Why is the input covered for a moment on Android before it moves?
Android only sends keyboardDidShow, after the keyboard is up, so KeyboardAvoidingView moves afterwards. iOS sends keyboardWillShow with the keyboard's duration, so it moves along with it.
What is keyboardVerticalOffset?
The distance from the top of the screen to the KeyboardAvoidingView, for when something sits above it, like a navigation header. Leave it out and it gets the overlap wrong, leaving the input partly covered.
Can dragging the list close the keyboard?
Yes: keyboardDismissMode="on-drag" on the ScrollView or FlatList closes it on the first drag, or "interactive" lets the keyboard follow your finger down, which is iOS only.
What does adjustResize in AndroidManifest.xml do?
It tells Android to give the keyboard's space back to the app by resizing the window, instead of panning the whole screen. React Native's template sets it; check on a real phone that, with KeyboardAvoidingView, you don't get an extra gap.
Next lesson
Gestures on a phoneLevel: Intermediate
Taps and drags met the keyboard here; next, how a phone tells a tap from a double tap, a swipe and a drag, and why a drag can freeze.