How to create a Floating Action Button in react native ?
Last Updated :
29 Jul, 2024
React Native is a framework developed by Facebook for creating native-style apps for iOS & Android under one common language, JavaScript. Initially, Facebook only developed React Native to support iOS. However, with its recent support of the Android operating system, the library can now render mobile UIs for both platforms.
Prerequisites:
Approach: In this article, we will create a floating action button in react-native. To create it, we will use react-native-paper library.
In our project, we will Create a FAB and on click of it, the text field will appear. We will see the implementation step-by-step.
Creating React Native App:
Step 1: Create a react-native project:
npx react-native init DemoProject
Step 2: Now install react-native-paper
npm install react-native-paper
Step 3: Start the server
npx react-native run-android
Step 4: Now go to your project and create a components folder. Inside the components folder, create a file Fab.js
Project Structure: The project should look like this:

Example: In Fab.js, initially, the input field and button will be hidden. On change of their states, both will appear below the Fab button.
Fab.js
import React, { useState } from 'react';
import { View, StyleSheet, Alert, Button } from 'react-native';
import { FAB, TextInput } from 'react-native-paper';
const FabExample = () => {
const [text, setText] = useState('');
const [showText, setShowText] = useState(false);
const [disablebtn, setDisablebtn] = useState(true);
const addItem = (text) => {
setShowText(true);
};
const showAlert = () => {
Alert.alert('Item added successfully');
};
return (
<View>
<FAB style={styles.fab} icon="plus" small label="Add more"
onPress={addItem} />
{showText ? (
<View style={styles.textInput}>
<TextInput
mode="outlined"
label="Item"
value={text}
onChangeText={(newText) => {
setText(newText);
setDisablebtn(false);
}}
/>
<View style={styles.btn}>
<Button title="Submit" disabled={disablebtn}
onPress={showAlert} />
</View>
</View>
) : (
<></>
)}
</View>
);
};
export default FabExample;
const styles = StyleSheet.create({
fab: {
position: 'relative',
margin: 16,
marginTop: 40,
right: 0,
bottom: 0,
},
textInput: {
position: 'relative',
margin: 18,
},
btn: {
marginTop: 20,
},
});
App.js
import React from 'react';
import { View } from 'react-native';
import FabExample from './components/Fab';
const App: () => Node = () => {
return (
<View>
<FabExample />
</View>
);
};
export default App;
Step to run the application: Run the application using the following command:
npx react-native run-android
Output:
Similar Reads
How to create custom FAB(Floating Action Button) in React Native? React Native doesn't come with any FAB component built in. A floating action button (FAB) is used whenever you want to display a button on top of everything. If you have used ScrollView and the user can scroll up and down, this FAB button will always stay at the same position and doesn't move with t
7 min read
How to create a basic button in React Native ? In this article, we will learn how to create a basic button in React Native. To build a React Native app, we will use the Expo CLI, which provides a smoother experience for running your applications.ExpoIt is a framework and a platform for universal React applications. It is a set of tools and servi
5 min read
How to add Floating Buttons in React Native ? React Native is an open-source UI software framework created by Meta Platforms, Inc. It is used to develop applications for Android, Android TV, iOS, macOS, tvOS, Web, Windows, and UWP by enabling developers to use the React framework along with native platform capabilities.In this article, we will
2 min read
How to Create Button in React-Native App ? React Native provides pre-defined components like button and TouchableOpacity to create buttons in a react native app. In this article, we will see how to create buttons in react-native, their syntax, and different types of buttons available in react-native.Table of ContentApproachButton in React Na
4 min read
How to create a Banner in react-native ? React Native is a framework developed by Facebook for creating native-style apps for iOS & Android under one common language, JavaScript. Initially, Facebook only developed React Native to support iOS. However, with its recent support of the Android operating system, the library can now render m
3 min read
How to Create a custom CheckBox Component in React Native? React Native is all about components. It provides many built-in components for us to use. By using this component, we can create a beautiful UI for mobile applications. But it lacks some of the components. And CheckBox is one of them. React Native does not provide any built-in component that we can
4 min read