Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to Change App Icon of Android Programmatically in Android?

  • Last Updated : 30 Jun, 2021

In this article, we are going to learn how to change the App Icon of an App on the Button Click. This feature can be used when we have an app for different types of users. Then as per the user type, we can change the App Icon Dynamically. 

Step by Step Implementation

Step 1: Create a New Project

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.

Step 2: Add this in AndroidManifest.xml File



XML




<?xml version="1.0" encoding="UTF-8"?>
  
<manifest package="com.prepare.packagename" 
          xmlns:android="http://schemas.android.com/apk/res/android">
  
<application 
  android:theme="@style/Theme.packagename"
  android:supportsRtl="true" 
  android:roundIcon="@mipmap/ic_launcher_round"
  android:label="@string/app_name" 
  android:icon="@mipmap/ic_launcher_old"
  android:allowBackup="true">
  
<activity android:name=".MainActivity">
  
    <intent-filter>
  
        <action android:name="android.intent.action.MAIN"/>
        <category android:name="android.intent.category.LAUNCHER"/>
  
    </intent-filter>
  
</activity>
  
<activity-alias 
  android:name=".MainActivityAlias" 
  android:roundIcon="@drawable/ic_launcher_p_foreground"
  android:label="@string/app_name"
  android:icon="@mipmap/ic_launcher"
  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>

Step 3: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.  

XML




<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_gravity="center"
    android:gravity="center"
    tools:context=".MainActivity">
  
    <TextView
        android:id="@+id/newicon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=" New Icon"
        android:textColor="@color/black"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    
    <TextView
        android:id="@+id/oldicon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=" Old Icon"
        android:textColor="@color/black"
        android:textSize="20sp"
        android:layout_marginTop="40dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
  
</LinearLayout>

Step 4: Working with the MainActivity.java file

Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.

Java




import android.content.ComponentName;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.BackgroundColorSpan;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
  
import androidx.appcompat.app.AppCompatActivity;
  
public class MainActivity extends AppCompatActivity {
  
    TextView click,newicon;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        click=findViewById(R.id.oldicon);
        newicon=findViewById(R.id.newicon);
        click.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                changeicon();
            }
        });
        newicon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                newicon();
            }
        });
  
    }
    private void changeicon() {
          
          // enable old icon
        PackageManager manager=getPackageManager();
        manager.setComponentEnabledSetting(new ComponentName(MainActivity.this,"com.prepare.makedirectory.MainActivity")
                ,PackageManager.COMPONENT_ENABLED_STATE_ENABLED,PackageManager.DONT_KILL_APP);
          
          // disable new icon
        manager.setComponentEnabledSetting(new ComponentName(MainActivity.this,"com.prepare.makedirectory.MainActivityAlias")
                ,PackageManager.COMPONENT_ENABLED_STATE_DISABLED,PackageManager.DONT_KILL_APP);
        Toast.makeText(MainActivity.this,"Enable Old Icon" ,Toast.LENGTH_LONG).show();
    }
  
    private void newicon() {
          
          // enable old icon
        PackageManager manager=getPackageManager();
        manager.setComponentEnabledSetting(new ComponentName(MainActivity.this,"com.prepare.makedirectory.MainActivity")
                ,PackageManager.COMPONENT_ENABLED_STATE_DISABLED,PackageManager.DONT_KILL_APP);
          
          // enable new icon
        manager.setComponentEnabledSetting(new ComponentName(MainActivity.this,"com.prepare.makedirectory.MainActivityAlias")
                ,PackageManager.COMPONENT_ENABLED_STATE_ENABLED,PackageManager.DONT_KILL_APP);
        Toast.makeText(MainActivity.this,"Enable New Icon" ,Toast.LENGTH_LONG).show();
    }
}

 Output:




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!