KeyboardAvoidingView is a built-in React Native component used to automatically adjust the layout when the on-screen keyboard appears. It prevents important UI elements like TextInput and buttons from being hidden by the keyboard, especially on mobile devices.
KeyboardAvoidingView adjusts its children based on keyboard visibility. The behavior prop defines how the view responds, such as shifting position, adding padding, or resizing height.
// Import KeyboardAvoidingView from react-native
import { KeyboardAvoidingView, Platform, TextInput, View } from "react-native";
export default function App() {
return (
<KeyboardAvoidingView
behavior={Platform.OS === "ios" ? "padding" : "height"}
style={{ flex: 1 }}
>
<View style={{ padding: 20 }}>
<TextInput placeholder="Enter name" />
</View>
</KeyboardAvoidingView>
);
}
When the keyboard opens, the entire view shifts upward. On iOS, padding is added below the content, while on Android the height of the view is adjusted automatically.
Use the simulator below to understand why KeyboardAvoidingView is necessary. Click the input field inside the phone to trigger the keyboard.
Status: KeyboardAvoidingView Active. The layout resizes so the input stays visible.
// Pseudo-layout representation for understanding behavior
[Screen]
[Input Field]
[Button]
------ Keyboard Appears ------
[Input Field]
[Button]
[Keyboard]