Some Times you may require user installed application in Android to perform certain task like Lock application or you may working on project which hide application or application like Task manager so this tutorial will help you to get all user installed application and sort them to display it. There are may ways to Display all application or only user installed apps with list view or Table view or you can use different layout for each Application to be display.
In this tutorial i am gone use following concept,
i hope you may know some basic concept of java like List,collection etc. but in list view adapter android call Listadapter.getview() method each time and create view and then display it. if you scroll list view it will create view for next few elements of the list and garbage collector will clean old created views every time you scroll list view.
I hope this may be helpful to you and share it so it can be helpful to the others. if you have any doubts or any error then let me know.
In this tutorial i am gone use following concept,
- List
- PackageManager
- Collections
- BaseAdapter
- PackageInfo
Main.java
package com.androprogrammer.app1; import java.util.Collections; import java.util.Comparator; import java.util.List; import android.app.Activity; import android.content.pm.ApplicationInfo; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.os.Bundle; import android.view.Menu; import android.widget.ListView; public class Main extends Activity { ListView apps; PackageManager packageManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); apps = (ListView) findViewById(R.id.listView1); packageManager = getPackageManager(); final List <packageinfo> packageList = packageManager .getInstalledPackages(PackageManager.GET_META_DATA); // all apps in the phone final List <packageinfo> packageList1 = packageManager .getInstalledPackages(0); try { packageList1.clear(); for (int n = 0; n < packageList.size(); n++) { PackageInfo PackInfo = packageList.get(n); if (((PackInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) != true) //check weather it is system app or user installed app { try { packageList1.add(packageList.get(n)); // add in 2nd list if it is user installed app Collections.sort(packageList1,new Comparator <packageinfo >() // this will sort App list on the basis of app name { public int compare(PackageInfo o1,PackageInfo o2) { return o1.applicationInfo.loadLabel(getPackageManager()).toString() .compareToIgnoreCase(o2.applicationInfo.loadLabel(getPackageManager()) .toString());// compare and return sorted packagelist. } }); } catch (NullPointerException e) { e.printStackTrace(); } } } } catch (Exception e) { e.printStackTrace(); } Listadapter Adapter = new Listadapter(this,packageList1, packageManager); apps.setAdapter(Adapter); } }
Listadapter.java
package com.androprogrammer.app1; import java.util.List; import android.app.Activity; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.graphics.drawable.Drawable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; public class Listadapter extends BaseAdapter{ List <packageinfo> packageList; Activity context; PackageManager packageManager; public Listadapter(Activity context, List <packageinfo> packageList, PackageManager packageManager) { super(); this.context = context; this.packageList = packageList; this.packageManager = packageManager; } private class ViewHolder { TextView apkName; } public int getCount() { return packageList.size(); } public Object getItem(int position) { return packageList.get(position); } public long getItemId(int position) { return 0; } @Override public View getView(final int position, View convertView, ViewGroup parent) { final ViewHolder holder; LayoutInflater inflater = context.getLayoutInflater(); if (convertView == null) { convertView = inflater.inflate(R.layout.list_item, null); holder = new ViewHolder(); holder.apkName = (TextView) convertView .findViewById(R.id.textView1); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } PackageInfo packageInfo = (PackageInfo) getItem(position); Drawable appIcon = packageManager .getApplicationIcon(packageInfo.applicationInfo); String appName = packageManager.getApplicationLabel( packageInfo.applicationInfo).toString(); appIcon.setBounds(0, 0, 40, 40); holder.apkName.setCompoundDrawables(appIcon, null, null, null); holder.apkName.setCompoundDrawablePadding(15); holder.apkName.setText(appName); return convertView; } }
main.xml
list_item.xml
You can directly copy and paste above code in your project and it will give you all user installed apps in sorted order. This will look like this. Image of Application will be fetch and display dynamically so you are not require to take image view in list_item.xml. for this
Drawable appIcon = packageManager.getApplicationIcon(packageInfo.applicationInfo);This will fetch icon from Packageinfo object and it will store in Drawable object.
i hope you may know some basic concept of java like List,collection etc. but in list view adapter android call Listadapter.getview() method each time and create view and then display it. if you scroll list view it will create view for next few elements of the list and garbage collector will clean old created views every time you scroll list view.
I hope this may be helpful to you and share it so it can be helpful to the others. if you have any doubts or any error then let me know.
Can u give me Zip file of this example
ReplyDeleteIt don't Show any error but it can't run on emulatore
It work perfect and help me So much
ReplyDeleteThank you very Much
I used this to create a list view with check box.. But the problem is when i click a check box in first page, the same position is selected in next page also, automatically... Plz help me... Thanks in advance
ReplyDeleteHey i can't understand what you are explaining.
Deletecan you put some code here or on pastebin so i can check and let you know
it will be easy to understand.