Wednesday, April 23, 2014

How to get all installed application in android example

How to get all installed application from device in android example

This post about how get installed  application from  android device
Create the layout for gridview and custom adapter view

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

    <GridView
        android:id="@+id/gridView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#636260"
        android:horizontalSpacing="1dp"
        android:numColumns="2"
        android:verticalSpacing="1dp" >
    </GridView>

</LinearLayout>


Adapter layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#f9dca0"
    android:orientation="vertical"
    android:paddingLeft="20dip" >

    <ImageView
        android:id="@+id/appIcon"
        android:layout_width="48dip"
        android:layout_height="48dip"
       />

    <TextView
        android:id="@+id/applicationName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="12dp" >
    </TextView>

</LinearLayout>

From the package manager get application name and icons
List<PackageInfo> pakageInstall = getPackageManager()
                           .getInstalledPackages(0);
              for (int i = 0; i < pakageInstall.size(); i++) {
                     PackageInfo packInfo = pakageInstall.get(i);
                     appStructure = new AppStructure();
                     appStructure.appName = packInfo.applicationInfo.loadLabel(getPackageManager()).toString();
                     appStructure.appIcons = packInfo.applicationInfo.loadIcon(getPackageManager());
                     appStructurelist.add(appStructure);
                     Log.e("error", "" + appStructure.appName);
                    

              }


Activity full source Code

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

import android.app.Activity;
import android.content.pm.PackageInfo;

import android.os.Bundle;
import android.util.Log;

import android.widget.GridView;


public class MainActivity extends Activity {

       ArrayList<AppStructure> appStructurelist = new ArrayList<AppStructure>();

       AppStructure appStructure;
       GridView gridView = null;

       @Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.myactivity);
              gridView = (GridView) findViewById(R.id.gridView1);
              List<PackageInfo> pakageInstall = getPackageManager()
                           .getInstalledPackages(0);
              for (int i = 0; i < pakageInstall.size(); i++) {
                     PackageInfo packInfo = pakageInstall.get(i);
                     appStructure = new AppStructure();
                     appStructure.appName = packInfo.applicationInfo.loadLabel(getPackageManager()).toString();
                     appStructure.appIcons = packInfo.applicationInfo.loadIcon(getPackageManager());
                     appStructurelist.add(appStructure);
                     Log.e("error", "" + appStructure.appName);
                    

              }
              InstalledAppAdapter adapter = new InstalledAppAdapter(this,
                           appStructurelist);
              gridView.setAdapter(adapter);
       }
}


CustomAdapter Class
import java.util.ArrayList;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class InstalledAppAdapter extends  BaseAdapter {

       Activity activity;
       ArrayList<AppStructure> appStrucutreList;
       public InstalledAppAdapter(Activity act, ArrayList<AppStructure> appStructure) {
             
              this.activity = act;
              this.appStrucutreList = appStructure;
             
       }
       @Override
       public int getCount() {
              // TODO Auto-generated method stub
              return appStrucutreList.size();
       }

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

       @Override
       public long getItemId(int arg0) {
              // TODO Auto-generated method stub
              return arg0;
       }

       @Override
       public View getView(int arg0, View view, ViewGroup arg2) {
      
              LayoutInflater inflator =(LayoutInflater)activity.getSystemService(activity.LAYOUT_INFLATER_SERVICE);
              view = inflator.inflate(R.layout.adapter_layout, null);
             
              TextView appName = (TextView)view.findViewById(R.id.applicationName);
              ImageView appIcon = (ImageView)view.findViewById(R.id.appIcon);
              appName.setText(appStrucutreList.get(arg0).appName);
              appIcon.setBackgroundDrawable(appStrucutreList.get(arg0).appIcons);
             
              return view;
       }

}




 AppStructure.class

import android.graphics.drawable.Drawable;

public class AppStructure {

      
       public String appName;
       public Drawable appIcons;
}




Downlaod Full Source Code Here

Check out this may be help you

Related Posts Plugin for WordPress, Blogger...