Blog Infos
Author
Published
Topics
, , , ,
Published

Hey there, Android developer! Have you ever wanted to jazz up your app by changing its icon on the fly? Maybe switch to a spooky Halloween icon in October, or a festive look during the holidays? Well, you’re in luck! This guide will show you how to change your app icon programmatically, using everyday analogies and a dash of fun.

Step 1: Setting the Stage

Think of your app as a play, and the icon is the star performer. Sometimes, you want the star to change costumes to fit different scenes. To do this, we need to prepare multiple costumes (icons) and make sure our star knows how to switch between them.

First, let’s define these costumes in our AndroidManifest.xml. This is like listing all possible costumes in the playbook.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.changeicon">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <!-- Costume 1: Halloween -->
        <activity-alias
            android:name=".MainActivityHalloween"
            android:enabled="false"
            android:icon="@mipmap/ic_halloween"
            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>

        <!-- Costume 2: Holidays -->
        <activity-alias
            android:name=".MainActivityHoliday"
            android:enabled="false"
            android:icon="@mipmap/ic_holiday"
            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>

    </application>

</manifest>

Job Offers

Job Offers

There are currently no vacancies.

OUR VIDEO RECOMMENDATION

,

Meta-programming with K2 compiler plugins

Let’s see what’s possible with plugins using the new K2 compiler, FIR. This live demo session will go through possible use cases that reduce boilerplate code and make your code safer.
Watch Video

Meta-programming with K2 compiler plugins

Tadeas Kriz
Senior Kotlin Developer
Touchlab

Meta-programming with K2 compiler plugins

Tadeas Kriz
Senior Kotlin Develo ...
Touchlab

Meta-programming with K2 compiler plugins

Tadeas Kriz
Senior Kotlin Developer
Touchlab

Jobs

Step 2: Switching Costumes

Now, let’s imagine our app is backstage, ready to switch costumes. We need a stage manager (in this case, our code) to tell our star performer to change outfits.

In your MainActivity, we’ll add some code to handle the costume change.

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Imagine you have a button for each costume
        findViewById(R.id.button_halloween).setOnClickListener(v -> changeIcon(1));
        findViewById(R.id.button_holiday).setOnClickListener(v -> changeIcon(2));
    }

    private void changeIcon(int iconType) {
        PackageManager pm = getPackageManager();

        // The default costume (icon) is disabled first
        pm.setComponentEnabledSetting(
                new ComponentName("com.example.changeicon", "com.example.changeicon.MainActivity"),
                PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                PackageManager.DONT_KILL_APP);

        // Then we enable the desired costume
        switch (iconType) {
            case 1:
                pm.setComponentEnabledSetting(
                        new ComponentName("com.example.changeicon", "com.example.changeicon.MainActivityHalloween"),
                        PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                        PackageManager.DONT_KILL_APP);
                break;
            case 2:
                pm.setComponentEnabledSetting(
                        new ComponentName("com.example.changeicon", "com.example.changeicon.MainActivityHoliday"),
                        PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                        PackageManager.DONT_KILL_APP);
                break;
            default:
                pm.setComponentEnabledSetting(
                        new ComponentName("com.example.changeicon", "com.example.changeicon.MainActivity"),
                        PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                        PackageManager.DONT_KILL_APP);
                break;
        }
    }
}
Step 3: Rehearse and Test

It’s rehearsal time! Rebuild your project and run it on your device. Press the buttons to see your app icon magically change. If everything works, give yourself a round of applause!

When to Change the App Icon

Just like in a play, timing is everything. Here are some great moments to switch your app’s icon:

  1. Seasonal Themes: Make your app festive with icons for different seasons or holidays.
  2. User Achievements: Reward users by changing the icon when they reach milestones.
  3. Special Events: Promote special events or updates with a temporary icon change.
  4. User Customization: Let users choose their favorite icon from a set.
Conclusion

And there you have it! Changing your app icon programmatically is like giving your app a costume closet full of fun outfits. By following these steps and using our code examples, you can make your app more dynamic and engaging. So go ahead, and let your app shine in its many fabulous costumes!

This article is previously published on proandroiddev.com

YOU MAY BE INTERESTED IN

YOU MAY BE INTERESTED IN

blog
Using annotations in Kotlin has some nuances that are useful to know
READ MORE
blog
One of the latest trends in UI design is blurring the background content behind the foreground elements. This creates a sense of depth, transparency, and focus,…
READ MORE
blog
Now that Android Studio Iguana is out and stable, I wanted to write about…
READ MORE
blog
The suspension capability is the most essential feature upon which all other Kotlin Coroutines…
READ MORE
Menu