Android List Installed Apps in Device Programmatically

Android List Installed Apps in Device Programmatically

In this tutorial, we are going to learn how to retrieve and display all the installed app in a device. We will only focus on third-party apps so we will exclude the System apps.

The app will use ListView to display all installed apps. Since retrieving installed apps is a tasking task, you can write this application to use an AsyncTask class. The will help to prevent the app from blocking the Main UI Thread.

Right now we will have a look at the screen shot of the application we will build. This will give us a clear idea and understand of what might be involved in achieving this task.

screenshot

Before we start, it is important for you to have an individual on the tools and resources I used in this application. Feel free to use any tools you are familiar with.

Windows 7

Android Studio

Sony Xperia ZL

Min SDK 14

Target SDK 19

To create a new android application project, following the steps as stipulated below.

Go to File menu

Click on New menu

Click on Android Application

Enter Project name: AndroidVoiceRecording

Package: com.inducesmile.androidvoicerecording

Keep other default selections.

Continue to click on next button until Finish button is active, then click on Finish Button

Now, lets open the main layout of our project (activity_main.xml). The layout file will contain a ListView inside a RelativeLayout. The ListView control will hold the installed apps with the use of an adapter. Copy and paste this code in your layout file.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#eceaea"
    tools:context="inducesmile.com.completeandroid.InstalledAppFragment">

    <ListView
        android:id="@+id/installed_app_list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentTop="true"
        android:scrollbars="none" />

</RelativeLayout>

Lets move on to create our adapter class. The adapter class will inherit from the BaseAdapter, the we will override some methods of this class.

The adapter class will inflate a layout file which will hold each item of the ListView. Copy and paste the following code in your adapter class (AppAdapter.java).

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

public class AppAdapter extends BaseAdapter {

    private LayoutInflater layoutInflater;
    private 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);
            convertView.setTag(listViewHolder);
        }else{
            listViewHolder = (ViewHolder)convertView.getTag();
        }
        listViewHolder.textInListView.setText(listStorage.get(position).getName());
        listViewHolder.imageInListView.setImageDrawable(listStorage.get(position).getIcon());

        return convertView;
    }

    static class ViewHolder{

        TextView textInListView;
        ImageView imageInListView;
    }
}

In the above code, you will see that the installed_app_list.xml layout was inflated. We are going to create this layout file in the layout folder of our project. In the layout file, we will add a TextView that will hold the name of the app and an ImageView that will hold the app icon. Copy and paste the following in your layout file.

<?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">

    <ImageView
        android:id="@+id/app_icon"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:contentDescription="@string/hello_world"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="16dp"
        android:paddingBottom="8dp"/>

    <TextView
        android:id="@+id/list_app_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:textSize="16dp"
        android:textColor="#000000"
        android:layout_toRightOf="@+id/app_icon"
        android:layout_alignTop="@+id/app_icon"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="2dp"
        android:text="@string/hello_world"/>

</RelativeLayout>

Since each of our retrieve app will contain the app name and icon, we will create an entity class that will hold information associated with each app. Lets create a java now called AppList.java. Copy and paste the following code in this file.

import android.graphics.drawable.Drawable;

public class AppList {

    private String name;
    Drawable icon;

    public AppList(String name, Drawable icon) {
        this.name = name;
        this.icon = icon;
    }

    public String getName() {
        return name;
    }

    public Drawable getIcon() {
        return icon;
    }
}

Finally, lets move over to our MainActivity class. We will get the handle of our ListView control. The instance of the adapter class will be create and on data source will be passed as one of the parameters. Thereafter, will set the adapter instance to our ListView. Copy and paste the following code to the MainActivity.java file.

package com.inducesmile.henry.listallinstalledapps;

import android.app.Activity;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ListView userInstalledApps = (ListView)findViewById(R.id.installed_app_list);

        List<AppList> installedApps = getInstalledApps();
        AppAdapter installedAppAdapter = new AppAdapter(MainActivity.this, installedApps);
        userInstalledApps.setAdapter(installedAppAdapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private List<AppList> getInstalledApps() {
        List<AppList> res = new ArrayList<AppList>();
        List<PackageInfo> packs = getPackageManager().getInstalledPackages(0);
        for (int i = 0; i < packs.size(); i++) {
            PackageInfo p = packs.get(i);
            if ((isSystemPackage(p) == false)) {
                String appName = p.applicationInfo.loadLabel(getPackageManager()).toString();
                Drawable icon = p.applicationInfo.loadIcon(getPackageManager());
                res.add(new AppList(appName, icon));
            }
        }
        return res;
    }

    private boolean isSystemPackage(PackageInfo pkgInfo) {
        return ((pkgInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) ? true : false;
    }
}

Now, when you run your application you will see the interface that looks similar to the sample that was shown earlier on.

You can download the code for this tutorial below. If you are having hard time downloading the tutorials, kindly contact me.

Remember to subscribe with your email so that you will be among the first to receive our new post once it is published.

If you like this tutorial, please kindly download my new android app in Google play store by clicking the image below

One Response

Add a Comment