Created
January 21, 2016 04:42
-
-
Save BALUSANGEM/2893679c8e4e2fb63a0c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AppsListActivity extends Activity { | |
private PackageManager manager; | |
private ArrayList<AppDetail> apps; | |
private ListView list; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_apps_list); | |
loadApps(); | |
loadListView(); | |
addClickListener(); | |
} | |
private void loadApps(){ | |
manager = getPackageManager(); | |
apps = new ArrayList<AppDetail>(); | |
Intent i = new Intent(Intent.ACTION_MAIN, null); | |
i.addCategory(Intent.CATEGORY_LAUNCHER); | |
List<ResolveInfo> availableActivities = manager.queryIntentActivities(i, 0); | |
for(ResolveInfo ri:availableActivities){ | |
AppDetail app = new AppDetail(); | |
app.label = ri.loadLabel(manager); | |
app.name = ri.activityInfo.packageName; | |
app.icon = ri.activityInfo.loadIcon(manager); | |
apps.add(app); | |
} | |
} | |
private void loadListView(){ | |
list = (ListView)findViewById(R.id.apps_list); | |
ArrayAdapter<AppDetail> adapter = new ArrayAdapter<AppDetail>(this, | |
R.layout.list_item, | |
apps) { | |
@Override | |
public View getView(int position, View convertView, ViewGroup parent) { | |
if(convertView == null){ | |
convertView = getLayoutInflater().inflate(R.layout.list_item, null); | |
} | |
ImageView appIcon = (ImageView)convertView.findViewById(R.id.item_app_icon); | |
appIcon.setImageDrawable(apps.get(position).icon); | |
TextView appLabel = (TextView)convertView.findViewById(R.id.item_app_label); | |
appLabel.setText(apps.get(position).label); | |
TextView appName = (TextView)convertView.findViewById(R.id.item_app_name); | |
appName.setText(apps.get(position).name); | |
return convertView; | |
} | |
}; | |
list.setAdapter(adapter); | |
} | |
private void addClickListener(){ | |
list.setOnItemClickListener(new AdapterView.OnItemClickListener() { | |
@Override | |
public void onItemClick(AdapterView<?> av, View v, int pos, | |
long id) { | |
Intent i = manager.getLaunchIntentForPackage(apps.get(pos).name.toString()); | |
AppsListActivity.this.startActivity(i); | |
} | |
}); | |
} | |
} | |
class AppDetail { | |
CharSequence label; | |
CharSequence name; | |
Drawable icon; | |
} |
I guess you could use a comparator to sort the apps arraylist
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is great! Please how do I sort them by name?