varunkr
varunkr

Reputation: 5542

getContext() method is undefined for the type MainActivity when get apps icon

I'm trying to get an icon of the installed app. Using the following way, I'm getting the error:

The method getContext() is undefined for the type MainActivity

try{
   String pkg = "com.app.my";//your package name
   Drawable icon = getContext().getPackageManager().getApplicationIcon(pkg);
   imageView.setImageDrawable(icon);
}
catch (PackageManager.NameNotFoundException ne) {

}

It would be very helpful if someone could guide me. Thanks in advance!!

Upvotes: 1

Views: 1408

Answers (2)

Koen Demonie
Koen Demonie

Reputation: 539

Activity class extends Context .So just call getContext(); This will make use of the activities layout defined context.

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1006869

Delete getContext(). Activity extends Context. Just call getPackageManager().getApplicationIcon(pkg).

Upvotes: 2

Related Questions