4 min read

Dynamic App Icons on Android using activity-alias

Ever wonder how apps like Discord or Twitter let users change their app icon? It’s not magic, it’s an Android trick using activity-alias in the manifest file! By toggling between the main activity and an alias with different icons, you can dynamically switch the app icon during runtime.
Dynamic App Icons on Android using activity-alias
Example of Discord Nitro, Reddit Premium, and Twitter Blue paid features to change app icons on Android

Introduction

Have you ever seen an app that can change its own icon? Some apps do this, especially those with special paid features. For example, Discord has Discord Nitro, Twitter has Twitter Blue, and Reddit has Reddit Premium. These let users pick different icons for the app on their home screen. When I first saw this, I thought, "What kind of magic is this?" We all know that app icons come from the manifest file in Android. So, do they change the manifest while the app is running? Is that even possible? The answer is no, they do not change the manifest. Instead, they use something called activity-alias to make this happen. It is a smart trick in Android development. Let me explain it step by step, starting with how app icons are usually set.

In Android, the icon for an app is set in the AndroidManifest.xml file. This file is like the blueprint for your app. It tells the system what the app looks like and how it starts. Normally, you use attributes like android:icon and android:roundIcon to point to the image files for the icon. These are set when you build the app, and they do not change after installation. But for apps that want to let users switch icons, developers add extra parts to the manifest. This is where activity-alias comes in. It acts like a shortcut or a fake entry that points to the real activity but with a different icon.

This feature is useful for making apps more fun and personal. Users like it because they can customize their phone without installing new apps. Developers like it because it can encourage people to buy premium features. But it needs careful setup to work right. If done wrong, it might confuse the system or not change the icon properly. In the next sections, I will show you how to do it with code examples and tips.

How App Icons Are Usually Set

Let us start with the basic way to set an app icon. In the AndroidManifest.xml, you have the application tag. Inside it, you put android:icon and android:roundIcon to choose the images. Then, there is the activity tag for the main screen. This activity has an intent-filter that says it is the launcher, meaning it starts when you tap the icon.

Here is a simple example of that:

This setup is what most apps use. When you install the app, the icon from those attributes shows up on the home screen. It stays the same forever unless you update the whole app. But what if you want to change it without a new version? That is when activity-alias helps. It lets you have multiple launcher entries that point to the same activity, but with different icons.

To make it dynamic, you add an activity-alias tag in the manifest. This alias has its own icon and roundIcon, but it targets the main activity. You set the main activity to enabled=true at first, and the alias to enabled=false. Then, in the app code, you can switch them on and off.

Here is how that looks in the manifest:

In this code, both the main activity and the alias point to the same YourActivity class. That means the app behaves the same way, no matter which icon is active. When the app installs, the main icon shows because the alias is disabled. To change it, you use code to enable the alias and disable the main one.

How to Change the Icon at Runtime

Now, let us talk about how to actually switch the icons while the app is running. You use the PackageManager class in your code. This class helps manage parts of the app, like enabling or disabling components. The key method is setComponentEnabledSetting. You call it twice: once to enable the alias and once to disable the main activity.

Here is a function in Kotlin that does this:

This code enables the alias and disables the main activity. The DONT_KILL_APP flag is important because it stops the system from restarting the app right away. After this, the icon on the home screen changes to the one from the alias. But remember, the change might not show instantly on all devices. Sometimes, users need to restart their launcher or wait a bit.

You can make this more advanced by having multiple aliases. For example, if you have several icon options, add more activity-alias tags in the manifest, each with a different icon. Then, in your code, enable the one the user picks and disable the others. This is how apps like Discord let users choose from many icons. Just make sure all aliases target the same main activity.

One thing to watch out for is permissions. On some Android versions, you might need to handle runtime permissions or confirm the change. Also, test on different devices because launchers can behave differently. For instance, on Samsung phones, the icon might update slower than on Google Pixel.

Why Use This Feature and Tips

Using dynamic icons can make your app stand out. It is a nice perk for premium users, like in Twitter Blue where they get special icons. It makes people feel like they get something extra for their money. Plus, it is not hard to add once you understand activity-alias.

But there are some downsides. If you have too many aliases, your manifest file gets big, and the app size increases a little because of extra icons. Also, if the user uninstalls and reinstalls, it goes back to the default.

Here are some tips to do it well: First, always include the default icon as the main activity. Second, name your aliases clearly, like .PremiumIcon1, .PremiumIcon2. Third, add a way in your app UI for users to pick and apply the icon. Maybe show a preview of each icon. Fourth, handle the case where the change does not work on old Android versions. Activity-alias works from Android 1.0, but dynamic enabling is best on Android 5.0 and up.

It feels like a small but cool customization. If you are building an app with in-app purchases, think about adding this. It can boost engagement.

Conclusion

In the end, changing app icons dynamically is not magic, but it feels like it to users. By using activity-alias in the manifest and PackageManager in code, you can make it happen easily. It is a great way to add value to your app.

We are all learning Android development together. So, share this post with your developer friends. Talk about it in your groups. Let us make this feature more common in apps. I am excited to see more apps using this trick! 🚀