1 /* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.example.android.apis.view; 18 19 import android.app.Activity; 20 import android.content.Intent; 21 import android.content.pm.ResolveInfo; 22 import android.os.Bundle; 23 import android.view.View; 24 import android.view.ViewGroup; 25 import android.widget.BaseAdapter; 26 import android.widget.GridView; 27 import android.widget.ImageView; 28 29 import java.util.List; 30 31 //Need the following import to get access to the app resources, since this 32 //class is in a sub-package. 33 import com.example.android.apis.R; 34 35 36 public class Grid1 extends Activity { 37 38 GridView mGrid; 39 40 @Override 41 protected void onCreate(Bundle savedInstanceState) { 42 super.onCreate(savedInstanceState); 43 44 loadApps(); // do this in onresume? 45 46 setContentView(R.layout.grid_1); 47 mGrid = (GridView) findViewById(R.id.myGrid); 48 mGrid.setAdapter(new AppsAdapter()); 49 } 50 51 private List<ResolveInfo> mApps; 52 53 private void loadApps() { 54 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); 55 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); 56 57 mApps = getPackageManager().queryIntentActivities(mainIntent, 0); 58 } 59 60 public class AppsAdapter extends BaseAdapter { 61 public AppsAdapter() { 62 } 63 64 public View getView(int position, View convertView, ViewGroup parent) { 65 ImageView i; 66 67 if (convertView == null) { 68 i = new ImageView(Grid1.this); 69 i.setScaleType(ImageView.ScaleType.FIT_CENTER); 70 i.setLayoutParams(new GridView.LayoutParams(50, 50)); 71 } else { 72 i = (ImageView) convertView; 73 } 74 75 ResolveInfo info = mApps.get(position); 76 i.setImageDrawable(info.activityInfo.loadIcon(getPackageManager())); 77 78 return i; 79 } 80 81 82 public final int getCount() { 83 return mApps.size(); 84 } 85 86 public final Object getItem(int position) { 87 return mApps.get(position); 88 } 89 90 public final long getItemId(int position) { 91 return position; 92 } 93 } 94 95 } 96