Change Application Icon Programmatically.
- iOS
- Android
$ yarn add install react-native-change-icon
$ react-native link react-native-change-icon
- In XCode, in the project navigator, right click
Libraries
➜Add Files to [your project's name]
- Go to
node_modules
➜react-native-change-icon
and addRNChangeIcon.xcodeproj
- In XCode, in the project navigator, select your project. Add
libRNChangeIcon.a
to your project'sBuild Phases
➜Link Binary With Libraries
- Run your project (
Cmd+R
)<
- Open up
android/app/src/main/java/[...]/MainApplication.java
- Add
import com.reactlibrary.RNChangeIconPackage;
to the imports at the top of the file - Add
new RNChangeIconPackage()
to the list returned by thegetPackages()
method
- Append the following lines to
android/settings.gradle
:include ':react-native-change-icon' project(':react-native-change-icon').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-change-icon/android')
- Insert the following lines inside the dependencies block in
android/app/build.gradle
:compile project(':react-native-change-icon')
- Open your project in Xcode and create an App Icons group inside you app's group.
- Add all the app icons you need inside this group with names like [email protected], [email protected].
- Your directory structure in Xcode would look like:
- Open the Info.plist file.
- Add
Icon files (iOS 5)
to the Information Property List. - Add
CFBundleAlternateIcons
as a dictionary to theIcon files (iOS 5)
, it is used for alternative icons. - Add dictionaries under
CFBundleAlternateIcons
named as your icon names in App Icons group. - For each dictionary, two properties,
UIPrerenderedIcon
andCFBundleIconFiles
need to be configured. - Set the type of
UIPrerenderedIcon
toString
and its value toNO
. - Set the type of
CFBundleIconFiles
toArray
and set its first key,Item 0
's type toString
and its value to the corresponding icon names. - After all these steps, your Info.plist file should look like:
- Add all the icons you need inside your project's
android/app/src/main/res/mipmap-*
directories:
- Modify your
AndroidManifest.xml
file's<application>
tag as following:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:name=".MainApplication"
android:label="@string/app_name"
android:icon="@mipmap/checked"
android:allowBackup="false"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity" />
<activity-alias
android:name="com.example.MainActivitychecked"
android:enabled="true"
android:icon="@mipmap/checked"
android:targetActivity=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity-alias>
<activity-alias
android:name="com.example.MainActivitycancel"
android:enabled="false"
android:icon="@mipmap/cancel"
android:targetActivity=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity-alias>
<activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
</application>
</manifest>
You can create more <activity-alias>
tags to make more alternate icons.
Note that the name in should be "com.{package_name}.MainActivity%", where %
is the icon name.
- Open the
MainApplication.java
file. - Pass
BuildConfig.APPLICATION_ID
tonew RNChangeIconPackage()
inside thegetPackages()
function.
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new RNChangeIconPackage(BuildConfig.APPLICATION_ID)
);
}
Note that all the icon names must be in lowercase and only limited to alphabets a-z
Now you can use the following code to change application icon:
import { changeIcon } from 'react-native-change-icon';
// Pass the name of icon to be enabled
changeIcon('icon_name');
changeIcon
function returns a promise. The promise is resolved only when the icon is changed.
Please refer to the Example app for demo on implementation