1. Code
  2. Mobile Development
  3. React Native Templates

Create Your First React Native Android App

Scroll to top

React Native is an open-source mobile application framework created by Facebook. You can use it to develop applications for Android and iOS devices with a single codebase. React Native powers some of the world's most popular apps, such as Instagram and Facebook, and in this post I'll show you how to create your first React Native app for Android.

Example of React Native Code

The React Native code for a typical mobile app screen looks like this:

1
import * as React from 'react';
2
import {Text, View, Stylesheet} from 'react-native';
3
4
const ExampleScreen = (props) {
5
6
  return (
7
8
    <View style = {styles.containerStyling}>
9
    <Text style = {styles.textStyling}> Hello world!</Text>

10
    </View>

11
    );
12
}
13
14
const styles = Stylesheet.create({
15
  containerStyling:{
16
    background: '#0000FF'
17
  },
18
  textStyling :{
19
    marginBottom :20,
20
    color:'#FFF'
21
  }
22
});
23
24
export default ExampleScreen

If you look closely, you'll see that React Native uses a combination of JavaScript, HTML-like markup, and CSS. This code snippet defines a screen with a text display and styling. 

React Native Development Environments: Expo vs. React Native CLI

There are two ways to create a React Native app:

  1. Expo CLI
  2. React Native CLI

I'll talk about the pros and cons of each below.

Expo CLI

Expo is an open-source framework and a platform for universal React applications that gives a managed app development workflow. It is a set of tools and services built around React Native and native platforms that help develop, build, deploy, and quickly iterate on iOS, Android, and web apps from the same JavaScript or TypeScript codebase.

Expo takes away all the complexities associated with building React Native apps. Some of the features of the Expo CLI include:

  • Universal APIs which provide access to features like camera, maps, notifications, sensors, haptics, and much more.
  • A cloud-based build service that gives you app-store-ready binaries and handles certificates.
  • Over-the-air updates which let you update your app at any time without the hassle and delays of submitting to the store.

React Native CLI

The React Native CLI is a more basic and bare-metal development environment. The good thing is that it makes it possible to build app binaries on your own machine, without relying on a cloud service. On the other hand, the setup is much more complicated—to build apps for Android, you'll need to install Android Studio and configure many features before getting started. This process can be a bit complex, but the React Native CLI environment is more suited to professional app developers.

For this tutorial, we'll use Expo since that's the easiest way to get started building React Native apps.

How Expo Works

Expo

To use Expo, you first need to install the Expo Client app, which is available on the Play Store (a version is also available on the iOS App Store). The Expo Client app will allow you to run the app for testing purposes in real time.

You can code your React Native app on your own computer with your favorite programming text editor, and then use the Expo CLI to test or publish your app. Behind the scenes, Expo will package your React Native code and make it available to the Expo Client app on your device. You can also use the Expo CLI to publish your app and make it available to anyone with the Expo Client, or to build a standalone version of your app that can be uploaded to the app store and run without installing the Expo Client.

Creating an App With Expo

In this tutorial, we will use the Expo CLI to create our app. 

Prerequisites

To create a React Native app with Expo, you need to meet the following:

Also, note that your development computer and phone must be connected to the same wireless network.

Download Node.js

Visit the Node.js website and download the latest version of Node. Node is available for Windows, macOS, and Linux. Simply choose your operating system and install it according to the instructions available on the site.

To check if Node.js is installed, open a terminal window and type:

1
node -v

This command will display the installed Node version.

Install Expo Client

After you've installed Node, you will also need to install the Expo CLI client. Simply run the following command:

1
 npm install expo-cli --global

For macOS and Linux users, ensure you use sudo.

1
sudo  npm install expo-cli --global

Ignore any warnings or errors which occur in the process of installing the Expo CLI. After a successful installation, you should see the message below.

install expo

Create a To-Do List App With React Native

We will create a simple to-do list app that lets you input a list of tasks you need to get done and displays them on the screen. 

Create a New Project With Expo

To get started, run the following Expo CLI command to create a new project:

1
expo init tasklist

tasklist is the name of the project. You will be prompted to choose a template for your project. For now, choose the blank template, which gives you minimal dependencies. 

The expo init command creates a project folder and installs all the dependencies required by the React Native app.

1
expo init tasklist-app
2
? Choose a template: expo-template-blank
3
4
📦 Using npm to install packages. You can pass --yarn to use Yarn instead.
5
6
✔ Downloaded and extracted project files.
7
✔ Installed JavaScript dependencies.
8
9
✅ Your project is ready!
10
11
To run your project, navigate to the directory and run one of the following npm commands.
12
13
- cd tasklist-app
14
- npm start # you can open iOS, Android, or web from here, or run them directly with the commands below.
15
- npm run android
16
- npm run ios # requires an iOS device or macOS for access to an iOS simulator
17
- npm run web

Navigate to the project folder and run the following command:

1
cd tasklist-app

2
npm start
Expo dev tools starting

npm start will start the Expo dev tools and open a new tab in your browser that looks like this:

Metro Bundler

This window allows you to run your app on a simulator or a connected device.

Connect a Device

Now, open the Expo client app on your physical Android device and select the Scan QR Code option, as shown below.

expo app

Next, go back to the Expo dev window, scan the QR code, and wait for the JavaScript bundle build process to complete. This usually takes a couple of minutes. When the process is complete, the application should be running on your phone!

Project Structure

Now that your development environment is ready, you can edit the code for the project using your preferred code editor. The project folder looks something like this:

project folder
  • assets: holds the images for the app
  • node_modules: contains all the dependencies for the project
  • App.js: holds the code which renders the UI and is an essential file

App.js is open in the screenshot above. Let's take a closer look. First, we import React from react. We then import the Text and View components from react-native.  We also import Stylesheet, which helps with styling. 

React Native comes with built-in components such as <Text> and <View> as opposed to standard HTML components, like <div> and <p>. The <View> component is the most fundamental component in React Native and is used for grouping other child components—like <div> in HTML. The <Text> component is used to display text content on the screen—like <p> in HTML.

In the boilerplate version of App.js that Expo creates, there is a simple view with a text component and a status bar. This view is returned from the App() function. The styles constant contains some basic CSS to style the view.

Next, let's add some new components and styles to the app!

Create the App UI

Open the App.js file and enter the following code.

1
import { StatusBar } from 'expo-status-bar';
2
import React , {useState} from 'react';
3
import { StyleSheet, Text, View, TextInput, Button } from 'react-native';
4
5
export default function App() {
6
7
  return (
8
    <View style= {styles.container}>
9
      <View style = {styles.inputContainer}>
10
        <TextInput
11
          placeholder = "Task List"
12
          style = {styles.input}
13
        />

14
        <Button title = "+"/> 
15
      </View>

16
    </View>

17
  );
18
}
19
20
const styles = StyleSheet.create({
21
  container: {
22
    flex: 1,
23
    backgroundColor: '#fff',
24
    alignItems: 'center',
25
    justifyContent: 'center',
26
  },
27
  input :{
28
    borderColor:"black", 
29
    borderWidth:1 , 
30
    padding :20,
31
  },
32
  inputContainer :{
33
    flexDirection :'row', 
34
    justifyContent :'space-between', 
35
    alignContent:'center',
36
    bottom:20
37
  },
38
});

The code above adds a simple text input and a button for adding new tasks. We use CSS flexbox styling to position the components next to each other.  

Add Event Handling

To get the user input, we first import the useState() function from react and use it to update the state of the newTask() and setnewTask() functions. Since the user hasn't typed anything yet, the initial state will be empty. Add the following code to the top of the App() function, just above return:

1
const [newTask, setNewTask] = useState('');

We then define the taskInputHandler, which listens to the change in the input and updates the content of the setNewTask() function. Add these lines to the App() function next:

1
  const taskInputHandler = (enteredText) => {
2
    setNewTask(enteredText);
3
  };

Now we register this input handler with the TextInput component. Update your TextInput component so it looks like the following.

1
   <TextInput
2
      placeholder = "Task List"
3
      style = {styles.input}
4
      onChangeText = {taskInputHandler}
5
      value = {newTask}
6
    />

Now we need to handle button presses. When the user presses the + button, we want to add the new task to a list.

First, we'll define our state for the list of tasks:

1
  const [appTasks, appTask] = useState([]);

Next, we define an addTaskHandler function to add the new task (found in the newTask state) to the list.

1
 const addTaskHandler = () =>{
2
    appTask(currentTask => [...currentTask, newTask]);
3
    console.log(newTask);
4
  };

And register that event handler with the <Button> component:

1
  <Button title = "+"
2
      onPress = {addTaskHandler}
3
  />

Finally, we'll add a new view to show all the tasks in the list we've created. This goes just after the input container view, but still inside the main container view.

1
    <View>
2
      {appTasks.map((task) => <Text>{task}</Text>)}

3
    </View>

Complete Source Code for App.js

The full code for App.js is shown below. Compare it to what you have.

1
 import { StatusBar } from 'expo-status-bar';
2
import React , {useState} from 'react';
3
import { StyleSheet, Text, View, TextInput, Button } from 'react-native';
4
5
export default function App() {
6
7
  const [newTask, setnewTask] = useState('');
8
9
  const [appTasks, appTask] = useState([]);
10
11
  const taskInputHandler = (enteredText) => {
12
    setnewTask(enteredText);
13
  };
14
15
  const addTaskHandler = () =>{
16
    appTask(currentTask => [...currentTask, newTask]);
17
    console.log(newTask);
18
  };
19
20
  return (
21
    <View style= {styles.container}>
22
      
23
      <View style = {styles.inputContainer}>
24
      
25
        <TextInput
26
          placeholder = "Task List"
27
          style = {styles.input}
28
          onChangeText = {taskInputHandler}
29
          value = {newTask}
30
        />

31
      
32
        <Button title = "+"
33
          onPress = {addTaskHandler}
34
        /> 

35
      </View>

36
      <View>
37
        {appTasks.map((task) => <Text>{task}</Text>)}

38
      </View>

39
    </View>

40
  );
41
}
42
43
const styles = StyleSheet.create({
44
  container: {
45
    flex: 1,
46
    backgroundColor: '#fff',
47
    alignItems: 'center',
48
    justifyContent: 'center',
49
  },
50
  input :{
51
    borderColor:"black", 
52
    borderWidth:1 , 
53
    padding :20,
54
    
55
  },
56
  inputContainer :{
57
    flexDirection :'row', 
58
    justifyContent :'space-between', 
59
    alignContent:'center,

60
    bottom:20

61
  },

62
});

Conclusion

In this tutorial, you learned how to create a React Native app with Expo. 

React Native is a great framework and a popular platform for both developers and businesses. Apps created with React Native are guaranteed to work smoothly on any platform or system. React Native also saves development work by letting you code your app once and run it on any mobile platform.

Premium Mobile App Templates From CodeCanyon

CodeCanyon is an online marketplace that has hundreds of mobile app templates—for Android, iOS, React Native, and Ionic. You can save days, even months, of effort by using one of them.

Whether you're just getting started with React Native, or are a seasoned developer, a React Native app template is a great way to save time and effort on your next app project. 

CodeCanyon mobile app template bestsellers

If you have trouble deciding which template on CodeCanyon is right for you, these articles should help: 

Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.