Create Application Launcher as a list
This is how Google Android default Application screen looks like:
It’s a grid view and when you click on one of those, the corresponding activity is launched.
Ok, so now I’ll guide you how to create a similar one but in a list view. Here the screenshot:
A – What Does It Do?
- All the installed application on the machine is displayed with their icons, label names and package names.
- When an item is clicked the corresponding activity is launched.
- If the item does not exist, a small message notification will display. (it might not happen, lol)
- If the item exist, but current user does not have the permission to launch, well, just do nothing. (it’s kinda fun on slow emulator
)
B – Create the Project
Project Name: Application Viewer
Build Target: Android 2.3.3 (the latest one at this time, well, I like it though)
Application Name: Application Viewer
Package Name: pete.android.study
Create Activity: MainActivity
Min SDK: 10
C – Sketch the Layout
Like old times (my previous post), we need two layouts:
+ one layout for each item in the list view
+ one layout for main list view display
1. List Item Layout (/res/layout/layout_appinfo.xml)
- You may want to refer to my previous tutorial on creating a PhoneBook, because the layout is much alike. I’m feeling lazy to describe it again…zzz
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="5dip"
>
<ImageView
android:id="@+id/ivIcon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="5dip"
android:scaleType="center"
/>
<LinearLayout
android:orientation="vertical"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
>
<TextView
android:id="@+id/tvName"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:gravity="center_vertical"
/>
<TextView
android:id="@+id/tvPack"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:singleLine="true"
android:ellipsize="marquee"
/>
</LinearLayout>
</LinearLayout>
2. Main Layout (/res/layout/layout_main.xml)
- Just one single list view to apply.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:id="@+id/lvApps"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
D – Class Design: On the Idea
This is a quite complicated shit of class diagram. I’m just thinking of making it more details.
MainActivity: the entry of the program, we will load list view here, create and set adapter to list view, and handle OnItemClickListener (which is from AdapterView) in list view.
AppInfoAdapter: the adapter inherits from BaseAdapter to handle list item, we need to handle things in getView() (loading controls from resources, set them appropriate values, get application info meta data..).
Utilities: to support
+ get installed application list.
+ launch an activity.
E – From Design to Coding (w/ Passion)
1. AppInfoAdapter: (AppInfoAdapter.java)
package pete.android.study;
import java.util.List;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class AppInfoAdapter extends BaseAdapter {
private Context mContext;
private List mListAppInfo;
private PackageManager mPackManager;
public AppInfoAdapter(Context c, List list, PackageManager pm) {
mContext = c;
mListAppInfo = list;
mPackManager = pm;
}
@Override
public int getCount() {
return mListAppInfo.size();
}
@Override
public Object getItem(int position) {
return mListAppInfo.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// get the selected entry
ApplicationInfo entry = mListAppInfo.get(position);
// reference to convertView
View v = convertView;
// inflate new layout if null
if(v == null) {
LayoutInflater inflater = LayoutInflater.from(mContext);
v = inflater.inflate(R.layout.layout_appinfo, null);
}
// load controls from layout resources
ImageView ivAppIcon = (ImageView)v.findViewById(R.id.ivIcon);
TextView tvAppName = (TextView)v.findViewById(R.id.tvName);
TextView tvPkgName = (TextView)v.findViewById(R.id.tvPack);
// set data to display
ivAppIcon.setImageDrawable(entry.loadIcon(mPackManager));
tvAppName.setText(entry.loadLabel(mPackManager));
tvPkgName.setText(entry.packageName);
// return view
return v;
}
}
2. Utilities: (Utilities.java)
package pete.android.study;
import java.util.List;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.widget.Toast;
public class Utilities {
/*
* Get all installed application on mobile and return a list
* @param c Context of application
* @return list of installed applications
*/
public static List getInstalledApplication(Context c) {
return c.getPackageManager().getInstalledApplications(PackageManager.GET_META_DATA);
}
/*
* Launch an application
* @param c Context of application
* @param pm the related package manager of the context
* @param pkgName Name of the package to run
*/
public static boolean launchApp(Context c, PackageManager pm, String pkgName) {
// query the intent for lauching
Intent intent = pm.getLaunchIntentForPackage(pkgName);
// if intent is available
if(intent != null) {
try {
// launch application
c.startActivity(intent);
// if succeed
return true;
// if fail
} catch(ActivityNotFoundException ex) {
// quick message notification
Toast toast = Toast.makeText(c, "Application Not Found", Toast.LENGTH_LONG);
// display message
toast.show();
}
}
// by default, fail to launch
return false;
}
}
3. MainActivity: (MainActivity.java)
package pete.android.study;
import android.app.Activity;
import android.content.pm.ApplicationInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
public class MainActivity extends Activity {
private ListView mListAppInfo;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set layout for the main screen
setContentView(R.layout.layout_main);
// load list application
mListAppInfo = (ListView)findViewById(R.id.lvApps);
// create new adapter
AppInfoAdapter adapter = new AppInfoAdapter(this, Utilities.getInstalledApplication(this), getPackageManager());
// set adapter to list view
mListAppInfo.setAdapter(adapter);
// implement event when an item on list view is selected
mListAppInfo.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView parent, View view, int pos, long id) {
// get the list adapter
AppInfoAdapter appInfoAdapter = (AppInfoAdapter)parent.getAdapter();
// get selected item on the list
ApplicationInfo appInfo = (ApplicationInfo)appInfoAdapter.getItem(pos);
// launch the selected application
Utilities.launchApp(parent.getContext(), getPackageManager(), appInfo.packageName);
}
});
}
}
F – Note
- Using the tip of retrieving the list of installed application.
- Using the tip of launching an activity from another.
- Performance is not so good
, of course. You can think of better solution than mine!
- The code is toooo simple, I guess!
G – Get the Sample Project by Pete
Pick your favorite file hosting server: Mediafire | Rapidshare | Megaupload
H – Final Words
- Have you learned something?
- Is it easy to understand?
- Make sure you practice day by day with me
, it’s fun, isn’t it?
- Hope you enjoy it!
Cheers,
Pete Houston
Leave a Reply Cancel reply
Categories
- Home (4)
- Learning (149)
- Books & Materials (2)
- Design Patterns (2)
- PhoneGap (1)
- Tricks & Tips (75)
- Tutorials (69)
- Of Diary (20)
- Pete's Works (8)
- Applications (7)
- Publications (1)
- Tools (7)
Recent Posts
Most Popular
Tag Cloud
Archives
- May 2013 (1)
- January 2013 (5)
- December 2012 (4)
- October 2012 (2)
- September 2012 (1)
- August 2012 (2)
- July 2012 (12)
- June 2012 (1)
- May 2012 (7)
- March 2012 (1)
- February 2012 (3)
- January 2012 (2)
- December 2011 (8)
- November 2011 (12)
- October 2011 (19)
- September 2011 (16)
- July 2011 (29)
- June 2011 (63)
Statistics
- 549,853 View



Thank you so much. It strongly helped me…
What about running apps list??
Can i get running apps list by editing AppInfoAdapter?
Can this list be sorted alphabetically by Application Name?
Yes, you do it by Collections.sort()
Sorry, where do you apply Collections.sort? I tried that on the List and the PackageManager and just found errors.
Nevermind, I forgot Collections.sort() allows Comparators as a parameter
Howdy I am so grateful I found your weblog, I really found you by accident, while I was looking on
Aol for something else, Anyhow I am here now and would
just like to say thanks for a incredible post and a all round
enjoyable blog (I also love the theme/design), I don’t have time to look over it all at the moment but I have book-marked it and also included your RSS feeds, so when I have time I will be back to read a great deal more, Please do keep up the great work.
Great! Now I want to get an app like this in the app store…
…
thank u so much for nice example can i change icons ???
Is it possible to integrate this application launcher with a list of displayed permissions from http://www.xinotes.org/notes/note/1153/ ? I tried a couple of methods but it is not working. I have no idea how to get the package name when trying to do a onclicklistener
Nevermind fixed it!
you saved my day
Hi..Thanks for the excellent tutorial
Hi, could you tell me how to show installed application (exclude systems app.) in GridView?
I keep getting an error around “”pm.getLaunchIntentForPackage(pkgName);”" in the Utilities.java file
Any help?
Hi,
thx for your work. I like that
Could you tell me what must be changed if I don’t want a list of all installed Apps, just one ore two chosen apps?
What I’m also interrested about is a query of a password. So that I can just enter the app from the launcher if I imput the correct password. If not it will do nothing. Any hint?
Thx
You’re always welcome buddy ^^! It’s great to know that my articles are helpful for you
Hello! Alfox here

I have another problem
I have create a list, now I want add a OnItemClick
this is my code:
CALL INSIDE CLASS
mListApp.setOnItemClickListener(mItemClickListener);
DECLARATION
private OnItemClickListener mItemClickListener = new OnItemClickListener() {
public void onItemClick(AdapterView parent, View view, int pos, long id) {
ViewHolderApp holder = (ViewHolderApp) view.getTag();
if(holder == null) {
Toast.makeText(Appelli.this, “Error”, Toast.LENGTH_SHORT).show();
return;
}
Toast.makeText(Appelli.this, “You have clicked on item ‘” + holder.app_desc.getText() + “‘”, Toast.LENGTH_SHORT).show();
}
};
ViewHolderApp is a method inside a service class “AppelliUtilsAdapter” (it works like AppInfoAdapter of your Grid example)
“Appelli” is the main class, like your MainActivity always in Grid example
I receive always the Toast with “Error”, so the “holder” is null, why? I am sure mListApp is not null
Thanks
EDIT: Solved with your example above
this is the code:
private OnItemClickListener mItemClickListener = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView parent, View view, int pos, long id) {
AppelliUtilsAdapter aua = (AppelliUtilsAdapter)parent.getAdapter();
final AppelliUtils appUtils = (AppelliUtils)aua.getItem(pos);
AlertDialog alertDialog = new AlertDialog.Builder(Appelli.this).create();
alertDialog.setTitle(“Prenotazione Esame”);
alertDialog.setMessage(“Vuoi prenotarti all’esame di\n”+appUtils.getDesc());
alertDialog.setButton(“Prenota”, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Appelli.this, “Esame Prenotato”, Toast.LENGTH_SHORT).show();
} });
alertDialog.setButton2(“Annulla”, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Appelli.this, “Annullato”, Toast.LENGTH_SHORT).show();
} });
alertDialog.show();
}
};
There is also an AlertDialog before
Thanks for all your tutorial, very helpful for my university exam!