Keyboard handling in React Native is essential when building forms or input-heavy screens. Without proper handling, the on-screen keyboard may cover input fields, buttons, or important UI elements. React Native provides built-in components to manage keyboard behavior smoothly across Android and iOS.
The most common approach is wrapping your screen or form inside a KeyboardAvoidingView. This component adjusts its layout based on the keyboard visibility. On iOS, padding behavior is commonly used, while Android often works well with height adjustment.
// Basic keyboard handling using KeyboardAvoidingView
import React from "react";
import { View, TextInput, Button, KeyboardAvoidingView, Platform } from "react-native";
export default function App() {
return (
<KeyboardAvoidingView
behavior={Platform.OS === "ios" ? "padding" : "height"}
style={{ flex: 1, justifyContent: "center", padding: 20 }}
>
<TextInput
placeholder="Enter username"
style={{ borderWidth: 1, marginBottom: 12, padding: 10 }}
/>
<TextInput
placeholder="Enter password"
secureTextEntry
style={{ borderWidth: 1, marginBottom: 12, padding: 10 }}
/>
<Button title="Login" onPress={() => {}} />
</KeyboardAvoidingView>
);
}
Click the input field in the phone to see how the keyboard affects the layout.
⬇️ Normal Screen
⬆️ Keyboard Appears
? KeyboardAvoidingView adjusts layout automatically
KeyboardAvoidingView with ScrollView for long formskeyboardShouldPersistTaps="handled" in ScrollViewbehavior values