0
0
React Nativemobile~20 mins

KeyboardAvoidingView in React Native - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
KeyboardAvoidingView Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
KeyboardAvoidingView behavior on iOS
What will happen when the keyboard appears in this React Native code using KeyboardAvoidingView with behavior set to 'padding' on iOS?
React Native
import React from 'react';
import { KeyboardAvoidingView, TextInput, Platform } from 'react-native';

export default function App() {
  return (
    <KeyboardAvoidingView behavior={Platform.OS === 'ios' ? 'padding' : 'height'} style={{flex: 1}}>
      <TextInput placeholder="Type here" style={{marginTop: 600, height: 40, borderColor: 'gray', borderWidth: 1}} />
    </KeyboardAvoidingView>
  );
}
AThe keyboard does not appear at all.
BThe TextInput stays fixed and is hidden behind the keyboard.
CThe entire view shrinks vertically but the TextInput stays in place.
DThe TextInput moves up so it is not hidden by the keyboard.
Attempts:
2 left
💡 Hint
Think about how 'padding' behavior adjusts the view when the keyboard shows on iOS.
ui_behavior
intermediate
2:00remaining
KeyboardAvoidingView behavior on Android
In this React Native code, what is the effect of setting KeyboardAvoidingView behavior to 'height' on Android when the keyboard appears?
React Native
import React from 'react';
import { KeyboardAvoidingView, TextInput, Platform } from 'react-native';

export default function App() {
  return (
    <KeyboardAvoidingView behavior={Platform.OS === 'android' ? 'height' : 'padding'} style={{flex: 1}}>
      <TextInput placeholder="Enter text" style={{marginTop: 600, height: 40, borderColor: 'gray', borderWidth: 1}} />
    </KeyboardAvoidingView>
  );
}
AThe keyboard covers the TextInput without any adjustment.
BThe KeyboardAvoidingView adds padding at the bottom, pushing content up.
CThe KeyboardAvoidingView reduces its height by the keyboard height, moving content up.
DThe TextInput automatically scrolls into view without layout changes.
Attempts:
2 left
💡 Hint
Consider how 'height' behavior changes the container size on Android.
lifecycle
advanced
2:00remaining
KeyboardAvoidingView and keyboard dismissal
What happens if you wrap your screen in KeyboardAvoidingView but forget to set keyboardVerticalOffset on iOS when you have a fixed header of 80 pixels?
React Native
import React from 'react';
import { KeyboardAvoidingView, TextInput, Platform } from 'react-native';

export default function App() {
  return (
    <KeyboardAvoidingView behavior={Platform.OS === 'ios' ? 'padding' : 'height'} style={{flex: 1}}>
      <TextInput placeholder="Input" style={{marginTop: 600, height: 40, borderColor: 'gray', borderWidth: 1}} />
    </KeyboardAvoidingView>
  );
}
AThe TextInput will be hidden behind the fixed header when keyboard appears.
BThe TextInput will be correctly positioned above the keyboard and header.
CThe keyboard will not appear at all.
DThe app will crash due to missing keyboardVerticalOffset.
Attempts:
2 left
💡 Hint
Think about how keyboardVerticalOffset adjusts for fixed elements like headers.
📝 Syntax
advanced
2:00remaining
Correct usage of KeyboardAvoidingView props
Which of the following KeyboardAvoidingView usages will cause a syntax error?
A<KeyboardAvoidingView behavior=padding style={{flex: 1}}><TextInput /></KeyboardAvoidingView>
B<KeyboardAvoidingView behavior="padding" style={{flex: 1}}><TextInput /></KeyboardAvoidingView>
C<KeyboardAvoidingView behavior={"height"} style={{flex: 1}}><TextInput /></KeyboardAvoidingView>
D<KeyboardAvoidingView behavior={'position'} style={{flex: 1}}><TextInput /></KeyboardAvoidingView>
Attempts:
2 left
💡 Hint
Check how string props must be passed in JSX.
🔧 Debug
expert
2:00remaining
Unexpected KeyboardAvoidingView behavior on Android
You notice that on Android, KeyboardAvoidingView with behavior='padding' does not move the TextInput up when the keyboard appears. What is the most likely cause?
AYou forgot to wrap the TextInput inside a ScrollView.
BAndroid does not support 'padding' behavior for KeyboardAvoidingView, so it has no effect.
CThe keyboardVerticalOffset is set too high, pushing content off screen.
DThe TextInput is inside a View with fixed height preventing layout changes.
Attempts:
2 left
💡 Hint
Check platform support for KeyboardAvoidingView behaviors.