How to create a simple App Launcher in Android?

By | March 22, 2013

You have seen Launchers listing your apps. Ever wondered how they get the list of all apps.

https://lh4.googleusercontent.com/-30y4nKNVGI0/UUxx3Nb7kcI/AAAAAAAAANY/I3f-77piaX4/w762-h428-p-o-k/app_launcher.png

This program help you to list all apps installed in your phone and in a gridview and invoke them.

Here we use this class..
PackageManager
Class for retrieving various kinds of information related to the application packages that are currently installed on the device. You can find this class through getPackageManager().

ResolveInfo
Information that is returned from resolving an intent against an IntentFilter. This partially corresponds to information collected from the AndroidManifest.xml’s tags.

First we will create the adapter for the GridView.

MyAdapter.java

package com.coderzheaven.applauncher;

import java.util.List;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class MyAdapter extends BaseAdapter {

	private Context myContext;
	private List<ResolveInfo> MyAppList;
	PackageManager myPackageManager;

	MyAdapter(Context c, List<ResolveInfo> appIntentList) {
		myContext = c;
		MyAppList = appIntentList;
		myPackageManager = c.getPackageManager();
	}

	@Override
	public int getCount() {
		return MyAppList.size();
	}

	@Override
	public Object getItem(int position) {
		return MyAppList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ImageView imageView;
		if (convertView == null) {
			imageView = new ImageView(myContext);
			imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
			imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
			imageView.setPadding(8, 8, 8, 8);
		} else {
			imageView = (ImageView) convertView;
		}

		ResolveInfo resolveInfo = MyAppList.get(position);
		imageView.setImageDrawable(resolveInfo.loadIcon(myPackageManager));

		return imageView;

	}

}

Now the Activity that uses this adapter in the GridView.

package com.coderzheaven.applauncher;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.LinearLayout;

public class AppListActivity extends Activity {

	PackageManager myPackageManager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		myPackageManager = getPackageManager();

		Intent intent = new Intent(Intent.ACTION_MAIN, null);
		intent.addCategory(Intent.CATEGORY_LAUNCHER);
		List<ResolveInfo> appIntentList = getPackageManager()
				.queryIntentActivities(intent, 0);

		GridView gridview = new GridView(this);
		gridview.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
		gridview.setNumColumns(GridView.AUTO_FIT);
		gridview.setHorizontalSpacing(30);
		gridview.setVerticalSpacing(30);
		gridview.setGravity(Gravity.CENTER);
		gridview.setColumnWidth(50);
		gridview.setPadding(10, 10, 10, 10);

		gridview.setAdapter(new MyAdapter(this, appIntentList));

		gridview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				ResolveInfo cleckedResolveInfo = (ResolveInfo) parent
						.getItemAtPosition(position);
				ActivityInfo clickedActivityInfo = cleckedResolveInfo.activityInfo;

				Intent intent = new Intent(Intent.ACTION_MAIN);
				intent.addCategory(Intent.CATEGORY_LAUNCHER);
				intent.setClassName(
						clickedActivityInfo.applicationInfo.packageName,
						clickedActivityInfo.name);
				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
						| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
				startActivity(intent);
			}

		});

		setContentView(gridview);
	}
}
Website Pin Facebook Twitter Myspace Friendfeed Technorati del.icio.us Digg Google StumbleUpon Premium Responsive

Leave a Reply

Your email address will not be published. Required fields are marked *