Created
April 28, 2019 21:13
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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