Different Ways to Get List of All Apps Installed in Your Android Phone

17 Dec, 2021
In this article, we are going to show the list of all the installed apps on your Android phone. So here we are going to learn how to implement that feature in three different ways. Note that we are going to implement this project using the Java language.  
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
Step 2: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. 
XML
<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout    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:layout_gravity="center"    android:gravity="center"    android:orientation="vertical"    android:padding="16sp"    tools:context=".MainActivity">     <Button        android:id="@+id/check"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginTop="16dp"        android:onClick="getallapps"        android:text="Get Installed Apps"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintStart_toStartOf="parent"        app:layout_constraintTop_toTopOf="parent" />     <TextView        android:id="@+id/totalapp"        android:layout_width="match_parent"        android:layout_height="wrap_content"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintStart_toStartOf="parent"        app:layout_constraintTop_toBottomOf="@+id/check" />     <ListView        android:id="@+id/listview"        android:layout_width="365dp"        android:layout_height="523dp"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintStart_toStartOf="parent"        app:layout_constraintTop_toBottomOf="@+id/totalapp"        tools:layout_editor_absoluteY="150dp" />     </androidx.constraintlayout.widget.ConstraintLayout>
Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.
Java
import android.content.pm.ApplicationInfo;import android.content.pm.PackageManager;import android.os.Bundle;import android.view.View;import android.widget.ArrayAdapter;import android.widget.ListView;import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; import java.util.List; public class MainActivity extends AppCompatActivity {     ListView listView;    TextView text;     @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);                 // initialise layout        listView = findViewById(R.id.listview);        text = findViewById(R.id.totalapp);    }     public void getallapps(View view) {        // get list of all the apps installed        List<ApplicationInfo> infos = getPackageManager().getInstalledApplications(PackageManager.GET_META_DATA);        // create a list with size of total number of apps        String[] apps = new String[infos.size()];        int i = 0;        // add all the app name in string list        for (ApplicationInfo info : infos) {            apps[i] = info.packageName;            i++;        }        // set all the apps name in list view        listView.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, apps));        // write total count of apps available.        text.setText(infos.size() + " Apps are installed");    }     @Override    protected void onStart() {        super.onStart();     }}
Output:
Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.
Java
import android.content.pm.PackageInfo;import android.os.Bundle;import android.view.View;import android.widget.ArrayAdapter;import android.widget.ListView;import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; import java.util.List; public class MainActivity extends AppCompatActivity {         ListView listView;    TextView text;     @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);                 // initialise layout        listView = findViewById(R.id.listview);        text = findViewById(R.id.totalapp);    }     public void getallapps(View view) {        // get list of all the apps installed        List<PackageInfo> packList = getPackageManager().getInstalledPackages(0);        String[] apps = new String[packList.size()];        for (int i = 0; i < packList.size(); i++) {            PackageInfo packInfo = packList.get(i);            apps[i] = packInfo.applicationInfo.loadLabel(getPackageManager()).toString();        }        // set all the apps name in list view        listView.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, apps));        // write total count of apps available.        text.setText(packList.size() + " Apps are installed");    }     @Override    protected void onStart() {        super.onStart();     }}
Output: 
Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.
Java
import android.content.Intent;import android.content.pm.PackageManager;import android.content.pm.ResolveInfo;import android.content.res.Resources;import android.os.Bundle;import android.view.View;import android.widget.ArrayAdapter;import android.widget.ListView;import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; import java.util.ArrayList;import java.util.List; public class MainActivity extends AppCompatActivity {     ListView listView;    TextView text;     @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);         // initialise layout        listView = findViewById(R.id.listview);        text = findViewById(R.id.totalapp);    }      public void getallapps(View view) throws PackageManager.NameNotFoundException {        final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);         // get list of all the apps installed        List<ResolveInfo> ril = getPackageManager().queryIntentActivities(mainIntent, 0);        List<String> componentList = new ArrayList<String>();        String name = null;        int i = 0;         // get size of ril and create a list        String[] apps = new String[ril.size()];        for (ResolveInfo ri : ril) {            if (ri.activityInfo != null) {                // get package                Resources res = getPackageManager().getResourcesForApplication(ri.activityInfo.applicationInfo);                // if activity label res is found                if (ri.activityInfo.labelRes != 0) {                    name = res.getString(ri.activityInfo.labelRes);                } else {                    name = ri.activityInfo.applicationInfo.loadLabel(                            getPackageManager()).toString();                }                apps[i] = name;                i++;            }        }        // set all the apps name in list view        listView.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, apps));        // write total count of apps available.        text.setText(ril.size() + " Apps are installed");    }     @Override    protected void onStart() {        super.onStart();     }}
Output:
as5853535
Android
Java
Java
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.