Skip to content

Instantly share code, notes, and snippets.

@umairayub79
Created July 22, 2019 14:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save umairayub79/22e6ab182e50c6e884305fe0c55fcd24 to your computer and use it in GitHub Desktop.
Save umairayub79/22e6ab182e50c6e884305fe0c55fcd24 to your computer and use it in GitHub Desktop.
Code for loading all apps (including system apps) in a listView
private PackageManager packageManager;
private ArrayList<String> packageNames;
private ArrayAdapter<String> adapter;
private ListView listView;
// Setup UI elements
listView = new ListView(this);
listView.setVerticalScrollBarEnabled(false);
listView.setId(android.R.id.list);
listView.setDivider(null);
setContentView(listView);
ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) listView.getLayoutParams();
p.setMargins(100, 0, 0, 0);
// Get a list of all the apps installed
packageManager = getPackageManager();
adapter = new ArrayAdapter<String>(
this, android.R.layout.simple_list_item_1, new ArrayList<String>());
packageNames = new ArrayList<>();
// Tap on an item in the list to launch the app
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
try {
startActivity(packageManager.getLaunchIntentForPackage(packageNames.get(position)));
} catch (Exception e) {
fetchAppList();
}
}
});
// Long press on an item in the list to open the app settings
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
try {
// Attempt to launch the app with the package name
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setData(Uri.parse("package:" + packageNames.get(position)));
startActivity(intent);
} catch (ActivityNotFoundException e) {
fetchAppList();
}
return false;
}
});
fetchAppList();
private void fetchAppList() {
// Start from a clean adapter when refreshing the list
adapter.clear();
packageNames.clear();
// Query the package manager for all apps
List<ResolveInfo> activities = packageManager.queryIntentActivities(
new Intent(Intent.ACTION_MAIN, null).addCategory(Intent.CATEGORY_LAUNCHER), 0);
// Sort the applications by alphabetical order and add them to the list
Collections.sort(activities, new ResolveInfo.DisplayNameComparator(packageManager));
for (ResolveInfo resolver : activities) {
// Exclude the settings app
String appName = (String) resolver.loadLabel(packageManager);
if (appName.equals("Settings"))
continue;
adapter.add(appName);
packageNames.add(resolver.activityInfo.packageName);
}
listView.setAdapter(adapter);
}
@Override
public void onBackPressed() {
// Prevent the back button from closing the activity.
fetchAppList();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment