Get List of User Installed Apps with icon, title, package name programmatically
In this tutorial we will learn how to get list of installed apps with icon, title, package name. We use custom ListView to display installed apps.
✓ Display User Installed Apps in ListView with icon, title, package
✓ Display total number of User Installed apps
✓ Handle On click(Display dialog to open app or open app info)
✓ Display User Installed Apps in ListView with icon, title, package
✓ Display total number of User Installed apps
✓ Handle On click(Display dialog to open app or open app info)
Step 1: Create a new project OR Open your project
Step 2: Create a new "Layout Resource File" in res>layout and name it as "installed_app_list.xml"
Step 3: Code
installed_app_list.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="3dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="5dp"> <ImageView android:id="@+id/app_icon" android:layout_width="48dp" android:layout_height="48dp"/> <LinearLayout android:layout_marginLeft="3dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:layout_marginStart="3dp"> <TextView android:id="@+id/list_app_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:textStyle="bold" android:textSize="16dp" android:text="App Name"/> <TextView android:id="@+id/app_package" android:text="App Package Name" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <TextView android:id="@+id/countApps" android:text="Count: " android:padding="5dp" android:layout_width="match_parent" android:layout_height="wrap_content" /> <ListView android:id="@+id/installed_app_list" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
MainActivity.java
package com.blogspot.devofandroid.myapplication; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.pm.ApplicationInfo; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Bundle; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private List<AppList> installedApps; private AppAdapter installedAppAdapter; ListView userInstalledApps; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); userInstalledApps = (ListView) findViewById(R.id.installed_app_list); installedApps = getInstalledApps(); installedAppAdapter = new AppAdapter(MainActivity.this, installedApps); userInstalledApps.setAdapter(installedAppAdapter); userInstalledApps.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, final int i, long l) { String[] colors = {" Open App", " App Info"}; AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("Choose Action") .setItems(colors, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // The 'which' argument contains the index position of the selected item if (which==0){ Intent intent = getPackageManager().getLaunchIntentForPackage(installedApps.get(i).packages); if(intent != null){ startActivity(intent); } else { Toast.makeText(MainActivity.this, installedApps.get(i).packages + " Error, Please Try Again...", Toast.LENGTH_SHORT).show(); } } if (which==1){ Intent intent = new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS); intent.setData(Uri.parse("package:" + installedApps.get(i).packages)); Toast.makeText(MainActivity.this, installedApps.get(i).packages, Toast.LENGTH_SHORT).show(); startActivity(intent); } } }); builder.show(); } }); //Total Number of Installed-Apps(i.e. List Size) String abc = userInstalledApps.getCount()+""; TextView countApps = (TextView)findViewById(R.id.countApps); countApps.setText("Total Installed Apps: "+abc); Toast.makeText(this, abc+" Apps", Toast.LENGTH_SHORT).show(); } private List<AppList> getInstalledApps() { PackageManager pm = getPackageManager(); List<AppList> apps = new ArrayList<AppList>(); List<PackageInfo> packs = getPackageManager().getInstalledPackages(0); //List<PackageInfo> packs = getPackageManager().getInstalledPackages(PackageManager.GET_PERMISSIONS); for (int i = 0; i < packs.size(); i++) { PackageInfo p = packs.get(i); if ((!isSystemPackage(p))) { String appName = p.applicationInfo.loadLabel(getPackageManager()).toString(); Drawable icon = p.applicationInfo.loadIcon(getPackageManager()); String packages = p.applicationInfo.packageName; apps.add(new AppList(appName, icon, packages)); } } return apps; } private boolean isSystemPackage(PackageInfo pkgInfo) { return (pkgInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0; } public class AppAdapter extends BaseAdapter { public LayoutInflater layoutInflater; public List<AppList> listStorage; public AppAdapter(Context context, List<AppList> customizedListView) { layoutInflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); listStorage = customizedListView; } @Override public int getCount() { return listStorage.size(); } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder listViewHolder; if(convertView == null){ listViewHolder = new ViewHolder(); convertView = layoutInflater.inflate(R.layout.installed_app_list, parent, false); listViewHolder.textInListView = (TextView)convertView.findViewById(R.id.list_app_name); listViewHolder.imageInListView = (ImageView)convertView.findViewById(R.id.app_icon); listViewHolder.packageInListView=(TextView)convertView.findViewById(R.id.app_package); convertView.setTag(listViewHolder); }else{ listViewHolder = (ViewHolder)convertView.getTag(); } listViewHolder.textInListView.setText(listStorage.get(position).getName()); listViewHolder.imageInListView.setImageDrawable(listStorage.get(position).getIcon()); listViewHolder.packageInListView.setText(listStorage.get(position).getPackages()); return convertView; } class ViewHolder{ TextView textInListView; ImageView imageInListView; TextView packageInListView; } } public class AppList { private String name; Drawable icon; private String packages; public AppList(String name, Drawable icon, String packages) { this.name = name; this.icon = icon; this.packages = packages; } public String getName() { return name; } public Drawable getIcon() { return icon; } public String getPackages() { return packages; } } }
Hi Parvez, thx .great tutorial. can you plz help with code i need to write. im a novice actually.
ReplyDeleteWhat is the issue?
Delete