Hello friends, We’re back with an amazing tutorial of react native. Many of us heard about Vector Icons and does not know properly how to use them in react native. First of all some information about vector icons, Vector icons has one of the largest Icon library for developers to freely use. To use vector icons in react native application we have to use their official NPM package react-native-vector-icons. But before starting using this package we have to configure it properly for our project. So in this tutorial we would learn about React Native Vector Icons Example.
List of Icons category comes with React Native Vector Icons :-
- AntDesign by AntFinance
- Entypo by Daniel Bruce
- EvilIcons by Alexander Madyankin & Roman Shamin
- Feather by Cole Bemis & Contributors
- FontAwesome by Dave Gandy
- FontAwesome 5 by Fonticons
- Fontisto by Kenan Gündoğan
- Foundation by ZURB
- Ionicons by Iconic Framework
- MaterialIcons by Google
- MaterialCommunityIcons by MaterialDesignIcons.com
- Octicons by Github
- Zocial by Sam Collins
- SimpleLineIcons by Sabbir & Contributors
One more thing, If you want to see all of the ICONS then Visit here.
Contents in this project React Native Vector Icons Example :-
1. Configure React Native Vector Icons for Android :-
1. First of all we have to install the React Native Vector Icons package in our react native project. So open your project’s main Root directory in CMD and execute below command to install it.
1 |
npm install react-native-vector-icons --save |
Screenshot :-
Here you go now the package is successfully installed in your project.
2. Now it’s time to make changes in some internal project files. So Open Your_React_Native_Project -> android -> app -> build.gradle file and put below fonts.gradlew line inside it.
1 |
apply from: "../../node_modules/react-native-vector-icons/fonts.gradle" |
3. Now again we have to put below line in Your_React_Native_Project -> android -> app -> build.gradle file in dependencies block.
1 |
compile project(':react-native-vector-icons') |
Source Code of My build.gradle file after adding above code :-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
apply plugin: "com.android.application" apply from: "../../node_modules/react-native-vector-icons/fonts.gradle" import com.android.build.OutputFile project.ext.react = [ enableHermes: false, // clean and rebuild if changing ] apply from: "../../node_modules/react-native/react.gradle" def enableSeparateBuildPerCPUArchitecture = false def enableProguardInReleaseBuilds = false def jscFlavor = 'org.webkit:android-jsc:+' def enableHermes = project.ext.react.get("enableHermes", false); def nativeArchitectures = project.getProperties().get("reactNativeDebugArchitectures") android { ndkVersion rootProject.ext.ndkVersion compileSdkVersion rootProject.ext.compileSdkVersion defaultConfig { applicationId "com.myapp" minSdkVersion rootProject.ext.minSdkVersion targetSdkVersion rootProject.ext.targetSdkVersion versionCode 1 versionName "1.0" } splits { abi { reset() enable enableSeparateBuildPerCPUArchitecture universalApk false // If true, also generate a universal APK include "armeabi-v7a", "x86", "arm64-v8a", "x86_64" } } signingConfigs { debug { storeFile file('debug.keystore') storePassword 'android' keyAlias 'androiddebugkey' keyPassword 'android' } } buildTypes { debug { signingConfig signingConfigs.debug if (nativeArchitectures) { ndk { abiFilters nativeArchitectures.split(',') } } } release { signingConfig signingConfigs.debug minifyEnabled enableProguardInReleaseBuilds proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro" } } // applicationVariants are e.g. debug, release applicationVariants.all { variant -> variant.outputs.each { output -> def versionCodes = ["armeabi-v7a": 1, "x86": 2, "arm64-v8a": 3, "x86_64": 4] def abi = output.getFilter(OutputFile.ABI) if (abi != null) { // null for the universal-debug, universal-release variants output.versionCodeOverride = defaultConfig.versionCode * 1000 + versionCodes.get(abi) } } } } dependencies { implementation fileTree(dir: "libs", include: ["*.jar"]) //noinspection GradleDynamicVersion implementation "com.facebook.react:react-native:+" // From node_modules implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.0.0" debugImplementation("com.facebook.flipper:flipper:${FLIPPER_VERSION}") { exclude group:'com.facebook.fbjni' } debugImplementation("com.facebook.flipper:flipper-network-plugin:${FLIPPER_VERSION}") { exclude group:'com.facebook.flipper' exclude group:'com.squareup.okhttp3', module:'okhttp' } debugImplementation("com.facebook.flipper:flipper-fresco-plugin:${FLIPPER_VERSION}") { exclude group:'com.facebook.flipper' } if (enableHermes) { def hermesPath = "../../node_modules/hermes-engine/android/"; debugImplementation files(hermesPath + "hermes-debug.aar") releaseImplementation files(hermesPath + "hermes-release.aar") } else { implementation jscFlavor } compile project(':react-native-vector-icons') } task copyDownloadableDepsToLibs(type: Copy) { from configurations.implementation into 'libs' } apply from: file("../../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesAppBuildGradle(project) |
4. Now Open Your_React_Native_Project -> android -> settings.gradle file and put both below lines inside it.
1 2 3 |
include ':react-native-vector-icons' project(':react-native-vector-icons').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-vector-icons/android') |
Source Code of My settings.gradle file after adding above code :-
1 2 3 4 5 6 7 |
rootProject.name = 'myapp' apply from: file("../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesSettingsGradle(settings) include ':app' include ':react-native-vector-icons' project(':react-native-vector-icons').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-vector-icons/android') |
5. Open Your_React_Native_Project -> android -> app -> src -> main -> java-> com -> ProjectName -> MainApplication.java and Import below VectorIconsPackage.
1 |
import com.oblador.vectoricons.VectorIconsPackage; |
Now we have to add below code inside List<ReactPackage> getPackages() block.
1 |
new VectorIconsPackage(); |
Source Code of My MainApplication.java file after adding above code :-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
package com.myapp; import android.app.Application; import android.content.Context; import com.facebook.react.PackageList; import com.facebook.react.ReactApplication; import com.facebook.react.ReactInstanceManager; import com.facebook.react.ReactNativeHost; import com.facebook.react.ReactPackage; import com.facebook.soloader.SoLoader; import java.lang.reflect.InvocationTargetException; import java.util.List; import com.oblador.vectoricons.VectorIconsPackage; 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() { @SuppressWarnings("UnnecessaryLocalVariable") List<ReactPackage> packages = new PackageList(this).getPackages(); // Packages that cannot be autolinked yet can be added manually here, for example: // packages.add(new MyReactNativePackage()); new VectorIconsPackage(); return packages; } @Override protected String getJSMainModuleName() { return "index"; } }; @Override public ReactNativeHost getReactNativeHost() { return mReactNativeHost; } @Override public void onCreate() { super.onCreate(); SoLoader.init(this, /* native exopackage */ false); initializeFlipper(this, getReactNativeHost().getReactInstanceManager()); } /** * Loads Flipper in React Native templates. Call this in the onCreate method with something like * initializeFlipper(this, getReactNativeHost().getReactInstanceManager()); * * @param context * @param reactInstanceManager */ private static void initializeFlipper( Context context, ReactInstanceManager reactInstanceManager) { if (BuildConfig.DEBUG) { try { /* We use reflection here to pick up the class that initializes Flipper, since Flipper library is not available in release mode */ Class<?> aClass = Class.forName("com.myapp.ReactNativeFlipper"); aClass .getMethod("initializeFlipper", Context.class, ReactInstanceManager.class) .invoke(null, context, reactInstanceManager); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } } } |
6. Now in the final step all we have to do is clean our previously build Gradlew file and rebuilt it again. So go to your project’s main Root directory and execute below command to rebuild the gradlew again.
1 |
cd android && gradlew clean && cd.. |
Now you are good to go in android with react native vector icons.
2. Configure React Native Vector Icons for iOS :-
1. First we have to install the vector icons package in our react native iOS project. So open your project’s main Root location in Terminal and execute below command.
1 |
npm install react-native-vector-icons --save |
2. Now we have to Install PODS and link the project to vector icons package.
1 |
cd ios && pod install && cd .. |
3. Now we have to manually add all of the ICONS ttf file in project’s main info.plist file. So open Your_React_Native_Project -> ios -> Your_Project -> info.plist file and add below code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<key>UIAppFonts</key> <array> <string>AntDesign.ttf</string> <string>Entypo.ttf</string> <string>EvilIcons.ttf</string> <string>Feather.ttf</string> <string>FontAwesome.ttf</string> <string>FontAwesome5_Brands.ttf</string> <string>FontAwesome5_Regular.ttf</string> <string>FontAwesome5_Solid.ttf</string> <string>Foundation.ttf</string> <string>Ionicons.ttf</string> <string>MaterialIcons.ttf</string> <string>MaterialCommunityIcons.ttf</string> <string>SimpleLineIcons.ttf</string> <string>Octicons.ttf</string> <string>Zocial.ttf</string> </array> |
Source Code of My info.plist file after adding above code :-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>CFBundleDevelopmentRegion</key> <string>en</string> <key>CFBundleDisplayName</key> <string>projectApp</string> <key>CFBundleExecutable</key> <string>$(EXECUTABLE_NAME)</string> <key>CFBundleIdentifier</key> <string>$(PRODUCT_BUNDLE_IDENTIFIER)</string> <key>CFBundleInfoDictionaryVersion</key> <string>6.0</string> <key>CFBundleName</key> <string>$(PRODUCT_NAME)</string> <key>CFBundlePackageType</key> <string>APPL</string> <key>CFBundleShortVersionString</key> <string>1.0</string> <key>CFBundleSignature</key> <string>????</string> <key>CFBundleVersion</key> <string>1</string> <key>LSRequiresIPhoneOS</key> <true/> <key>NSAppTransportSecurity</key> <dict> <key>NSExceptionDomains</key> <dict> <key>localhost</key> <dict> <key>NSExceptionAllowsInsecureHTTPLoads</key> <true/> </dict> </dict> </dict> <key>NSLocationWhenInUseUsageDescription</key> <string></string> <key>UILaunchStoryboardName</key> <string>LaunchScreen</string> <key>UIRequiredDeviceCapabilities</key> <array> <string>armv7</string> </array> <key>UISupportedInterfaceOrientations</key> <array> <string>UIInterfaceOrientationPortrait</string> <string>UIInterfaceOrientationLandscapeLeft</string> <string>UIInterfaceOrientationLandscapeRight</string> </array> <key>UIViewControllerBasedStatusBarAppearance</key> <false/> <key>UIAppFonts</key> <array> <string>AntDesign.ttf</string> <string>Entypo.ttf</string> <string>EvilIcons.ttf</string> <string>Feather.ttf</string> <string>FontAwesome.ttf</string> <string>FontAwesome5_Brands.ttf</string> <string>FontAwesome5_Regular.ttf</string> <string>FontAwesome5_Solid.ttf</string> <string>Foundation.ttf</string> <string>Ionicons.ttf</string> <string>MaterialIcons.ttf</string> <string>MaterialCommunityIcons.ttf</string> <string>SimpleLineIcons.ttf</string> <string>Octicons.ttf</string> <string>Zocial.ttf</string> </array> </dict> </plist> |
Here you go friends. Now we are good to go in iOS also.
3. Start Coding for App :-
1. Open your project’s main App.js file and import StyleSheet, Text, Alert, TouchableOpacity and SafeAreaView component.
1 2 3 |
import React from 'react'; import { StyleSheet, Text, Alert, TouchableOpacity, SafeAreaView } from 'react-native'; |
2. Import Icon component from FontAwesome icons in the package. I’m also mentioning all the icons combos. You can Visit their page from here and see all the List of Icons they have.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import Icon from 'react-native-vector-icons/FontAwesome'; // import Icon from 'react-native-vector-icons/AntDesign'; // import Icon from 'react-native-vector-icons/Entypo'; // import Icon from 'react-native-vector-icons/EvilIcons'; // import Icon from 'react-native-vector-icons/Feather'; // import Icon from 'react-native-vector-icons/FontAwesome 5'; // import Icon from 'react-native-vector-icons/Foundation'; // import Icon from 'react-native-vector-icons/Ionicons'; // import Icon from 'react-native-vector-icons/MaterialIcons'; // import Icon from 'react-native-vector-icons/MaterialCommunityIcons'; // import Icon from 'react-native-vector-icons/Octicons'; // import Icon from 'react-native-vector-icons/Zocial'; // import Icon from 'react-native-vector-icons/SimpleLineIcons'; |
3. Creating our main App component.
1 2 3 4 5 |
export default function App() { } |
4. Creating a Constant named as facebook_button. In this component we would make a facebook login button using Icon.Button component.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
const facebook_button = ( <Icon.Button name="facebook" backgroundColor="#3b5998" size={21} onPress={() => { Alert.alert("Facebook Button Clicked") }}> <Text style={styles.buttonText}> Login with Facebook </Text> </Icon.Button> ); |
Button Screenshot :-
5. Creating another constant component named as twitter_button.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
const twitter_button = ( <Icon.Button name="twitter" backgroundColor="#51aaf0" size={21} onPress={() => { Alert.alert("Twitter Button Clicked") }}> <Text style={styles.buttonText}> Follow Us on Twitter </Text> </Icon.Button> ); |
6. Creating one more button component named as github_button.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
const github_button = ( <Icon.Button name="github" backgroundColor="#CFD8DC" iconStyle={{ color: 'black' }} size={21} onPress={() => { Alert.alert("Github Button Clicked") }}> <Text style={{ fontFamily: 'Arial', fontSize: 15, color: 'black' }}> Follow Us on GitHub </Text> </Icon.Button> ); |
Button Screenshot :-
7. We can also create indivijual icons using the Icon component. So we’re creating 5 types of Icon component.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
const android_icon = ( <Icon name="android" size={50} color="#007c00" onPress={() => { Alert.alert("Android Icon Clicked") }} /> ); const music_icon = ( <Icon name="music" size={50} color="#fb3742" onPress={() => { Alert.alert("Music Icon Clicked") }} /> ); const heart_icon = ( <Icon name="heart" size={50} color='red' onPress={() => { Alert.alert("Heart Icon Clicked") }} /> ); const camera_icon = ( <Icon name="camera" size={50} color="#006064" onPress={() => { Alert.alert("Camera Icon Clicked") }} /> ); const github_icon = ( <Icon name="github" size={50} color="#1B5E20" onPress={() => { Alert.alert("Github Icon Clicked") }} /> ); |
Screenshot of Icons :-
8. Creating return() block, Now we would call all aove defined constants in Touchable Opacity component.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
return ( <SafeAreaView style={styles.MainContainer}> <TouchableOpacity> {facebook_button} </TouchableOpacity> <TouchableOpacity style={{ marginTop: 10 }}> {twitter_button} </TouchableOpacity> <TouchableOpacity style={{ marginTop: 10 }}> {github_button} </TouchableOpacity> <TouchableOpacity style={{ marginTop: 10 }}> {android_icon} </TouchableOpacity> <TouchableOpacity style={{ marginTop: 10 }}> {music_icon} </TouchableOpacity> <TouchableOpacity style={{ marginTop: 10 }}> {heart_icon} </TouchableOpacity> <TouchableOpacity style={{ marginTop: 10 }}> {camera_icon} </TouchableOpacity> <TouchableOpacity style={{ marginTop: 10 }}> {github_icon} </TouchableOpacity> </SafeAreaView> ); |
9. Creating Style.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
const styles = StyleSheet.create({ MainContainer: { flex: 1, justifyContent: 'center', alignItems: 'center', }, buttonText: { fontFamily: 'Arial', fontSize: 15, color: '#fff' } }); |
10. Complete Source Code for App.js file :-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
import React from 'react'; import { StyleSheet, Text, Alert, TouchableOpacity, SafeAreaView } from 'react-native'; import Icon from 'react-native-vector-icons/FontAwesome'; // import Icon from 'react-native-vector-icons/AntDesign'; // import Icon from 'react-native-vector-icons/Entypo'; // import Icon from 'react-native-vector-icons/EvilIcons'; // import Icon from 'react-native-vector-icons/Feather'; // import Icon from 'react-native-vector-icons/FontAwesome 5'; // import Icon from 'react-native-vector-icons/Foundation'; // import Icon from 'react-native-vector-icons/Ionicons'; // import Icon from 'react-native-vector-icons/MaterialIcons'; // import Icon from 'react-native-vector-icons/MaterialCommunityIcons'; // import Icon from 'react-native-vector-icons/Octicons'; // import Icon from 'react-native-vector-icons/Zocial'; // import Icon from 'react-native-vector-icons/SimpleLineIcons'; export default function App() { const facebook_button = ( <Icon.Button name="facebook" backgroundColor="#3b5998" size={21} onPress={() => { Alert.alert("Facebook Button Clicked") }}> <Text style={styles.buttonText}> Login with Facebook </Text> </Icon.Button> ); const twitter_button = ( <Icon.Button name="twitter" backgroundColor="#51aaf0" size={21} onPress={() => { Alert.alert("Twitter Button Clicked") }}> <Text style={styles.buttonText}> Follow Us on Twitter </Text> </Icon.Button> ); const github_button = ( <Icon.Button name="github" backgroundColor="#CFD8DC" iconStyle={{ color: 'black' }} size={21} onPress={() => { Alert.alert("Github Button Clicked") }}> <Text style={{ fontFamily: 'Arial', fontSize: 15, color: 'black' }}> Follow Us on GitHub </Text> </Icon.Button> ); const android_icon = ( <Icon name="android" size={50} color="#007c00" onPress={() => { Alert.alert("Android Icon Clicked") }} /> ); const music_icon = ( <Icon name="music" size={50} color="#fb3742" onPress={() => { Alert.alert("Music Icon Clicked") }} /> ); const heart_icon = ( <Icon name="heart" size={50} color='red' onPress={() => { Alert.alert("Heart Icon Clicked") }} /> ); const camera_icon = ( <Icon name="camera" size={50} color="#006064" onPress={() => { Alert.alert("Camera Icon Clicked") }} /> ); const github_icon = ( <Icon name="github" size={50} color="#1B5E20" onPress={() => { Alert.alert("Github Icon Clicked") }} /> ); return ( <SafeAreaView style={styles.MainContainer}> <TouchableOpacity> {facebook_button} </TouchableOpacity> <TouchableOpacity style={{ marginTop: 10 }}> {twitter_button} </TouchableOpacity> <TouchableOpacity style={{ marginTop: 10 }}> {github_button} </TouchableOpacity> <TouchableOpacity style={{ marginTop: 10 }}> {android_icon} </TouchableOpacity> <TouchableOpacity style={{ marginTop: 10 }}> {music_icon} </TouchableOpacity> <TouchableOpacity style={{ marginTop: 10 }}> {heart_icon} </TouchableOpacity> <TouchableOpacity style={{ marginTop: 10 }}> {camera_icon} </TouchableOpacity> <TouchableOpacity style={{ marginTop: 10 }}> {github_icon} </TouchableOpacity> </SafeAreaView> ); } const styles = StyleSheet.create({ MainContainer: { flex: 1, justifyContent: 'center', alignItems: 'center', }, buttonText: { fontFamily: 'Arial', fontSize: 15, color: '#fff' } }); |
Screenshots :-
thanks you sir from indonesia
Welcome 🙂 .
thanyou sir
Welcome 🙂 .