7

I have tried below code in order to change application icon dynamically but no use.

Manifest:

<uses-permission android:name="com.android.launcher.action.INSTALL_SHORTCUT"/>

Class:

Intent myLauncherIntent = new Intent();
myLauncherIntent.setClassName("your.package.name", "YourLauncherActivityName");
myLauncherIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, myLauncherIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Application Name");
intent.putExtra
       (
        Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
        Intent.ShortcutIconResource.fromContext
                                    (
                                         getApplicationContext(),
                                         R.drawable.app_icon
                                    )
       );
intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");

getApplicationContext().sendBroadcast(intent);

I need to show counter on app icon whenever any incoming message comes to my app like gmail app. So How can i get app icon & set icon dynamically in android???

Android Developer
  • 987
  • 1
  • 8
  • 22
Yella Goud
  • 157
  • 1
  • 4
  • 14

1 Answers1

4

You cannot modify the app icon dynamically. Gmail does not modify the app icon dynamically.

A home screen -- being the app that actually displays these things -- could elect to apply a badge to certain app icons. And you can create an app widget that looks like an icon with a badge.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • oh but i have requirement that whenever incoming message comes to app it should show some counter on top, right conrner of the message – Yella Goud Jun 17 '13 at 12:36
  • 1
    @user2436481: You are welcome to build an app widget that offers this sort of badged icon. You cannot affect your app icon itself. Moreover, your approach outlined in the answer may not be honored by all home screens (as evidenced by the `com.android.launcher` `Intent` action) and may wind up creating N launcher shortcuts even by those launchers that do honor it. – CommonsWare Jun 17 '13 at 12:46
  • @CommonsWare do you know perhaps how to change the app icons dynamically, assuming your app has root access? – android developer Jan 31 '14 at 00:38
  • @androiddeveloper: Root will be unlikely to help, short of replacing the user's home screen with your own home screen implementation that supports this. There are hundreds, perhaps thousands, of home screen implementations. None are required to even show shortcuts, let alone accept them from third-party apps via this broadcast, let alone offer some ability to change them. – CommonsWare Jan 31 '14 at 00:55
  • 3
    @CommonsWare the app SuperSU allows you to change its icon, so it is possible. i think it might work by changing the icon and broadcast an intent that the app was replaced/updated . also, even if launcher apps store the icons on the memory , they need to get it again on the next time they are started. – android developer Jan 31 '14 at 07:58
  • @CommonsWare are you sure? Because apps like facebook and twitter will add a number on their icons when you have messages. How is that done? It seems like there is some sort of dynamic function for that. – portfoliobuilder Sep 27 '15 at 00:40
  • 1
    @portfoliobuilder: Some home screens offer APIs for it. Few do. There is nothing in Android for it. – CommonsWare Sep 27 '15 at 10:42
  • @CommonsWare I have seen the app SuperSU changing their icon dynamically. So my question is can we do that and be sure that google would not take down the app? I mean is it against their Terms? I want to implement this but can't find any official "yes" or "no" before proceeding. – Prudhvi Mar 09 '16 at 22:52
  • @prudhvi: "is can we do that" -- as I wrote, there's no official support for it. "I mean is it against their Terms?" -- you would have to ask your attorney. Google's terms are very fluid and get applied to a lot of things that I would not have thought of. – CommonsWare Mar 09 '16 at 23:06
  • @CommonsWare - Can we see official statement on this from Google/Android Dev OR any link on Android Dev Pages which states it is not possible - [cannot modify the app icon dynamically] – void pointer Dec 19 '21 at 16:19
  • @voidpointer: I doubt that there is one, as there are an infinite number of things that cannot be done. – CommonsWare Dec 19 '21 at 16:29