Home > Tutorials > Create Application Launcher as a list

Create Application Launcher as a list


This is how Google Android default Application screen looks like:

Default Android Application Launcher

Default Android Application Launcher

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:

Application Viewer

Application Viewer

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 :D)

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.

Application Viewer - Class Diagram

Application Viewer - Class Diagram

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 :D, it’s fun, isn’t it?

- Hope you enjoy it!

Cheers,

Pete Houston

About these ads
  1. November 21, 2014 at 11:07 am

    Students have been known to spend thousands upon thousands of dollars at schools
    and universities to be able to do it, but that is not necessary.
    The support provided by professionals this way is highly efficient and makes a great choice when it comes to the
    balance between benefits and costs. Have you downloaded any new software on your device between the last time you were able to effectively
    use the broken application and now.

  2. Chitra
    September 17, 2014 at 8:38 pm

    This code is working but on emulator only resides for very few seconds,how i can make it stay for longer time period,but still,”Thanks a lot,u saved my final year submission marks!!!!!!!:)”

  3. August 2, 2014 at 4:34 am

    Hello there! This is my first visit to your blog!
    We are a collection of volunteers and starting a
    new project in a community in the same niche. Your blog provided us useful information to work on. You have done a wonderful job!

  4. July 21, 2014 at 3:08 am

    This site was… how do I say it? Relevant!! Finally I have
    found something which helped me. Thanks!

  5. July 14, 2014 at 10:11 pm

    Unquestionably consider that that you stated.

    Your favorite justification appeared to be on the net the easiest
    thing to take note of. I say to you, I certainly get annoyed whilst folks think about
    worries that they plainly don’t understand about.
    You managed to hit the nail upon the top and also outlined out the whole thing
    with no need side effect , other folks could take a signal.
    Will likely be again to get more. Thanks

  6. Aba
    October 14, 2013 at 4:07 am

    How can I organize the list into alphabetical order using Collections.sort()?
    I understand that it uses a comparator but how would I go about using it?

  7. August 30, 2013 at 9:04 am

    If you notice you are not converting well, make changes to attempt to improve sales.

    Top News lists active news and updates among your friends.
    All it takes is a little common sense and your mind to be made up to get the training.

  8. TheDevMan
    August 21, 2013 at 2:23 pm

    Thanks for nice piece of code. The problem is this shows even system applications like launcher, Network location and more. How do I filter it not to show them?

  9. July 23, 2013 at 2:10 pm

    Hello Pete Houston, Thanks for this great tutorials, I want full source code of any one of this Image Processing tutorial, because I cant get how to process and which library to use please help me, I struggle to use OpenCV library for image processing.

  10. jake
    June 21, 2013 at 10:26 am

    How do you filter out non-launchable apps?

  11. Halil Kaya
    February 23, 2013 at 1:54 am

    Thank you so much. It strongly helped me…

  12. February 13, 2013 at 10:55 am

    What about running apps list??
    Can i get running apps list by editing AppInfoAdapter?

  13. Will
    January 13, 2013 at 6:48 am

    Can this list be sorted alphabetically by Application Name?

    • January 13, 2013 at 9:38 am

      Yes, you do it by Collections.sort()

      • Will
        February 12, 2013 at 7:14 am

        Sorry, where do you apply Collections.sort? I tried that on the List and the PackageManager and just found errors.

      • Will
        February 28, 2013 at 2:35 am

        Nevermind, I forgot Collections.sort() allows Comparators as a parameter

  14. December 1, 2012 at 2:44 am

    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.

  15. subs
    November 24, 2012 at 7:57 pm

    Great! Now I want to get an app like this in the app store… :)

  16. November 22, 2012 at 5:11 pm

    thank u so much for nice example can i change icons ???

  17. Jefferson
    July 23, 2012 at 12:29 am

    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

    • Jefferson
      July 23, 2012 at 3:13 am

      Nevermind fixed it!

  18. gorbi
    June 6, 2012 at 8:04 pm

    you saved my day :-)

  19. April 29, 2012 at 10:09 pm

    Hi..Thanks for the excellent tutorial :)

  20. ElM
    January 19, 2012 at 3:32 pm

    Hi, could you tell me how to show installed application (exclude systems app.) in GridView?

  21. December 7, 2011 at 9:22 am

    I keep getting an error around “”pm.getLaunchIntentForPackage(pkgName);”” in the Utilities.java file
    Any help?

  22. B770
    November 15, 2011 at 8:00 pm

    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 :)

  23. July 16, 2011 at 10:30 pm

    You’re always welcome buddy ^^! It’s great to know that my articles are helpful for you :)

  24. Alfox
    July 16, 2011 at 5:12 pm

    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

    • Alfox
      July 16, 2011 at 5:29 pm

      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!

  1. November 21, 2014 at 4:21 pm
  2. October 31, 2014 at 3:52 am
  3. May 25, 2012 at 2:32 pm
  4. November 26, 2011 at 5:33 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: