2

I want to make a list of app icons and display it in listView.I have stored the package names in ArrayList variable array.I am getting the applications icon by using this code:

Drawable icon=getPackageManager().getApplicationLogo(array.toString());

and storing all the icons in drawable variable icon.Now I want to set the variable icon with ArrayAdapter in order to display it in listview.But I don't know how to do it can anyone help me..

5
  • Do you have an adapter? If so, post your code Commented Jul 23, 2016 at 13:40
  • Nope I have not created it yet Commented Jul 23, 2016 at 13:41
  • @Kurlicue I know how to make adapter for ArrayAdapter<String> but not for ArrayAdapter for any drawable object Commented Jul 23, 2016 at 13:43
  • Do you want to display a diffrent icon for each row, or the same icon for every row? Commented Jul 23, 2016 at 13:52
  • @Kurlicue I want to display different icon in each row Commented Jul 23, 2016 at 14:06

2 Answers 2

1

If you want to make your listview element dynamic, for instance with image & package name, create your object class :

public class AppIcon {

    private Drawable mDrawable;

    private String mPackageName;

    public AppIcon(Drawable drawable, String packageName) {
        this.mDrawable = drawable;
        this.mPackageName = packageName;
    }

    public Drawable getmDrawable() {
        return mDrawable;
    }

    public String getmPackageName() {
        return mPackageName;
    }
}

Create the following custom RecyclerView adapter :

public class ImageArrayAdapter extends RecyclerView.Adapter<ImageArrayAdapter.ViewHolder> {

    /**
     * list of devices
     */
    private List<AppIcon> imageList = new ArrayList<>();

    private Context mContext;

    private IViewHolderClickListener mListener;

    public ImageArrayAdapter(Context context, List<AppIcon> objects, IViewHolderClickListener listener) {
        this.mContext = context;
        this.imageList = objects;
        this.mListener = listener;
    }

    @Override
    public ImageArrayAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflater = LayoutInflater.from(parent.getContext()).inflate(R.layout.listview_item, parent, false);
        return new ViewHolder(inflater, mListener);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        AppIcon item = imageList.get(position);

        holder.image.setImageDrawable(item.getmDrawable());
        holder.packageName.setText(item.getmPackageName());
    }

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

    @Override
    public int getItemCount() {
        return imageList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        public ImageView image;

        public TextView packageName;

        public IViewHolderClickListener mListener;

        /**
         * ViewHolder
         *
         * @param v
         * @param listener
         */
        public ViewHolder(View v, IViewHolderClickListener listener) {
            super(v);
            mListener = listener;
            image = (ImageView) v.findViewById(R.id.image);
            packageName = (TextView) v.findViewById(R.id.package_name);
            v.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            mListener.onClick(v);
        }
    }
}

This is better to use RecyclerView especially if you deal with a large list of image.

build.gradle :

compile 'com.android.support:appcompat-v7:24.0.0'
compile 'com.android.support:recyclerview-v7:+'

You can see that ViewHolder holds all your nested fields in your listview items and will dispatch click event too with :

public interface IViewHolderClickListener {

    void onClick(View v);
}

In your activity, create your adapter and set it as adapter's RecyclerView. Also, initialize your adapter with list of image :

public class DescriptionActivity extends Activity {

    private RecyclerView imageRecyclerView = null;

    private ImageArrayAdapter imageAdapter = null;

    private SwipeRefreshLayout mSwipeRefreshLayout;

    private ArrayList<AppIcon> imageList;

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

        //setup recyclerview
        imageRecyclerView = (RecyclerView) findViewById(R.id.image_list);

        imageList = new ArrayList<>();

        try {
            Drawable icon = getPackageManager().getApplicationIcon(getPackageName());
            imageList.add(new AppIcon(icon, getPackageName()));
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        imageAdapter = new ImageArrayAdapter(DescriptionActivity.this, imageList, new IViewHolderClickListener() {

            @Override
            public void onClick(View v) {

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(DescriptionActivity.this, "click on item detected", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });

        //set layout manager
        imageRecyclerView.setLayoutManager(new GridLayoutManager(this, 1, LinearLayoutManager.VERTICAL, false));

        imageRecyclerView.setAdapter(imageAdapter);

        //setup swipe refresh
        mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swiperefresh);

        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        //refresh list here
                        imageAdapter.notifyDataSetChanged();
                        mSwipeRefreshLayout.setRefreshing(false);
                    }
                });
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

}

activity.xml looks like :

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swiperefresh"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/image_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="vertical" />

    </android.support.v4.widget.SwipeRefreshLayout>

</FrameLayout>

and listview_item.xml which contain the layout for each your image/package name listview item :

<ImageView
    android:id="@+id/image"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:scaleType="center"
    android:layout_weight="0.5" />

<TextView
    android:id="@+id/package_name"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="0.5"
    android:fontFamily="sans-serif"
    android:textSize="18sp" />

Also, note that optionnaly I added a SwipeRefreshLayout to swipe refresh your listview in a material style

Sign up to request clarification or add additional context in comments.

4 Comments

You could pass an id of the drawable instead?
Thanks bro,I will sure try it and tell you the result
@Eenvincible Yes it would be far better to have an int rather than storing Drawable but if the op wants to get icon from other apps, it seems to be quite difficult to obtain a ressource id for these
I totally agree with you - makes sense now
0

Drawables are only integer values. So instead of using Drawable class, you can use an ArrayList array to store your drawables.

ArrayList<Integer> list = {R.drawable.icon1, R.drawable.icon2};

Then you can fetch the icon from this list using

getResources().getDrwawable(list.get(iconIndex));

Hope this solves your problem

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.