How to display system app in ListView in Android

Create New Project

1. First create a new project in Android Studio (File => New Project)


All Apps fetching in MainActivity and display to other activity after on click button.

activity_main.xml

									   
   
   <?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"
	android:gravity="center"
    android:padding="10dp">

    <Button
        android:id="@+id/manageApps_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#4bb9b9"
        android:padding="30dp"
        android:text="All Apps"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#fff"
        android:textStyle="bold" />

</LinearLayout>

MainActivity.java


package com.nk.appsdetails;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

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

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    Context context;
    Button manageApps_btn;
    ArrayList arrayList;
    private List apps;
    private PackageManager packageManager;
    private ArrayList applicationInfos;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;
        manageApps_btn = (Button) findViewById(R.id.manageApps_btn);
        arrayList = new ArrayList<>();
        applicationInfos = new ArrayList<>();

        manageApps_btn.setOnClickListener(this);

        packageManager = getPackageManager();

        try {
            new AllAppTask(this, applicationInfos, packageManager).execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.manageApps_btn:
                startActivity(new Intent(context, DisplayAllApps.class).putExtra("app", arrayList));
                break;
        }
    }

    public void callBackDataFromAsynctask(List list) {
        try {
            applicationInfos.clear();
        } catch (Exception e) {
            e.printStackTrace();
        }

        for (int i = 0; i < list.size(); i++) {
            applicationInfos.add(list.get(i));
        }

        for (int j = 0; j < applicationInfos.size(); j++) {

            AppGETSET appGETSET = new AppGETSET();

            appGETSET.setAppname((String) applicationInfos.get(j).loadLabel(packageManager));
            appGETSET.setPackagename(applicationInfos.get(j).packageName);
            appGETSET.setIcon(String.valueOf(applicationInfos.get(j).loadIcon(packageManager)));

            arrayList.add(appGETSET);
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finishAffinity();
    }
}
	

AllAppTask.java

                                       
package com.nk.appsdetails;

import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.AsyncTask;

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

/**
 * Created by narendra on 30-Nov-17.
 */

public class AllAppTask extends AsyncTask> {

    private MainActivity activity;
    private List applicationInfos;
    private PackageManager packageManager;

    public AllAppTask(MainActivity activity, ArrayList applicationInfos, PackageManager packageManager) {
        this.activity = activity;
        this.applicationInfos = applicationInfos;
        this.packageManager = packageManager;
    }


    @Override
    protected List doInBackground(Void... params) {
        applicationInfos = checkForLaunchIntent(packageManager.getInstalledApplications(PackageManager.GET_META_DATA));
        return applicationInfos;
    }

    private List checkForLaunchIntent(List list) {
        ArrayList applist = new ArrayList<>();
        for (ApplicationInfo applicationInfo : list) {
            try {
                if (packageManager.getLaunchIntentForPackage(applicationInfo.packageName) != null) {
                    applist.add(applicationInfo);
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

        return applist;
    }

    @Override
    protected void onPostExecute(List list) {
        super.onPostExecute(list);
        activity.callBackDataFromAsynctask(list);
    }

}							   
	

AppGETSET.java

                                       
									  package com.nk.appsdetails;

import android.content.pm.ApplicationInfo;

import java.io.Serializable;

/**
 * Created by narendra on 01-Dec-17.
 */

public class AppGETSET implements Serializable {

    String Appname;
    String packagename;
    String apptime;
    ApplicationInfo applicationInfo;
    String icon;

    int Blockposition;

    boolean appBlockCheck;

    public boolean isChecked;

    public int getBlockposition() {
        return Blockposition;
    }

    public void setBlockposition(int blockposition) {
        Blockposition = blockposition;
    }

    public boolean isChecked() {
        return isChecked;
    }

    public void setChecked(boolean checked) {
        isChecked = checked;
    }

    public ApplicationInfo getApplicationInfo() {
        return applicationInfo;
    }

    public void setApplicationInfo(ApplicationInfo applicationInfo) {
        this.applicationInfo = applicationInfo;
    }

    public String getIcon() {
        return icon;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }

    public String getAppname() {
        return Appname;
    }

    public void setAppname(String appname) {
        Appname = appname;
    }

    public String getPackagename() {
        return packagename;
    }

    public void setPackagename(String packagename) {
        this.packagename = packagename;
    }

    public String getApptime() {
        return apptime;
    }

    public void setApptime(String apptime) {
        this.apptime = apptime;
    }
}

									   
	

DisplayAllApps.java


                                       
									  package com.nk.appsdetails;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;

public class DisplayAllApps extends AppCompatActivity {

    Context context;
    ListView all_apps_listView;
    ArrayList AppsArray;
    AllAppsAdapter allAppsAdapter;
    private TextView headerText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_all_apps);
        context = this;
        all_apps_listView = (ListView) findViewById(R.id.all_apps_listView);

        LayoutInflater inflater = getLayoutInflater();
        View header = inflater.inflate(R.layout.layout_lv_header, all_apps_listView, false);
        headerText = (TextView) header.findViewById(R.id.text_header);
        all_apps_listView.addHeaderView(header, null, false);

        AppsArray = (ArrayList) getIntent().getSerializableExtra("app");
        if (AppsArray == null) {
            Log.w("------------", "MainActivity Array nullllll ");
        } else {
            try {
                Log.w("------------", "MainActivity Array List Size  : " + AppsArray.size());

                headerText.setText("All Apps (" + AppsArray.size() + ")");

                allAppsAdapter = new AllAppsAdapter(DisplayAllApps.this, AppsArray);
                all_apps_listView.setAdapter(allAppsAdapter);
                //   allAppsAdapter.notifyDataSetChanged();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
}

									   
	

activity_display_all_apps.xml

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

    <ListView
        android:id="@+id/all_apps_listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="5dp"
        android:scrollbars="none" />

</LinearLayout>
									   
	

layout_lv_header.xml

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

    <TextView
        android:id="@+id/text_header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="22sp"
        android:textColor="@android:color/white"
        android:textStyle="bold" />

</LinearLayout>
									   
	

AllAppsAdapter.java

                                       
package com.nk.appsdetails;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
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.ArrayList;

/**
 * Created by narendra on 01-Dec-17.
 */

public class AllAppsAdapter extends BaseAdapter {

    private final String TAG = "AllAppsAdapter";

    private ArrayList alreadyAppsArray;
    private ArrayList blockArrayList;
    private DisplayAllApps activity;
    private PackageManager packageManager;
    ArrayList ChildBlockApp;
    AppGETSET appGETSET;

    public AllAppsAdapter(DisplayAllApps activity, ArrayList alreadyAppsArray) {
        this.activity = activity;
        this.alreadyAppsArray = alreadyAppsArray;
        this.blockArrayList = new ArrayList<>();
        packageManager = activity.getPackageManager();
    }

    @Override
    public int getCount() {
        return alreadyAppsArray.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        final ViewHolder holder;
        LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_listview, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.app_name.setText(alreadyAppsArray.get(position).getAppname());
        holder.app_package.setText(alreadyAppsArray.get(position).getPackagename());

        PackageManager pm = activity.getPackageManager();
        ApplicationInfo info = null;
        try {
            info = pm.getApplicationInfo(alreadyAppsArray.get(position).getPackagename(), PackageManager.GET_META_DATA);
            holder.app_icon.setImageDrawable(info.loadIcon(pm));
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        convertView.setOnClickListener(onClickListener(position));
        return convertView;
    }

    private View.OnClickListener onClickListener(final int position) {
        return new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ApplicationInfo app = null;
                try {
                    Intent intent = packageManager.getLaunchIntentForPackage(alreadyAppsArray.get(position).getPackagename());
                    activity.startActivity(intent);
                    if (null != intent) {
                        activity.startActivity(intent);
                    }
                } catch (ActivityNotFoundException e) {
                    e.printStackTrace();
                }
            }
        };
    }

    private class ViewHolder {
        private ImageView app_icon;
        private TextView app_name;
        private TextView app_package;

        public ViewHolder(View v) {
            app_icon = (ImageView) v.findViewById(R.id.icon);
            app_name = (TextView) v.findViewById(R.id.name);
            app_package = (TextView) v.findViewById(R.id.app_package);
        }
    }
}
									   
	

item_listview.xml

                                       

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="3dp">

    <ImageView
        android:id="@+id/icon"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:padding="3dp"
        android:scaleType="centerCrop" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:paddingLeft="2dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:id="@+id/name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:textSize="18sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/app_package"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:textSize="12sp" />

        </LinearLayout>

    </LinearLayout>

</LinearLayout>
									  
									   
	

Output :