Application picker: display all the applications

We start implement the application picker first (aka All Application / Application Drawer). To start, we are going to start a pretty standard Android Project and link to Android-Binding (which the detail is covered in the other post).

Our App Picker is simply an Activity. (N.B. in Android-Binding, it is discouraged using ListActivity). Following is the code of the ApplicationPicker.java:

public class ApplicationPicker extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Binder.setAndBindContentView(this, R.layout.main, new ApplicationPickerViewModel(this));
    }
}

The most important logic is within the ApplicationPickerViewModel, which, it connects the query of application and the View itself:

public class ApplicationPickerViewModel {
	private final Context mContext;
	public ApplicationPickerViewModel(Context context){
		mContext = context;
		ApplicationList.addAll(getApplications());
	}

	public final ArrayListObservable
		ApplicationList = new ArrayListObservable(App.class);

	private List getApplications(){
		List apps = new ArrayList();

		Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);

        List mApps = mContext.getPackageManager().queryIntentActivities(mainIntent, 0);

        int length = mApps.size();
        for(int i=0; i        	ResolveInfo info = mApps.get(i);
        	Drawable image = info.loadIcon(mContext.getPackageManager());

        	apps.add(new App(
        			info.activityInfo.loadLabel(mContext.getPackageManager()),
        			info.activityInfo.name,
        			image));
        }

        Collections.sort(apps);

        return apps;
	}

	public static class App implements Comparable{
		public final StringObservable Name = new StringObservable();
		public final CharSequenceObservable Label = new CharSequenceObservable();
		public final Observable Icon = new Observable(Drawable.class);

		public App(CharSequence label, String name, Drawable icon) {
			super();
			Label.set(label);
			Name.set(name);
			Icon.set(icon);
		}

		public int compareTo(App another) {
			return Name.get().compareTo(another.Name.get());
		}
	}
}

The ApplicationViewModel consists of an Inner static class: App, which represents the App (icon) in the Application picker. To make sure the thing is working, we bind the above to a GridView, with a very simple item template:

Main Layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	xmlns:binding="http://www.gueei.com/android-binding/"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
	<GridView
	    android:layout_width="fill_parent"
	    android:layout_height="fill_parent"
	    android:layout_weight="1"
	    android:numColumns="4"
	    android:verticalSpacing="20dp"
    	android:horizontalSpacing="20dp"
    	android:gravity="center"
	    android:stretchMode="columnWidth"
	    binding:itemSource="ApplicationList"
	    binding:itemTemplate="@layout/application_picker_app_item"
	    />
</LinearLayout>

application_picker_app_item

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	xmlns:binding="http://www.gueei.com/android-binding/"
    android:layout_width="fill_parent"
    android:layout_height="80dip"
    android:gravity="center"
    android:orientation="vertical"
    >
	<ImageView
	    android:layout_width="40dip"
	    android:layout_height="40dip"
	    android:scaleType="fitCenter"
	    binding:source="Icon"
	    />
	<TextView
	    android:layout_width="fill_parent"
	    android:layout_height="fill_parent"
	    android:layout_weight="1"
	    android:gravity="center"
	    android:textSize="12dip"
	    android:maxLines="2"
	    android:ellipsize="marquee"
	    binding:text="Name"
	    />
</LinearLayout>

The display is fine, and next thing is to wire up the click, and implement the look and feel we want.

Advertisements