Thursday, 25 April 2013

Create Our Own App Lancher


Main.xml
<?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"
    android:background="@drawable/bg">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Purple App Lancher"
        android:layout_gravity="center"
        android:layout_marginTop="15dp"
        android:textStyle="bold"
        android:textSize="30sp"
        android:textColor="#2E0854" />
        <ListView 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@android:id/list"
            android:layout_marginLeft="200dp"
            android:layout_marginRight="200dp"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:background="@drawable/purple"/>
             
</LinearLayout>
   

row.xml


<?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="wrap_content">
       
        <ImageView
                android:id="@+id/app_icon"
                android:layout_width="48px"
                android:layout_height="48px"
                android:padding="3px"
                android:scaleType="fitXY"/>
       
        <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">
               
                <TextView
                        android:id="@+id/app_name"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:gravity="center_vertical"
                        android:textColor="#fff"/>
       
        </LinearLayout>
</LinearLayout>


MainActivity:


package com.example.applauncher;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class AppLauncher extends ListActivity {
PackageManager packageManager = null;
List<ApplicationInfo> applist = null;
ApplicationAdaptor listadaptor = null;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

packageManager = getPackageManager();

new LoadApplicationTask().execute();
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);

ApplicationInfo app = applist.get(position);


try {
Intent intent = packageManager
.getLaunchIntentForPackage(app.packageName);

if (null != intent) {
startActivity(intent);
}
} catch (ActivityNotFoundException e) {
Toast.makeText(AppLauncher.this, e.getMessage(), Toast.LENGTH_LONG)
.show();
} catch (Exception e) {
Toast.makeText(AppLauncher.this, e.getMessage(), Toast.LENGTH_LONG)
.show();
}

}

private List<ApplicationInfo> checkForLaunchIntent(
List<ApplicationInfo> list) {
ArrayList<ApplicationInfo> applist = new ArrayList<ApplicationInfo>();

for (ApplicationInfo info : list) {
if (info.packageName.startsWith("com.test")) {

try {
if (null != packageManager.getLaunchIntentForPackage(info.packageName)) {
applist.add(info);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

return applist;
}

private class LoadApplicationTask extends AsyncTask<Void, Void, Void> {
private ProgressDialog progress = null;

@Override
protected Void doInBackground(Void... params) {
applist = checkForLaunchIntent(packageManager
.getInstalledApplications(PackageManager.GET_META_DATA));

listadaptor = new ApplicationAdaptor(AppLauncher.this,
R.layout.row, applist);

return null;
}

@Override
protected void onCancelled() {
super.onCancelled();
}

@Override
protected void onPostExecute(Void result) {
setListAdapter(listadaptor);

progress.dismiss();

super.onPostExecute(result);
}

@Override
protected void onPreExecute() {
progress = ProgressDialog.show(AppLauncher.this, null,
"Loading application info...");

super.onPreExecute();
}

@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}

private class ApplicationAdaptor extends ArrayAdapter<ApplicationInfo> {
private List<ApplicationInfo> objects = null;

public ApplicationAdaptor(Context context, int textViewResourceId,
List<ApplicationInfo> objects) {
super(context, textViewResourceId, objects);

this.objects = objects;
}

@Override
public int getCount() {
return ((null != objects) ? objects.size() : 0);
}

@Override
public ApplicationInfo getItem(int position) {
return ((null != objects) ? objects.get(position) : null);
}

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

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

if (null == view) {
LayoutInflater vi = (LayoutInflater) AppLauncher.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.applauncherrow, null);
}

ApplicationInfo data = objects.get(position);

if (null != data) {
TextView textName = (TextView) view.findViewById(R.id.app_name);
ImageView iconview = (ImageView) view
.findViewById(R.id.app_icon);

textName.setText(data.loadLabel(packageManager) + " ("
+ data.packageName + ")");
iconview.setImageDrawable(data.loadIcon(packageManager));
}

return view;
}
};
}



in the above text color ,there you can give you own package name