Wednesday, 20 June 2012

Load items when scroll reaches the end in ListView

listview.setOnScrollListener(new OnScrollListener() {
            private int threshold = 0;
            private int previousTotal = 0;
            private boolean loading = true;

            public void onScrollStateChanged(AbsListView view, int scrollState) {
                // TODO Auto-generated method stub

            }

            public void onScroll(AbsListView view, int firstVisibleItem,
                    int visibleItemCount, int totalItemCount) {
                // TODO Auto-generated method stub

                if (loading) {
                    if (totalItemCount > previousTotal) {
                        loading = false;
                        previousTotal = totalItemCount;
                    }
                } else {
                    Log.v("in gridview loading more","grid load");
                    if ((totalItemCount - visibleItemCount) == (firstVisibleItem + threshold)) {
                        // add items to your listview
                                        }
                }

            }
        });

Saturday, 4 February 2012

List all apk files present in exernal directory

The following code will list all the .apk files present in your external directory and ask us to install in the device.


public class ListApkActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ExtFilter apkFilter = new ExtFilter("apk");
        File file[] = Environment.getExternalStorageDirectory().listFiles(apkFilter);
        Log.d("InstallApk", "Filter applied. Size: "+ file.length);

        for (int i=0; i < file.length; i++)
        {
            Log.d("InstallApk", "FileName:" + file[i].getName());
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.fromFile(file[i]),  
    "application/vnd.android.package-archive");
            startActivity(intent);
        }
    }

    class ExtFilter implements FilenameFilter {  // class which list all the files having .apk extension
        String ext;
        public ExtFilter(String ext) {
            this.ext = "." + ext;
        }
        public boolean accept(File dir, String name) {
            return name.endsWith(ext);
        }
    }
    }