How to change an application icon programmatically in Android?

前端 未结 10 2137
闹比i
闹比i 2020-11-21 22:33

Is it possible to change an application icon directly from the program?
I mean, change icon.png in the res\\drawable folder.
I would like t

10条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-21 23:19

    Applying the suggestions mentioned, I've faced the issue of app getting killed whenever default icon gets changed to new icon. So have implemented the code with some tweaks. Step 1). In file AndroidManifest.xml, create for default activity with android:enabled="true" & other alias with android:enabled="false". Your will not contain but append those in with android:enabled="true".

           
    
        
        
        
            android:enabled="true"
            android:targetActivity=".activities.SplashActivity"> 
            
                
    
                
                
            
        
        
        
            android:enabled="false"
            android:targetActivity=".activities.SplashActivity"> 
            
                
    
                
                
            
        
    

    Step 2). Make a method that will be used to disable 1st activity-alias that contains default icon & enable 2nd alias that contains icon need to be changed.

    /**
     * method to change the app icon dynamically
     *
     * @param context
     * @param isNewIcon  : true if new icon need to be set; false to set default 
     * icon
     */
    
    public static void changeAppIconDynamically(Context context, boolean isNewIcon) {
        PackageManager pm = context.getApplicationContext().getPackageManager();
        if (isNewIcon) {
            pm.setComponentEnabledSetting(
                    new ComponentName(context,
                            "com.example.dummy.SplashActivityAlias1"), //com.example.dummy will be your package
                    PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                    PackageManager.DONT_KILL_APP);
    
            pm.setComponentEnabledSetting(
                    new ComponentName(context,
                            "com.example.dummy.SplashActivityAlias"),
                    PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                    PackageManager.DONT_KILL_APP);
        } else {
            pm.setComponentEnabledSetting(
                    new ComponentName(context,
                            "com.example.dummy.SplashActivityAlias1"),
                    PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                    PackageManager.DONT_KILL_APP);
    
            pm.setComponentEnabledSetting(
                    new ComponentName(context,
                            "com.example.dummy.SplashActivityAlias"),
                    PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                    PackageManager.DONT_KILL_APP);
        }
    }
    

    Step 3). Now call this method depending on your requirement, say on button click or date specific or occasion specific conditions, simply like -

    // Switch app icon to new icon
        GeneralUtils.changeAppIconDynamically(EditProfileActivity.this, true);
    // Switch app icon to default icon
                GeneralUtils.changeAppIconDynamically(EditProfileActivity.this, false);
    

    Hope this will help those who face the issue of app getting killed on icon change. Happy Coding :)

提交回复
热议问题