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:

  1. npm install react-native-vector-icons –save  
React Native Vector Icons

After successful execution of the above code, it adds the react-native-vector-icons library.

React Native Vector Icons

2. Open your_react_native_project->android -> app -> build.gradle file and put below code of line inside it.

  1. 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.

  1. 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:

  1. apply plugin: “com.android.application”  
  2. apply from: “../../node_modules/react-native-vector-icons/fonts.gradle”  
  3. import com.android.build.OutputFile  
  4.   
  5. project.ext.react = [  
  6.     entryFile: “index.js”  
  7. ]  
  8.   
  9. apply from: “../../node_modules/react-native/react.gradle”  
  10.   
  11. def enableSeparateBuildPerCPUArchitecture = false  
  12.   
  13. /** 
  14.  * Run Proguard to shrink the Java bytecode in release builds. 
  15.  */  
  16. def enableProguardInReleaseBuilds = false  
  17.   
  18. android {  
  19.     compileSdkVersion rootProject.ext.compileSdkVersion  
  20.     buildToolsVersion rootProject.ext.buildToolsVersion  
  21.   
  22.     defaultConfig {  
  23.         applicationId “com.vectoricons”  
  24.         minSdkVersion rootProject.ext.minSdkVersion  
  25.         targetSdkVersion rootProject.ext.targetSdkVersion  
  26.         versionCode 1  
  27.         versionName “1.0”  
  28.         ndk {  
  29.             abiFilters “armeabi-v7a”, “x86”  
  30.         }  
  31.     }  
  32.     splits {  
  33.         abi {  
  34.             reset()  
  35.             enable enableSeparateBuildPerCPUArchitecture  
  36.             universalApk false  // If true, also generate a universal APK  
  37.             include “armeabi-v7a”, “x86”  
  38.         }  
  39.     }  
  40.     buildTypes {  
  41.         release {  
  42.             minifyEnabled enableProguardInReleaseBuilds  
  43.             proguardFiles getDefaultProguardFile(“proguard-android.txt”), “proguard-rules.pro”  
  44.         }  
  45.     }  
  46.     // applicationVariants are e.g. debug, release  
  47.     applicationVariants.all { variant ->  
  48.         variant.outputs.each { output ->  
  49.             // For each separate APK per architecture, set a unique version code as described here:  
  50.             // http://tools.android.com/tech-docs/new-build-system/user-guide/apk-splits  
  51.             def versionCodes = [“armeabi-v7a”:1, “x86”:2]  
  52.             def abi = output.getFilter(OutputFile.ABI)  
  53.             if (abi != null) {  // null for the universal-debug, universal-release variants  
  54.                 output.versionCodeOverride =  
  55.                         versionCodes.get(abi) * 1048576 + defaultConfig.versionCode  
  56.             }  
  57.         }  
  58.     }  
  59. }  
  60.   
  61. dependencies {  
  62.     compile project(‘:react-native-vector-icons’)  
  63.     implementation fileTree(dir: “libs”, include: [“*.jar”])  
  64.     implementation “com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}”  
  65.     implementation “com.facebook.react:react-native:+”  // From node_modules  
  66.     implementation project(‘:react-native-vector-icons’)  
  67. }  
  68.   
  69. // Run this once to be able to run the application with BUCK  
  70. // puts all compile dependencies into folder libs for BUCK to use  
  71. task copyDownloadableDepsToLibs(type: Copy) {  
  72.     from configurations.compile  
  73.     into ‘libs’  
  74. }  

4. Open your_react_native_project-> android-> settings.gradle file and add the below code:

  1. include ‘:react-native-vector-icons’  
  2. project(‘:react-native-vector-icons’).projectDir = new File(rootProject.projectDir, ‘../node_modules/react-native-vector-icons/android’)  

your_react_native_project-> android-> settings.gradle

  1. rootProject.name = ‘VectorIcons’  
  2. include ‘:react-native-vector-icons’  
  3. project(‘:react-native-vector-icons’).projectDir = new File(rootProject.projectDir, ‘../node_modules/react-native-vector-icons/android’)  
  4. 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.

  1. 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.

  1. new VectorIconsPackage()  

MainApplication.java

  1. package com.vectoricons;  
  2. import android.app.Application;  
  3. import com.facebook.react.ReactApplication;  
  4. import com.facebook.react.ReactNativeHost;  
  5. import com.facebook.react.ReactPackage;  
  6. import com.facebook.react.shell.MainReactPackage;  
  7. import com.facebook.soloader.SoLoader;  
  8. import com.oblador.vectoricons.VectorIconsPackage;  
  9. import java.util.Arrays;  
  10. import java.util.List;  
  11.   
  12. public class MainApplication extends Application implements ReactApplication {  
  13.   
  14.   private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {  
  15.     @Override  
  16.     public boolean getUseDeveloperSupport() {  
  17.       return BuildConfig.DEBUG;  
  18.     }  
  19.   
  20.     @Override  
  21.     protected List<ReactPackage> getPackages() {  
  22.       return Arrays.<ReactPackage>asList(  
  23.           new MainReactPackage(),  
  24.           new VectorIconsPackage()  
  25.       );  
  26.     }  
  27.   
  28.     @Override  
  29.     protected String getJSMainModuleName() {  
  30.       return “index”;  
  31.     }  
  32.   };  
  33.   
  34.   @Override  
  35.   public ReactNativeHost getReactNativeHost() {  
  36.     return mReactNativeHost;  
  37.   }  
  38.   @Override  
  39.   public void onCreate() {  
  40.     super.onCreate();  
  41.     SoLoader.init(this/* native exopackage */ false);  
  42.   }  
  43. }  

Linking of Dependency

After installing the above code, we need to link it with our project.

  1. react-native link  
React Native Vector Icons

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:

PropsDescription
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.
colorIt sets the color of Text and Icon.

Create two another constant of Icon named as android_icon and music_icon inside render block.

PropsDescription
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

  1. /** 
  2.  * Sample React Native App 
  3.  * https://github.com/facebook/react-native 
  4.  * 
  5.  * @format 
  6.  * @flow 
  7.  */  
  8.   
  9. import React, {Component} from ‘react’;  
  10. import {Platform, StyleSheet, Text, View, TouchableOpacity, Alert} from ‘react-native’;  
  11. import Icon from ‘react-native-vector-icons/FontAwesome’;  
  12.   
  13. const instructions = Platform.select({  
  14.   ios: ‘Press Cmd+R to reload,\n’ + ‘Cmd+D or shake for dev menu’,  
  15.   android:  
  16.     ‘Double tap R on your keyboard to reload,\n’ +  
  17.     ‘Shake or press menu button for dev menu’,  
  18. });  
  19.   
  20. type Props = {};  
  21. export default class App extends Component<Props> {  
  22.   render() {  
  23.       const facebook_button = (  
  24.         <Icon.Button name=”facebook” backgroundColor=”#3b5998″ size={20} onPress={()=>{Alert.alert(“Facebook Button Clicked”)}}>  
  25.           <Text style={{fontFamily: ‘Arial’, fontSize: 15, color: ‘#fff’}}>Login with Facebook</Text>  
  26.         </Icon.Button>  
  27.       );  
  28.       
  29.       const twitter_button = (  
  30.        <Icon.Button name=”twitter” backgroundColor=”#51aaf0″ size={20} onPress={()=>{Alert.alert(“Twitter Button Clicked”)}}>  
  31.          <Text style={{fontFamily: ‘Arial’, fontSize: 15, color: ‘#fff’}}>Follow Us on Twitter</Text>  
  32.        </Icon.Button>  
  33.       );  
  34.   
  35.       const android_icon = (  
  36.        <Icon name=”android” size={60} color=”#007c00″ onPress={()=>{Alert.alert(“Android Icon Clicked”)}} />  
  37.       );  
  38.    
  39.       const music_icon = (  
  40.        <Icon name=”music” size={60} color=”#fb3742″ onPress={()=>{Alert.alert(“Music Icon Clicked”)}} />  
  41.       );  
  42.   
  43.     return (  
  44.       <View style={styles.MainContainer}>  
  45.           <TouchableOpacity>  
  46.             {facebook_button}  
  47.          </TouchableOpacity>  
  48.     
  49.         <TouchableOpacity style={{marginTop: 10}}>  
  50.            {twitter_button}  
  51.          </TouchableOpacity>  
  52.    
  53.    
  54.         <TouchableOpacity style={{marginTop: 10}}>  
  55.            {android_icon}  
  56.          </TouchableOpacity>  
  57.    
  58.         <TouchableOpacity style={{marginTop: 10}}>  
  59.           {music_icon}  
  60.         </TouchableOpacity>  
  61.       </View>  
  62.    
  63.     );  
  64.    
  65.   }  
  66. }  
  67. const styles = StyleSheet.create({  
  68.   MainContainer: {  
  69.     flex: 1,  
  70.     justifyContent: ‘center’,  
  71.     alignItems: ‘center’,  
  72.     backgroundColor: ‘#F5FCFF’,  
  73.     padding: 20  
  74.   }  
  75. });  

Output:

React Native Vector Icons
React Native Vector Icons

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *