React Native - React Native Vector Icons
React Native Vector Icons
React Native Vector Icons are the most popular custom icons of NPM GitHub library. It has more than 3K (3000) icons collection in it. All these icons are free to use. The React Native Vector icons come with complete customization such as icon size, icon color, and it also supports multiple styling.
Following are the list of icons category available in React Native Vector Icons. You can also visit at https://oblador.github.io/react-native-vector-icons/ for these icons.
- AntDesign by AntFinance (297 icons)
- Entypo by Daniel Bruce (411 icons)
- EvilIcons by Alexander Madyankin & Roman Shamin (v1.10.1, 70 icons)
- Feather by Cole Bemis & Contributors (v4.21.0, 279 icons)
- FontAwesome by Dave Gandy (v4.7.0, 675 icons)
- FontAwesome 5 by Fonticons, Inc. (v5.7.0, 1500 (free) 5082 (pro) icons)
- Fontisto by Kenan Gündo?an (v3.0.4, 615 icons)
- Foundation by ZURB, Inc. (v3.0, 283 icons)
- Ionicons by Ben Sperry (v4.2.4, 696 icons)
- MaterialIcons by Google, Inc. (v3.0.1, 932 icons)
- MaterialCommunityIcons by MaterialDesignIcons.com (v3.6.95, 3695 icons)
- Octicons by Github, Inc. (v8.4.1, 184 icons)
- Zocial by Sam Collins (v1.0, 100 icons)
- SimpleLineIcons by Sabbir & Contributors (v2.4.1, 189 icons)
Installation of React Native Vector Icons
1. Open your react native project folder in command prompt and execute the below code:
npm install react-native-vector-icons --save
After successful execution of the above code, it adds the react-native-vector-icons library.
2. Open your_react_native_project->android -> app -> build.gradle file and put below code of line inside it.
apply from: "../../node_modules/react-native-vector-icons/fonts.gradle"
3. Again open your_react_native_project -> android -> app -> build.gradle file and put the below code inside the dependency block.
implementation project(':react-native-vector-icons')
your_react_native_project->android -> app -> build.gradle
After adding the above code, the build.gradle file looks like as:
apply plugin: "com.android.application"
apply from: "../../node_modules/react-native-vector-icons/fonts.gradle"
import com.android.build.OutputFile
project.ext.react = [
entryFile: "index.js"
]
apply from: "../../node_modules/react-native/react.gradle"
def enableSeparateBuildPerCPUArchitecture = false
/**
* Run Proguard to shrink the Java bytecode in release builds.
*/
def enableProguardInReleaseBuilds = false
android {
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
defaultConfig {
applicationId "com.vectoricons"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 1
versionName "1.0"
ndk {
abiFilters "armeabi-v7a", "x86"
}
}
splits {
abi {
reset()
enable enableSeparateBuildPerCPUArchitecture
universalApk false // If true, also generate a universal APK
include "armeabi-v7a", "x86"
}
}
buildTypes {
release {
minifyEnabled enableProguardInReleaseBuilds
proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
}
}
// applicationVariants are e.g. debug, release
applicationVariants.all { variant ->
variant.outputs.each { output ->
// For each separate APK per architecture, set a unique version code as described here:
// http://tools.android.com/tech-docs/new-build-system/user-guide/apk-splits
def versionCodes = ["armeabi-v7a":1, "x86":2]
def abi = output.getFilter(OutputFile.ABI)
if (abi != null) { // null for the universal-debug, universal-release variants
output.versionCodeOverride =
versionCodes.get(abi) * 1048576 + defaultConfig.versionCode
}
}
}
}
dependencies {
compile project(':react-native-vector-icons')
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}"
implementation "com.facebook.react:react-native:+" // From node_modules
implementation project(':react-native-vector-icons')
}
// Run this once to be able to run the application with BUCK
// puts all compile dependencies into folder libs for BUCK to use
task copyDownloadableDepsToLibs(type: Copy) {
from configurations.compile
into 'libs'
}
4. Open your_react_native_project-> android-> settings.gradle file and add the below code:
include ':react-native-vector-icons'
project(':react-native-vector-icons').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-vector-icons/android')
your_react_native_project-> android-> settings.gradle
rootProject.name = 'VectorIcons'
include ':react-native-vector-icons'
project(':react-native-vector-icons').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-vector-icons/android')
include ':app'
5. Open your_react_native_project -> android -> app -> src -> main -> java-> com-> your_project_name -> MainApplication.java file and import vector icons package using below line of code.
import com.oblador.vectoricons.VectorIconsPackage;
6. In the MainApplication.java file, call the react native vector icons below package name inside the return Arrays.asList() block.
new VectorIconsPackage()
MainApplication.java
package com.vectoricons;
import android.app.Application;
import com.facebook.react.ReactApplication;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.react.shell.MainReactPackage;
import com.facebook.soloader.SoLoader;
import com.oblador.vectoricons.VectorIconsPackage;
import java.util.Arrays;
import java.util.List;
public class MainApplication extends Application implements ReactApplication {
private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
@Override
public boolean getUseDeveloperSupport() {
return BuildConfig.DEBUG;
}
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new VectorIconsPackage()
);
}
@Override
protected String getJSMainModuleName() {
return "index";
}
};
@Override
public ReactNativeHost getReactNativeHost() {
return mReactNativeHost;
}
@Override
public void onCreate() {
super.onCreate();
SoLoader.init(this, /* native exopackage */ false);
}
}
Linking of Dependency
After installing the above code, we need to link it with our project.
react-native link
In the App.js file, create two constant named as facebook_button and twitter_button inside the render block. We call these constant directly into the TouchableOpacity component. The props of Icon.Button are given below:
Props | Description |
---|---|
name=" " | In this prop, we pass the name of the icon. |
backgroundColor=" " | It is used to set the color of the button. |
size={} | It sets the size of the button. |
onPress={} | It reprsents the onPress event on button. |
color | It sets the color of Text and Icon. |
Create two another constant of Icon named as android_icon and music_icon inside render block.
Props | Description |
---|---|
name=" " | In this prop, we pass the name of the icon. |
size={} | It sets the size of Icon. |
color=" " | It sets the color of the Icon. |
onPress={} | It is the onPress event on button. |
App.js
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
* @flow
*/
import React, {Component} from 'react';
import {Platform, StyleSheet, Text, View, TouchableOpacity, Alert} from 'react-native';
import Icon from 'react-native-vector-icons/FontAwesome';
const instructions = Platform.select({
ios: 'Press Cmd+R to reload,\n' + 'Cmd+D or shake for dev menu',
android:
'Double tap R on your keyboard to reload,\n' +
'Shake or press menu button for dev menu',
});
type Props = {};
export default class App extends Component<Props> {
render() {
const facebook_button = (
<Icon.Button name="facebook" backgroundColor="#3b5998" size={20} onPress={()=>{Alert.alert("Facebook Button Clicked")}}>
<Text style={{fontFamily: 'Arial', fontSize: 15, color: '#fff'}}>Login with Facebook</Text>
</Icon.Button>
);
const twitter_button = (
<Icon.Button name="twitter" backgroundColor="#51aaf0" size={20} onPress={()=>{Alert.alert("Twitter Button Clicked")}}>
<Text style={{fontFamily: 'Arial', fontSize: 15, color: '#fff'}}>Follow Us on Twitter</Text>
</Icon.Button>
);
const android_icon = (
<Icon name="android" size={60} color="#007c00" onPress={()=>{Alert.alert("Android Icon Clicked")}} />
);
const music_icon = (
<Icon name="music" size={60} color="#fb3742" onPress={()=>{Alert.alert("Music Icon Clicked")}} />
);
return (
<View style={styles.MainContainer}>
<TouchableOpacity>
{facebook_button}
</TouchableOpacity>
<TouchableOpacity style={{marginTop: 10}}>
{twitter_button}
</TouchableOpacity>
<TouchableOpacity style={{marginTop: 10}}>
{android_icon}
</TouchableOpacity>
<TouchableOpacity style={{marginTop: 10}}>
{music_icon}
</TouchableOpacity>
</View>
);
}
}
const styles = StyleSheet.create({
MainContainer: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
padding: 20
}
});
Output: