skb1129/react-native-change-icon
About
Change your application icon programmatically in React Native apps
Web & App DevelopmentMeasured star growth
- in 24h
- 0
- in 7 days
- 0
- in 15 days
- -1
- in 1 month
- Collecting history
Net star changes over overlapping periods, not daily totals. Missing measurements are left blank. Stars measure attention, not software quality.
README.md
Original
React Native Change Icon
Programmatically change the application icon.
Supported Platforms
yarn add react-native-change-icon
npm i react-native-change-icon
How to Use
1. Make your Icons
I'd suggest exporting them around `1024px` or higher.
2. Generate Icons
Use a service such as https://www.appicon.co/ in order to generate the platform specific icon files.
- Just upload your images from earlier, and checkmark both
iPhoneandAndroid. - This will give you a
.zipfile with the files needed.
3. Prepare Icon Files
- You need to rename and sort these files slightly differently for both
iOSandAndroid.
Android 🤖
- Simply just rename them to something appropriate - typically this follows the naming convention
ic_launcher_<type>.pnge.g.ic_launcher_dark.png- Make sure to keep them within the folder structure they are in
mipmap-hdpi... etc.
- Make sure to keep them within the folder structure they are in
- Create a single
androiddirectory with all themipmap-*directories inside. Inside them place all your generated icons.
iOS 🍏
- You will need the generated folder called
AppIcon.appiconsetas this contains your icons. - Rename this folder a bit like above for Android using a naming convention such as
<type>.appiconsete.g.Dark.appiconset - You will also need to edit the
Contents.jsonto change and references fromAssets.xcassets/AppIcon.appiconsetto what you have renamed the file now e.g.Images.xcassets/AppIcon.appiconset
4. Import the Icons
Android 🤖
- Drag all of the
mipmapfolders intoandroid/app/src/main/res/
iOS 🍏
- Drag all of the
.appiconsetfolders intoios/<app-name>/Images.xcassets
5. Setup Environments
Android 🤖
- Add an alias for each of your new icons within the
AndroidManifest.xml(within<application>).- Make sure these have the properties as shown below.
- Create an alias for
.MainActivityDefaultas well but for this, setandroid:enabled="true". - For the name prefix it
.MainActivity...followed by the name you will use to reference your icon. e.g. for our light icon we will use.MainActivityLight
- You'll have to remove the
LAUNCHERintent filter from the main<activity>as we have added the launcher in.MainActivityDefault.
<activity-alias
android:name=".MainActivityLight"
android:enabled="false"
android:exported="true"
android:icon="@mipmap/ic_launcher_light"
android:targetActivity=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity-alias>
iOS 🍏
- At the bottom of your
Info.plistinsert a key forCFBundleIcons- Note: For iPad, you need to add the key
CFBundleIcons~ipad
- Note: For iPad, you need to add the key
- Within this dictionary add another key for
CFBundleAlternateIcons - Finally then within this dictionary you can add in the keys for you new icons
- The
keyis the name you will reference from within code. - Set the first array item to the name of the
.appiconsetwe created earlier.
- The
- In XCode, in your app's
Generalsettings, underApp Icons and Launch Screen, set "App Icon" toDefaultand check the "Include all app icon assets" checkbox below.
<key>Dark</key>
<dict>
<key>CFBundleIconFiles</key>
<array>
<string>Dark</string>
</array>
<key>UIPrerenderedIcon</key>
<false/>
</dict>
6. Use the Icons
You can now use your icons and switch between them within React Native 🎉
import { changeIcon, getIcon, resetIcon } from 'react-native-change-icon';
// Pass the name of icon to be enabled
changeIcon('Dark');
changeIcon('Light');
// Get the icon currently enabled
getIcon();
// Reset the Icon to the default
resetIcon();
All functions are typed and return a promise that either resolves successfully, or will reject with the error that has occurred.
react-native-push-notification and notifee
When using react-native-push-notification or notifee, notifications won't work as we are using activity-alias.
To fix this, you need to create a Java file for each of the activity-alias in your AndroidManifest.xml.
The file should be placed alongside you MainActivity.java. Example:
android/app/src/main/java/com/myapp/MainActivity<KEY>.java
The content of this file should be:
package com.myapp;
public class MainActivity<KEY> extends MainActivity {}
Replace <KEY> with the icon name used in the manifest. Replace com.myapp with your android app structure.
Explore related projects
Loading…