Coder Social home page Coder Social logo

mvanroon / react-native-change-icon Goto Github PK

View Code? Open in Web Editor NEW

This project forked from skb1129/react-native-change-icon

0.0 1.0 0.0 452 KB

Change your application icon programmatically for React Native apps

License: MIT License

JavaScript 11.93% Objective-C 37.74% Ruby 4.42% Python 14.38% Java 31.52%

react-native-change-icon's Introduction

React Native Change Icon

npm version

Change Application Icon Programmatically.

  • iOS
  • Android

Getting started

$ yarn add install react-native-change-icon

Mostly automatic installation (Recommended)

$ react-native link react-native-change-icon

Manual installation

iOS

  1. In XCode, in the project navigator, right click LibrariesAdd Files to [your project's name]
  2. Go to node_modulesreact-native-change-icon and add RNChangeIcon.xcodeproj
  3. In XCode, in the project navigator, select your project. Add libRNChangeIcon.a to your project's Build PhasesLink Binary With Libraries
  4. Run your project (Cmd+R)<

Android

  1. 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 the getPackages() method
  1. 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')
    
  2. Insert the following lines inside the dependencies block in android/app/build.gradle:
      compile project(':react-native-change-icon')
    

Usage

iOS

  1. Open your project in Xcode and create an App Icons group inside you app's group.
  2. Add all the app icons you need inside this group with names like [email protected], [email protected].
  3. Your directory structure in Xcode would look like:

Xcode Directory

  1. Open the Info.plist file.
  2. Add Icon files (iOS 5) to the Information Property List.
  3. Add CFBundleAlternateIcons as a dictionary to the Icon files (iOS 5), it is used for alternative icons.
  4. Add dictionaries under CFBundleAlternateIcons named as your icon names in App Icons group.
  5. For each dictionary, two properties, UIPrerenderedIcon and CFBundleIconFiles need to be configured.
  6. Set the type of UIPrerenderedIcon to String and its value to NO.
  7. Set the type of CFBundleIconFiles to Array and set its first key, Item 0's type to String and its value to the corresponding icon names.
  8. After all these steps, your Info.plist file should look like:

Info.plist

Android

  1. Add all the icons you need inside your project's android/app/src/main/res/mipmap-* directories:

Android Directory

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

  1. Open the MainApplication.java file.
  2. Pass BuildConfig.APPLICATION_ID to new RNChangeIconPackage() inside the getPackages() 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

react-native-change-icon's People

Contributors

jpamarohorta avatar skb1129 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.