Course contentsShow
← React Native
Lesson 170 of 1,5553. Core Primitive ComponentsFree lesson

Handling Soft Keyboard Overlap

Learners will adjust UI positioning dynamically when the native keyboard appears on screen.

Handling Soft Keyboard Overlap

In this lesson, you will learn how to prevent the native software keyboard from obscuring your input fields and critical UI elements when a user begins typing.

The Overlap Problem

When a user taps an TextInput component, the system-level software keyboard slides up from the bottom of the screen. By default, React Native views might stay exactly where they are, meaning your text input—or the submit button beneath it—could get hidden behind the keyboard. This creates a frustrating experience where users type "blindly."

Using KeyboardAvoidingView

To solve this, React Native provides a specialized component called KeyboardAvoidingView. This component acts as a wrapper that automatically adjusts its height, position, or bottom padding when the keyboard appears.

Instead of manually calculating screen coordinates, you wrap your input-heavy containers in this component:

import { KeyboardAvoidingView, Platform, TextInput } from 'react-native';

const MyForm = () => {
  return (
    <KeyboardAvoidingView 
      behavior={Platform.OS === 'ios' ? 'padding' : 'height'}
      style={{ flex: 1 }}
    >
      <TextInput placeholder="Type something here..." />
    </KeyboardAvoidingView>
  );
};

Understanding Behavior

The behavior prop tells the component how to react to the keyboard:

  • padding: Adds padding to the bottom of the view so the content is pushed up.
  • height: Adjusts the height of the component itself.
  • position: Adjusts the bottom position of the view.

We typically use Platform.OS checks because iOS and Android handle keyboard events differently at the system level. By setting this wrapper, you ensure that as the keyboard rises, your layout dynamically recalculates its boundaries to keep the active input visible within the remaining safe screen space. This maintains a clean user flow without needing to write complex animation logic yourself.

Key Takeaway: Wrap your input-heavy layouts in KeyboardAvoidingView to automatically shift your content out of the way whenever the native keyboard appears.