Dynamic Android App Icons — How can we change the app icon programmatically?
How do we change the application icon or application name before a new update comes out in an android application? How do banks that blow a cake candle on the application icon on my birthday do it? It is actually not as big as changing the tire of a vehicle on the road. Now let’s take a look together how we change this tire :) .
1- Preparing assests
First, we start by adding the images we will use as application icons to the application. I want to make an app icon that changes according to our mood at that moment, for this we use the New>Image Asset menu. Android studio sizes this image in all necessary formats for us.
2- Edit AndroidManifest.xml file
Actually, the icon replacement logic here works as follows; We have multiple application shortcuts with different icons, and we keep the ones we don’t use closed and make the ones we need visible.
To prepare these shortcuts, we create an <activity-alias> for each icon in the Manifest file. In this section, if we want to change the application name, you can add your new application name in the label section.
<activity-alias
android:name=".MainActivitySmile"
android:enabled="false"
android:exported="true"
android:icon="@mipmap/ic_launcher_smile"
android:roundIcon="@mipmap/ic_launcher_smile_round"
android:label="@string/app_name"
android:targetActivity=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity-alias>
3- Application Icons List
We are creating a data class to easily match the icons and activity names we have prepared for the application and then access these icons from the front face. I set an id for each icon so that it is easier to distinguish later.
data class AppIcon(
val id : String,
val component: String,
@DrawableRes
val foregroundResource: Int
)
val appIcons : List<AppIcon> = listOf(
AppIcon(
id = "default",
component = "com.yamanf.iconchanger.MainActivity",
foregroundResource = R.mipmap.ic_launcher_default
),
AppIcon(
id = "smile",
component = "com.yamanf.iconchanger.MainActivitySmile",
foregroundResource = R.mipmap.ic_launcher_smile
)
)
4- Interface Preparation
This is a bit specific to this article, we give the user the freedom to choose the icon they want. If you wish, you can do this automatically with a query from the backend.
5- Changing the icon
Now we come to the piece of code that caused me to write this article. This function will search for the id you provide in the icon list we created and activate the corresponding application shortcut.
fun setAppIcon(iconId: String) {
val icon = appIcons.find { it.id == iconId } ?: return
val context = baseContext
val packageManager = context.packageManager
appIcons.filterNot { it.id == iconId }.forEach {
packageManager.setComponentEnabledSetting(
ComponentName(context, it.component),
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP
)
}
packageManager.setComponentEnabledSetting(
ComponentName(context, icon.component),
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP
)
}
The project we have created for this article is available below, please do not hesitate to contact us for advice and suggestions.
Follow me on social 🤓
website : furkanyaman.com
twitter : @yamanf_dev
instagram : @yamanf.dev