Dynamic Modification of Application Icon and Name in AndroidManifest.xml

Posted by biba028 on Sun, 19 May 2019 04:19:28 +0200

I. activity-alias Tags

Android supports dynamic modification of application icons and application names. Apps such as Taobao and Jingdong automatically change icons without updating when they are double 11 and double 12, so the best application scenario for dynamically updating the icons and names of applications is just like that used in promotional activities of e-commerce projects.
Android supports dynamic icon updates because there is an activity-alias tag in Android Manifest. xml, which can be seen from the tag name as an alias for activity. Let's look at the introduction on the official website.
https://developer.android.com/guide/topics/manifest/activity-alias-element.html

An alias for an activity, named by the targetActivity attribute. The target must be in the same application as the alias and it must be declared before the alias in the manifest. 

This is the alias of the property targetActivity: the activity of the property targetActivity: the activity of the property targetActivity and the alias must be under the same application, and the targetActivity must be defined under the alias money.

Let's see what attributes the activity-alias tag has.

    <activity-alias android:enabled=["true" | "false"]
                    android:exported=["true" | "false"]
                    android:icon="drawable resource"
                    android:label="string resource"
                    android:name="string"
                    android:permission="string"
                    android:targetActivity="string" >
        . . .
    </activity-alias>

Many attributes are consistent with the attributes of the activity tag, mainly depending on the targetActivity attribute mentioned in the definition. The value of this attribute is the activity you want to replace with an alias, and the activity must be defined before the activity-alias. Here's a point. If we want to change icons and names dynamically, we have to declare it.`

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter> `This alias for activation is changed in the alias.

II. Dynamic modification of icons and names

1. Set the alias of launch activity:

<activity android:name=".MainActivity"
            android:icon="@mipmap/ic_launcher"
            >

            <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity-alias
            android:name=".Test"
            android:enabled="false"
            android:icon="@mipmap/launch_01"
            android:label="Test application name"
            android:targetActivity=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity-alias>

The alias of main activity is set here. In activity-alias, the icon and label attributes are modified to the icon and label attributes that you want to replace. This name is the alias name, and in activity, the class name of activity. You must not omit the android:targetActivity=".MainActivity" attribute.

2. Operate where needed to be replaced

First, I define a simple button in the layout file, activity_main.xml, to change the icon when clicked.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.pursue.it.iconchange.MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Modify Icon"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />

</android.support.constraint.ConstraintLayout>

In main activity:

package com.pursue.it.iconchange;

import android.content.ComponentName;
import android.content.pm.PackageManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeIcon();
            }
        });
    }


//Method of modifying icons and names
    private void changeIcon() {
        PackageManager pm = getApplicationContext().getPackageManager();
        System.out.println(getComponentName());
       //Remove old icons
        pm.setComponentEnabledSetting(getComponentName(),
                PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                PackageManager.DONT_KILL_APP);
        //Display new icons
        pm.setComponentEnabledSetting(new ComponentName(
                        getBaseContext(),
                        "com.pursue.it.iconchange.Test"),
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                PackageManager.DONT_KILL_APP);
    }
}

Click on the button and wait for a few seconds to see the icon and name automatically changed on the mobile desktop.

Topics: Android Attribute xml encoding