Change Launcher icon dynamically in Android! What is it mean? Well, let’s discuss it. In the mobile application, we can say the launcher icon is the unique identification of that app. The launcher icon is self-sufficient to define the type of application. Normally a single app consists of a single launcher icon. But how we will manage, when we have to change or update the launcher icon on runtime according to different scenarios. Or when we try to change the launcher icon dynamically.
In this blog, we will learn how we can change or update the launcher icon dynamically or on runtime. For this, we will follow the mentioned steps:
In this step, we will create an empty class for each launcher icon that we want to change on runtime. This is class is an empty class, which consists of nothing in its body. I have created two empty classes for the two launcher icon, you can create any number of them as per the requirement.
For the first launcher icon:
1 2 3 |
class FirstLauncherAlias { } |
And for the second launcher icon:
1 2 3 |
class SecondLauncherIcon { } |
Use Activity alias in the manifest file
Activity alias represents the symbolic link (Symlink) to an activity. It is the most unused tag in the android manifest.
1 2 3 4 5 6 7 8 9 10 11 |
<activity-alias android:name="FirstLauncherAlias" android:enabled="true" android:icon="@mipmap/ic_launcher1" android:label="@string/app_name" android:targetActivity=".activities.SplashScreenActivity"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity-alias> |
1 2 3 4 5 6 7 8 9 10 11 12 |
<activity-alias android:name="SecondLauncherAlias" android:enabled="false" android:icon="@mipmap/ic_launcher1" android:label="@string/app_name" android:targetActivity=".activities.SplashScreenActivity"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity-alias> |
Note:
- If you are using any deep links inside the app, then please put them in all the activity alias.
- Only one activity alias should be enabled initially, otherwise, it will create multiple launcher icon at the time time of app launching.
- Should enable the default activity alias which we want to show on the first time of app launching.
- If you want a different name with each launcher icon then you can also change the label name in each activity alias.
Now, the final steps (Enable and disable the activity alias for the respective launcher icons)
After all the steps, we can now enable or disable the activity alias according to your requirement. In this demo, we are performing on the splash screen launching, with the help of shared pref. You can do it on this button click event or any other event.
At a time one activity alias should be enabled and all others should be disabled. For enabling the first launcher we do the following code:
1 2 3 |
packageManager.setComponentEnabledSetting(ComponentName(this@SplashScreenActivity, FirstLauncherAlias::class.java), PackageManager.COMPONENT_ENABLED_STATE_ENABLED,PackageManager.DONT_KILL_APP) packageManager.setComponentEnabledSetting(ComponentName(this@SplashScreenActivity, SecondLauncherAlias::class.java), PackageManager.COMPONENT_ENABLED_STATE_DISABLED,PackageManager.DONT_KILL_APP) |
For enabling a second launcher icon, we do the following code.
1 2 3 |
packageManager.setComponentEnabledSetting(ComponentName(this@SplashScreenActivity, SecondLauncherIcon::class.java), PackageManager.COMPONENT_ENABLED_STATE_ENABLED,PackageManager.DONT_KILL_APP) packageManager.setComponentEnabledSetting(ComponentName(this@SplashScreenActivity, FirstLauncherAlias::class.java), PackageManager.COMPONENT_ENABLED_STATE_DISABLED,PackageManager.DONT_KILL_APP) |