Categories
JavaScript Answers React Native Answers

How to fix KeyboardAvoidingView covering the last text input with React Native?

Sometimes, we want to fix KeyboardAvoidingView covering the last text input with React Native.

In this article, we’ll look at how to fix KeyboardAvoidingView covering the last text input with React Native.

How to fix KeyboardAvoidingView covering the last text input with React Native?

To fix KeyboardAvoidingView covering the last text input with React Native, we can set the keyboardVerticalOffset prop.

For instance, we write:

import * as React from 'react';
import { KeyboardAvoidingView, TextInput, Platform } from 'react-native';
import Constants from 'expo-constants';
import { Card } from 'react-native-paper';

export default function App() {
  const keyboardVerticalOffset = Platform.OS === 'ios' ? 40 : 0;

  return (
    <KeyboardAvoidingView
      behavior="position"
      keyboardVerticalOffset={keyboardVerticalOffset}>
      <TextInput placeholder="Example 1" />
      <TextInput placeholder="Example 2" />
      <TextInput placeholder="Example 3" />
      <TextInput placeholder="Example 4" />
      <TextInput placeholder="Example 5" />
      <TextInput placeholder="Example 6" />
      <TextInput placeholder="Example 7" />
    </KeyboardAvoidingView>
  );
}

to check the platform the app is running on with Platform.OS.

Then we set the keyboardVerticalOffset accordingly.

As a result, the last text input should always be showing.

Conclusion

To fix KeyboardAvoidingView covering the last text input with React Native, we can set the keyboardVerticalOffset prop.

Categories
JavaScript Answers React Native Answers

How to convert an image to grayscale in React Native?

Sometimes, we want to convert an image to grayscale in React Native.

In this article, we’ll look at how to convert an image to grayscale in React Native.

How to convert an image to grayscale in React Native?

To convert an image to grayscale in React Native, we can add the same image twice, with one set to a different opacity than the other one, and add the tintColor prop.

For instance, we write:

import * as React from 'react';
import { Image, View } from 'react-native';
import Constants from 'expo-constants';
import { Card } from 'react-native-paper';
import { Icon } from 'react-native-elements';

export default function App() {
  return (
    <View>
      <Image
        source={{ uri: 'https://picsum.photos/200/300' }}
        style={{ tintColor: 'gray', height: 300, width: 200 }}
      />
      <Image
        source={{ uri: 'https://picsum.photos/200/300' }}
        style={{ position: 'absolute', opacity: 0.3, height: 300, width: 200 }}
      />
    </View>
  );
}

to set the tintColor to 'gray' to add the grayscale tint.

Then we add the same image with the position set to 'absolute' to put it over the gray image.

And we set the opacity to 0.3 to we can see the image.

Conclusion

To convert an image to grayscale in React Native, we can add the same image twice, with one set to a different opacity than the other one, and add the tintColor prop.

Categories
JavaScript Answers React Native Answers

How to check when the focus is lost in a React Native TextInput?

Sometimes, we want to check when the focus is lost in a React Native TextInput.

In this article, we’ll look at how to check when the focus is lost in a React Native TextInput.

How to check when the focus is lost in a React Native TextInput?

To check when the focus is lost in a React Native TextInput, we can set the onBlur prop to a function that runs when focus is lost.

For instance, we write:

import * as React from 'react';
import { TextInput, View } from 'react-native';
import Constants from 'expo-constants';
import { Card } from 'react-native-paper';
import { Icon } from 'react-native-elements';

export default function App() {
  return (
    <View>
      <TextInput onBlur={() => console.log('focus lost')} placeholder="Name" />
    </View>
  );
}

to set the onBlur prop to a function that logs 'focus lost' when we move focus away from the text input.

Conclusion

To check when the focus is lost in a React Native TextInput, we can set the onBlur prop to a function that runs when focus is lost.

Categories
JavaScript Answers React Native

How to add a floating action button on React Native

Sometimes, we want to add a floating action button on React Native.

In this article, we’ll look at how to add a floating action button on React Native.

How to add a floating action button on React Native

To add a floating action button on React Native, we can use the TouchableOpacity and Icon components.

For instance, we write:

import * as React from 'react';
import { TouchableOpacity, View } from 'react-native';
import Constants from 'expo-constants';
import { Card } from 'react-native-paper';
import { Icon } from 'react-native-elements';

export default function App() {
  return (
    <View style={{ justifyContent: 'center', alignItems: 'center', flex: 1 }}>
      <TouchableOpacity
        style={{
          borderWidth: 1,
          borderColor: 'rgba(0,0,0,0.2)',
          alignItems: 'center',
          justifyContent: 'center',
          width: 70,
          position: 'absolute',
          bottom: 10,
          right: 10,
          height: 70,
          backgroundColor: '#fff',
          borderRadius: 100,
        }}>
        <Icon name="plus" size={30} color="#01a699" />
      </TouchableOpacity>
    </View>
  );
}

to position that TouchableOpacity component with position, bottom, and right properties.

We set borderRadius to 100 to make it round.

And we set justifyContent and alignItems to 'center' to center the contents.

Next, we add an Icon in it to show an icon inside the button.

Conclusion

To add a floating action button on React Native, we can use the TouchableOpacity and Icon components.

Categories
JavaScript Answers React Native Answers

How to change the height of a FlatList in React Native?

Sometimes, we want to change the height of a FlatList in React Native.

In this article, we’ll look at how to change the height of a FlatList in React Native.

How to change the height of a FlatList in React Native?

To change the height of a FlatList in React Native, we can set the height of the View that we wrap around the FlatList.

For instance, we write:

import * as React from 'react';
import { FlatList, Text, View } from 'react-native';
import Constants from 'expo-constants';
import { Card } from 'react-native-paper';

const flatListItems = Array(200)
  .fill()
  .map((_, i) => ({ title: i, id: i }));

const Item = ({ title }) => (
  <View
    style={{
      backgroundColor: '#f9c2ff',
      padding: 20,
      marginVertical: 8,
      marginHorizontal: 16,
    }}>
    <Text>{title}</Text>
  </View>
);

export default function App() {
  const renderItem = ({ item }) => <Item title={item.title} />;

  return (
    <View style={{ height: 300 }}>
      <FlatList
        data={flatListItems}
        renderItem={renderItem}
        keyExtractor={(item) => item.id}
      />
    </View>
  );
}

to set the height of the View to 300px to make the height of the FlatList 300px.

Conclusion

To change the height of a FlatList in React Native, we can set the height of the View that we wrap around the FlatList.