Methods to Programmatically Change Android App Icon

Nov 27, 2025 · Programming · 10 views · 7.8

Keywords: Android | App Icon | Shortcut | Programming

Abstract: This article explores methods to change the Android app icon programmatically, focusing on using shortcuts and activity aliases. It provides step-by-step code examples and discusses limitations, including permission configuration, code implementation, and compatibility issues.

In Android development, changing the application icon programmatically is a common requirement for customizing user experience. However, directly modifying resources in the APK is not feasible after installation, as the signed APK is locked. This article discusses practical approaches, primarily using shortcuts, to achieve this functionality, with supplementary details on activity alias methods.

Using Shortcuts to Change App Icon

The most effective method involves manipulating the app's shortcut on the home screen. By uninstalling and reinstalling the shortcut with a new icon, users can see the updated icon. This approach requires specific permissions and uses broadcasts to interact with the launcher.

First, add the following permissions to your AndroidManifest.xml file:

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />

Next, implement methods to add and remove shortcuts. Here's a rewritten example based on the core concept:

private void addShortcut(String name, Bitmap iconBitmap) {
    Intent shortcutIntent = new Intent(this, MainActivity.class);
    shortcutIntent.setAction(Intent.ACTION_MAIN); // Use a custom action if needed

    Intent installIntent = new Intent();
    installIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    installIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
    installIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON, iconBitmap);
    installIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
    sendBroadcast(installIntent);
}

private void removeShortcut(String name) {
    Intent shortcutIntent = new Intent(this, MainActivity.class);
    shortcutIntent.setAction(Intent.ACTION_MAIN);

    Intent uninstallIntent = new Intent();
    uninstallIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    uninstallIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
    uninstallIntent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
    sendBroadcast(uninstallIntent);
}

In your activity, you can use these methods to change the icon. For instance, when a button is clicked, remove the old shortcut and add a new one with a different icon.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button changeIconButton = findViewById(R.id.change_icon_button);
    changeIconButton.setOnClickListener(v -> {
        removeShortcut("MyApp");
        // Create a new bitmap for the icon, e.g., from resources or dynamically
        Bitmap newIcon = BitmapFactory.decodeResource(getResources(), R.drawable.new_icon);
        addShortcut("MyApp", newIcon);
    });
}

Note that this method may show toast messages from the launcher and is not supported on all devices or launchers. Testing on target devices is recommended.

Using Activity Alias for Icon Change

Another approach is to use activity-alias in the manifest. This involves defining multiple aliases for the main activity, each with a different icon, and enabling/disabling them programmatically.

First, modify the AndroidManifest.xml to remove the MAIN intent-filter from the main activity and add activity aliases:

<activity android:name=".MainActivity">
    <!-- Remove the MAIN intent-filter if present -->
</activity>
<activity-alias android:name=".MainActivityAlias1"
    android:icon="@drawable/icon1"
    android:enabled="false"
    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=".MainActivityAlias2"
    android:icon="@drawable/icon2"
    android:enabled="false"
    android:targetActivity=".MainActivity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity-alias>

Then, in code, use the PackageManager to enable and disable the aliases:

private void enableAlias(String aliasName) {
    PackageManager pm = getPackageManager();
    ComponentName component = new ComponentName(this, aliasName);
    pm.setComponentEnabledSetting(component, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
    // Disable other aliases to ensure only one is active
    // Example: disable all others
}

private void disableAlias(String aliasName) {
    PackageManager pm = getPackageManager();
    ComponentName component = new ComponentName(this, aliasName);
    pm.setComponentEnabledSetting(component, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
}

This method changes the icon by switching which alias is enabled, but it requires that at least one alias is always enabled.

Comparison and Limitations

Both methods have pros and cons. The shortcuts method is more flexible and can work with multiple shortcuts, but it relies on launcher support and may show unwanted toasts. The activity alias method is more integrated but requires manifest changes and careful management of enabled states.

It's important to note that neither method is officially supported by Android, and results may vary across devices and launchers. Testing on target devices is recommended.

Conclusion

Programmatically changing the Android app icon is achievable through workarounds like using shortcuts or activity aliases. Developers should choose the method based on their specific needs and be aware of the limitations. Always test thoroughly to ensure compatibility.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.