How can I get the default app icon's drawable id in android

0 votes
asked Jul 14, 2014 by fisher

In my situation, I know some methods below

getApplicationContext().getResources().getIdentifier("nameOfDrawable", "drawable", packageName);
getApplicationContext().getPackageManager().getApplicationIcon(packageName);

I want to get app icon's drawable id, but I do not know the "nameOfDrawable" of the app icon. I say I don't know because the default app icon's name can be "ic_launcher.png" or be changed to other like "icon.png". I find the second method above can get the drawable of app icon, but then I don't know what to do. Does anyone know how to get app icon's drawable id? Please help, thanks!

In fact, I want to show a notification, but it need to use the icon's drawable id. I find the first method above can get app icon's drawable id, but it need the name of app icon file which can be different in different app. What I'm doing is to make a little library to do something, so something can not be confirmed.

2 Answers

+1 vote
answered Jul 14, 2014 by shivam-verma

This is how you can retrieve and use the Drawable :

Drawable icon = getPackageManager().getApplicationIcon("com.example.samplepackagename");
imageView.setImageDrawable(icon);

The id of any resource is local to that particular application.

0 votes
answered Jul 14, 2014 by fisher

Finally, I find out the way to solve my problem. The code is below.

String packageName = context.getApplicationContext().getPackageName();
        Intent launchIntent = context.getApplicationContext().getPackageManager().getLaunchIntentForPackage(packageName);
        String className = launchIntent.getComponent().getClassName();

Class<?> c = null;
        if(className != null) {
            try {
                c = Class.forName(className);
            } catch (ClassNotFoundException e) {
                Log.i(TAG, "Throw exception when try to get the main class.");
                e.printStackTrace();
            }
        }

ApplicationInfo info = null;
        try {
            info = context.getApplicationContext().getPackageManager().getApplicationInfo(packageName, 0);
        } catch (NameNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        int icon = info.icon;

long when = System.currentTimeMillis();
        Notification notification = new Notification(icon, null, when);
        notification.flags = Notification.FLAG_AUTO_CANCEL;
        notification.defaults = notification.DEFAULT_SOUND|notification.DEFAULT_VIBRATE;
        Intent openintent= new Intent(context, c);
        PendingIntent contentIntent = PendingIntent.getActivity(context.getApplicationContext(), 0, openintent, 0);
        notification.setLatestEventInfo(context.getApplicationContext(), "Notification", message, contentIntent);

        NotificationManager mNotificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(NOTIFICATION_COUNT, notification);
Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...