Adarsh Adarsh - 4 months ago 7
Android Question

How to launch application by is app icons which is arranged in listView

I have a

ListView
which contains list of app icons(no texts only images in
ListView
).I am getting the different app icons from a
arrayList
which contains a list of different package names.Here is the code:

for(String a:arr){ //arr is the arrayList which contains list of different apps package names
try {

drawable = getPackageManager().getApplicationIcon(a);
iconL.add(drawable); //iconL is Drawable type arrayList which stores different app icons


}
catch (PackageManager.NameNotFoundException e){
Toast.makeText(Window.this,"error",Toast.LENGTH_SHORT).show();
}
}


After setting the
iconL
with
ListView
adapter I had successfully created a
ListView
of app icons.Now my problem is that I want to Launch the particular apps by clicking on the particular app icons which is available in the
ListView
,
e.g. When I click on the
Whatsapp
app icon which is available in the
listView
it should launch the
Whatsapp
app,and if I click on facebook app icon it should launch facebook app ,etc.Please tell me how can I do that.

Answer

Store package names as String list with icons and launch the app as follows:

String package1 = getApplicationContext().getPackageName();

Intent launchIntent = getPackageManager().getLaunchIntentForPackage(package1);
if (launchIntent != null) { 
    startActivity(launchIntent);//null pointer check in case package name was not found
}