Android show installed app in ListView ** New Source code

How to show all installed app in Android

We are always want to work with android all installed apps first step is to show all app in listview its easy. Here below is complete source code of show android all installed application. Show installed app in listview android. NextGenEarn Blog Get Latest.

Below source code of MainActivity –

package write your package name here;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

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

public class Videosw extends AppCompatActivity {

    private List<ApplicationInfo> appList = null;

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

        final ListView myInstalledApp = (ListView) findViewById(R.id.all_installed_app);

        final List<AppList> installedApps = getAllInstalledApps();
        final AppAdapter installedAppAdapter = new AppAdapter(Videosw.this, installedApps);
        myInstalledApp.setAdapter(installedAppAdapter);
    }

    private List<AppList> getAllInstalledApps() {

        List<AppList> List = 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());
                List.add(new AppList(appName, icon));
            }
        }
        return List;
    }

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

    public class AppAdapter extends BaseAdapter {

        private LayoutInflater layoutInflater;
        private List<AppList> appInList;

        public AppAdapter(Context context, List<AppList> customizedListView) {
            layoutInflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            appInList = customizedListView;
        }
        @Override
        public int getCount() {
            return appInList.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.app_installed_list, parent, false);

                listViewHolder.appNameText = (TextView)convertView.findViewById(R.id.app_names);
                listViewHolder.appIcon = (ImageView)convertView.findViewById(R.id.app_icons);
                convertView.setTag(listViewHolder);
            }
            else{
                listViewHolder = (ViewHolder)convertView.getTag();
            }
            listViewHolder.appNameText.setText(appInList.get(position).getName());
            listViewHolder.appIcon.setImageDrawable(appInList.get(position).getIcon());

            return convertView;
        }
    }
    static class ViewHolder{
        TextView appNameText;
        ImageView appIcon;
    }

    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;
        }
    }
}

Here is the code of activity_main.xml -
<?xml version="1.0" encoding="utf-8"?>
<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=".MainActivity">

    <ListView
        android:id="@+id/installed_app_list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

</RelativeLayout>

Create new xml file inside res > layout app_installed_list.xml -
<?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="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="#00ffe1"
        android:layout_toRightOf="@+id/app_icon"
        android:layout_alignTop="@+id/app_icon"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="2dp"
        android:text="App Name"/>

</RelativeLayout>

Source for AndroidManifest.xml -
<?xml version=”1.0″ encoding=”utf-8″ ?>
<manifest
xmlns:android=”http://schemas.android.com/apk/res/android xmlns:tools=”http://schemas.android.com/tools
package=”utube.attraction.passcode>
  <uses-permission android:name=”android.permission.MOUNT_UNMOUNT_FILESYSTEMS />
  <uses-permission android:name=”android.permission.PACKAGE_USAGE_STATS />
  <uses-permission android:name=”android.permission.GET_TASKS /> // try without this line
<application
android:allowBackup=”true
android:icon=”@mipmap/ic_launcher
android:label=”@string/app_name
android:supportsRtl=”true
android:theme=”@style/AppTheme>
<activity
android:name=”.MainActivity>
<intent-filter>
  <action
android:name=”android.intent.action.MAIN />
  <category
android:name=”android.intent.category.LAUNCHER />
  </intent-filter>
  </activity>
  <uses-permission android:name=”android.permission.PACKAGE_USAGE_STATS tools:ignore=”ProtectedPermissions />
  </service>
  </application>
  </manifest>

Add a Comment

Your email address will not be published. Required fields are marked *