managarten/apps-archived/maerchenzauber/apps/mobile/README/ExpoKeyboardHandlingReadMe.md
Till-JS 61d181fbc2 chore: archive inactive projects to apps-archived/
Move inactive projects out of active workspace:
- bauntown (community website)
- maerchenzauber (AI story generation)
- memoro (voice memo app)
- news (news aggregation)
- nutriphi (nutrition tracking)
- reader (reading app)
- uload (URL shortener)
- wisekeep (AI wisdom extraction)

Update CLAUDE.md documentation:
- Add presi to active projects
- Document archived projects section
- Update workspace configuration

Archived apps can be re-activated by moving back to apps/

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-29 07:03:59 +01:00

13 KiB

Keyboard handling

A guide for handling common keyboard interactions on an Android or iOS device.

Keyboard handling is crucial for creating an excellent user experience in your Expo app. React Native provides Keyboard and KeyboardAvoidingView, which are commonly used to handle keyboard events. For more complex or custom keyboard interactions, you can consider using react-native-keyboard-controller, which is a library that offers advanced keyboard handling capabilities.

This guide covers common keyboard interactions and how to manage them effectively.

Keyboard Handling tutorial for React Native apps Keyboard Handling tutorial for React Native apps In this keyboard handling tutorial for React Native apps, you'll learn how to solve the problem of the keyboard covering your input when you try to type on your app.

Keyboard handling basics The following sections explain how to handle keyboard interactions with common APIs.

Keyboard avoiding view The KeyboardAvoidingView is a component that automatically adjusts a keyboard's height, position, or bottom padding based on the keyboard height to remain visible while it is displayed.

Android and iOS interact with the behavior property differently. On iOS, padding is usually what works best, and for Android, just having the KeyboardAvoidingView prevents covering the input. This is why the following example uses undefined for Android. Playing around with the behavior is a good practice since a different option could work best for your app.

HomeScreen.tsx

Copy

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

export default function HomeScreen() { return ( <KeyboardAvoidingView behavior={Platform.OS === 'ios' ? 'padding' : undefined} style={{ flex: 1 }}> ; ); } In the above example, the height of the KeyboardAvoidingView automatically adjusts based on the device's keyboard height, which ensures that the input is always visible.

When using a Bottom Tab navigator on Android, you might notice that focusing on an input field causes the bottom tabs to be pushed above the keyboard. To address this issue, add the softwareKeyboardLayoutMode property to your Android configuration in app confg and set it to pan.

app.json

Copy

"expo" { "android": { "softwareKeyboardLayoutMode": "pan" } } After adding this property, restart the development server and reload your app to apply the changes.

It's also possible to hide the bottom tab when the keyboard opens using tabBarHideOnKeyboard. It is an option with the Bottom Tab Navigator. If set to true, it will hide the bar when the keyboard opens.

app/_layout.tsx

Copy

import { Tabs } from 'expo-router';

export default function TabLayout() { return ( <Tabs screenOptions={{ tabBarHideOnKeyboard: true, }}> <Tabs.Screen name="index" /> ); } Keyboard events The Keyboard module from React Native allows you to listen for native events, react to them, and make changes to the keyboard, such as dismissing it.

To listen for keyboard events, use the Keyboard.addListener method. This method accepts an event name and a callback function as arguments. When the keyboard is shown or hidden, the callback function is called with the event data.

The following example illustrates a use case for adding a keyboard listener. The state variable isKeyboardVisible is toggled each time the keyboard shows or hides. Based on this variable, a button allows the user to dismiss the keyboard only if the keyboard is active. Also, notice that the button uses the Keyboard.dismiss method.

HomeScreen.tsx

Copy

import { useEffect, useState } from 'react'; import { Keyboard, View, Button, TextInput } from 'react-native';

export default function HomeScreen() { const [isKeyboardVisible, setIsKeyboardVisible] = useState(false);

useEffect(() => { const showSubscription = Keyboard.addListener('keyboardDidShow', handleKeyboardShow); const hideSubscription = Keyboard.addListener('keyboardDidHide', handleKeyboardHide);

return () => {
  showSubscription.remove();
};

}, []);

const handleKeyboardShow = event => { setIsKeyboardVisible(true); };

const handleKeyboardHide = event => { setIsKeyboardVisible(false); };

return ( {isKeyboardVisible &&