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 in both Java and Kotlin Programming languages for Android.
Step-by-Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. The code for that has been given in both Java and Kotlin Programming Language for Android.
Step 2: Working with the XML files
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 version = "1.0" encoding = "utf-8" ?>
< androidx.constraintlayout.widget.ConstraintLayout
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 >
|
Method 1
Go to the MainActivity file and refer to the following code. Below is the code for the MainActivity file. Comments are added inside the code to understand the code in more detail.
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();
}
} |
import android.content.pm.PackageManager
import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.ListView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
lateinit var listView: ListView
lateinit var text: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super .onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// initialise layout
listView = findViewById(R.id.listview)
text = findViewById(R.id.totalapp)
}
fun getAllApps() {
// get list of all the apps installed
val infos = packageManager.getInstalledApplications(PackageManager.GET_META_DATA)
// create a list with size of total number of apps
val apps = arrayOfNulls<String>(infos.size)
// add all the app name in string list
for ((i, info) in infos.withIndex()) {
apps[i] = info.packageName
}
// set all the apps name in list view
listView.adapter = ArrayAdapter( this @MainActivity , android.R.layout.simple_list_item_1, apps)
// write total count of apps available.
text.text = infos.size.toString() + " Apps are installed"
}
override fun onStart() {
super .onStart()
}
} |
Output:
Method 2
Go to the MainActivity file and refer to the following code. Below is the code for the MainActivity file. Comments are added inside the code to understand the code in more detail.
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();
}
} |
import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.ListView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
lateinit var listView: ListView
lateinit var text: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super .onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// initialise layout
listView = findViewById(R.id.listview)
text = findViewById(R.id.totalapp)
}
fun getAllApps() {
// get list of all the apps installed
val packList = packageManager.getInstalledPackages( 0 )
val apps = arrayOfNulls<String>(packList.size)
for (i in packList.indices) {
val packInfo = packList[i]
apps[i] = packInfo.applicationInfo.loadLabel(packageManager).toString()
}
// set all the apps name in list view
listView.adapter = ArrayAdapter( this @MainActivity , android.R.layout.simple_list_item_1, apps)
// write total count of apps available.
text.text = packList.size.toString() + " Apps are installed"
}
override fun onStart() {
super .onStart()
}
} |
Output:
Method 3
Go to the MainActivity file and refer to the following code. Below is the code for the MainActivity file. Comments are added inside the code to understand the code in more detail.
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();
}
} |
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.ListView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
lateinit var listView: ListView
lateinit var text: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super .onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// initialise layout
listView = findViewById(R.id.listview)
text = findViewById(R.id.totalapp)
}
@Throws (PackageManager.NameNotFoundException:: class )
fun getAllApps() {
val mainIntent = Intent(Intent.ACTION_MAIN, null )
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER)
// get list of all the apps installed
val ril = packageManager.queryIntentActivities(mainIntent, 0 )
val componentList: List<String> = ArrayList()
lateinit var name: String
var i = 0
// get size of ril and create a list
val apps = arrayOfNulls<String>(ril.size)
for (ri in ril) {
if (ri.activityInfo != null ) {
// get package
val res = packageManager.getResourcesForApplication(ri.activityInfo.applicationInfo)
// if activity label res is found
name = if (ri.activityInfo.labelRes != 0 ) {
res.getString(ri.activityInfo.labelRes)
} else {
ri.activityInfo.applicationInfo.loadLabel(packageManager).toString()
}
apps[i] = name
i++
}
}
// set all the apps name in list view
listView.adapter = ArrayAdapter( this @MainActivity , android.R.layout.simple_list_item_1, apps)
// write total count of apps available.
text.text = ril.size.toString() + " Apps are installed"
}
override fun onStart() {
super .onStart()
}
} |