Skip to content

Instantly share code, notes, and snippets.

@JakeSteam
Created April 28, 2019 21:13
Show Gist options
  • Save JakeSteam/5efffeee23097d8141a69e3c74649a2f to your computer and use it in GitHub Desktop.
Save JakeSteam/5efffeee23097d8141a69e3c74649a2f to your computer and use it in GitHub Desktop.
"How to programmatically change your Android app icon and name" for http://blog.jakelee.co.uk/programmatically-changing-app-icon
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/red"
android:text="Red" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/green"
android:text="Green" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/blue"
android:text="Blue" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="uk.co.jakelee.dynamiciconchanging">
<application
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity-alias
android:label="Red app"
android:icon="@mipmap/icon_red"
android:name=".RED"
android:enabled="true"
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:label="Green app"
android:icon="@mipmap/icon_green"
android:name=".GREEN"
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:label="Blue app"
android:icon="@mipmap/icon_blue"
android:name=".BLUE"
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>
</application>
</manifest>
class MainActivity : AppCompatActivity() {
enum class ICON_COLOUR { RED, GREEN, BLUE }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
red.setOnClickListener { setIcon(ICON_COLOUR.RED) }
green.setOnClickListener { setIcon(ICON_COLOUR.GREEN) }
blue.setOnClickListener { setIcon(ICON_COLOUR.BLUE) }
}
private fun setIcon(targetColour: ICON_COLOUR) {
for (value in ICON_COLOUR.values()) {
val action = if (value == targetColour) {
PackageManager.COMPONENT_ENABLED_STATE_ENABLED
} else {
PackageManager.COMPONENT_ENABLED_STATE_DISABLED
}
packageManager.setComponentEnabledSetting(
ComponentName(BuildConfig.APPLICATION_ID, "${BuildConfig.APPLICATION_ID}.${value.name}"),
action, PackageManager.DONT_KILL_APP
)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment