Retrieve Installed Applications Information with RecyclerView

In this tutorial, you will learn how to develop an Android application that retrieves and displays information about installed applications on a device. You'll implement functionality to fetch details such as application name, package name, version, and icon using Android's PackageManager. This information will be presented in a scrollable list using RecyclerView, allowing users to view and interact with the installed applications' data within the app's UI efficiently. The tutorial will guide you through the process of setting up RecyclerView, fetching application details, and displaying them in a structured manner for enhanced user experience.
Submitted on July 07, 2024

To create an Android app that retrieves information about installed applications, including their name, package name, version, and icon, we can follow these steps. We'll create a simple app that displays this information in a list using a RecyclerView.

Step-by-Step Implementation

1. Update Manifest Permissions

Add the following permissions to your ‘AndroidManifest.xml’ file:


<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_TASKS" />

      

2. Create Layout (activity_main.xml)

Create a layout file (‘activity_main.xml’) that includes a RecyclerView to display the list of installed applications:


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="8dp"
        android:clipToPadding="false"
        android:clipChildren="false"
        android:scrollbars="vertical" />

</RelativeLayout>


      

3. Create RecyclerView Item Layout (list_item_app.xml)

Create a layout file (‘list_item_app.xml’) for each item in the RecyclerView:


<?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="horizontal"
    android:padding="8dp">

    <ImageView
        android:id="@+id/imageViewIcon"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:scaleType="centerCrop"
        android:src="@mipmap/ic_launcher_round"
        android:contentDescription="@string/app_icon" />

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical"
        android:paddingStart="8dp">

        <TextView
            android:id="@+id/textViewName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="16sp"
            android:textStyle="bold"
            android:maxLines="1"
            android:ellipsize="end"
            android:text="App Name" />

        <TextView
            android:id="@+id/textViewPackage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="14sp"
            android:textColor="@android:color/darker_gray"
            android:maxLines="1"
            android:ellipsize="end"
            android:text="Package Name" />

        <TextView
            android:id="@+id/textViewVersion"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="14sp"
            android:textColor="@android:color/darker_gray"
            android:maxLines="1"
            android:ellipsize="end"
            android:text="Version" />

    </LinearLayout>
</LinearLayout>

      

4. Create AppInfo Model Class (AppInfo.kt)

Create a Kotlin data class (‘AppInfo.kt’) to represent information about each installed application:


package com.example.installedapps

data class AppInfo(
    val appName: String,
    val packageName: String,
    val versionName: String,
    val versionCode: Long,
    val icon: Int // Resource ID of the app's icon
)

    

5. Implement MainActivity (MainActivity.kt)

Implement ‘MainActivity.kt’ to retrieve installed applications and populate the RecyclerView:


package com.example.installedapps

import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    private lateinit var appList: MutableList<AppInfo>
    private lateinit var adapter: AppAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        appList = mutableListOf()
        adapter = AppAdapter(this, appList)

        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.adapter = adapter

        loadInstalledApps()
    }

    private fun loadInstalledApps() {
        val packageManager = packageManager
        val installedApps = packageManager.getInstalledApplications(PackageManager.GET_META_DATA)

        for (app in installedApps) {
            try {
                val appName = packageManager.getApplicationLabel(app).toString()
                val packageName = app.packageName
                val packageInfo = packageManager.getPackageInfo(packageName, 0)
                val versionName = packageInfo.versionName ?: "Unknown"
                val versionCode = if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P) {
                    packageInfo.longVersionCode
                } else {
                    packageInfo.versionCode.toLong()
                }
                val icon = packageManager.getApplicationIcon(app.packageName)
                val appInfo = AppInfo(appName, packageName, versionName, versionCode, icon)
                appList.add(appInfo)
            } catch (e: PackageManager.NameNotFoundException) {
                e.printStackTrace()
            }
        }

        adapter.notifyDataSetChanged()
    }
}

    

6. Create RecyclerView Adapter (AppAdapter.kt)

Create a RecyclerView adapter (‘AppAdapter.kt’) to bind data to the list items:


package com.example.installedapps

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

class AppAdapter(private val context: Context, private val appList: List<AppInfo>) :
    RecyclerView.Adapter<AppAdapter.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.list_item_app, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val appInfo = appList[position]

        holder.textViewName.text = appInfo.appName
        holder.textViewPackage.text = appInfo.packageName
        holder.textViewVersion.text = "Version: ${appInfo.versionName}"

        holder.imageViewIcon.setImageDrawable(appInfo.icon)
    }

    override fun getItemCount(): Int {
        return appList.size
    }

    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val imageViewIcon: ImageView = itemView.findViewById(R.id.imageViewIcon)
        val textViewName: TextView = itemView.findViewById(R.id.textViewName)
        val textViewPackage: TextView = itemView.findViewById(R.id.textViewPackage)
        val textViewVersion: TextView = itemView.findViewById(R.id.textViewVersion)
    }
}

    

Explanation:

  • Layout (activity_main.xml): Defines the structure of the main activity with a RecyclerView to display installed applications.

  • RecyclerView Item Layout (list_item_app.xml): Defines the layout for each item in the RecyclerView, displaying app name, package name, version, and icon.

  • Model Class (AppInfo.kt): Represents the data model for an installed application, including its attributes like name, package name, version, and icon.

  • MainActivity (MainActivity.kt):

    • Initializes RecyclerView and its adapter (AppAdapter).
    • Uses PackageManager to retrieve a list of installed applications (getInstalledApplications).
    • Iterates through the list of applications, extracts relevant information (appName, packageName, versionName, versionCode, icon), and creates AppInfo objects.
    • Populates appList with AppInfo objects and notifies the adapter of changes (notifyDataSetChanged).
  • RecyclerView Adapter (AppAdapter.kt):

    • Binds data from AppInfo objects to the views in list_item_app.xml (imageViewIcon, textViewName, textViewPackage, textViewVersion).
    • Manages the RecyclerView item views and efficiently updates them based on changes in appList.

Running the App:

After completing these steps, your app should display a list of installed applications when launched on an emulator or a physical device. Each item in the list will show the application's name, package name, version, and icon. This implementation provides a basic framework for displaying installed applications and can be extended with additional features or UI enhancements as needed.