4

I have seen other android apps have a selection for app icon, and then the changes are reflected in the app drawer and on home screen instantly.

How do these apps change app icon like this?

rosghub
  • 8,924
  • 4
  • 24
  • 37
  • 5
    Possible duplicate of [How to change an application icon programmatically in Android?](http://stackoverflow.com/questions/1103027/how-to-change-an-application-icon-programmatically-in-android) – cyroxis May 06 '16 at 21:01
  • which "other android apps" are you referring to? – user1506104 May 16 '18 at 05:52

1 Answers1

6

First prepare your new app icons for each resolution and put them into the corresponding mipmap folders.

Then use activity-alias, for example, in your AndroidManifest.xml, edit this:

    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>

            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>

    <activity-alias
        android:name=".MainAliasActivity"
        android:enabled="false"
        android:icon="@mipmap/ic_launcher_fight"
        android:label="Main Alias Activity"
        android:targetActivity=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity-alias>

Add the following code when you want to change your app icon

        PackageManager pm = getPackageManager();
        pm.setComponentEnabledSetting(
                getComponentName(),
                PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                PackageManager.DONT_KILL_APP);
        pm.setComponentEnabledSetting(
                new ComponentName(this, "YOUR PACKAGE.MainAliasActivity"),
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                PackageManager.DONT_KILL_APP);

To reset the app icon you can use the same way.

GitHub Project here

Lim CHAN
  • 1,216
  • 1
  • 12
  • 21