jjLin
jjLin

Reputation: 3291

How to dynamic add image/imagebutton in android listview or gridview

I have a list of emotion icons(over 100 icons).
And I would like to display in a view that people can click it and toast what he is clicking.
emotion icons Now, I have some confusing points:
1) What views should I use to include all this icons (GridView or ListView)?
2) How to dynamic add them or add them pro-grammatically? If I declare each one by one, I think it is no good. But any other ideas?
All the images icons is in drawable folder. Thanks.

Upvotes: 1

Views: 2344

Answers (1)

Lalit Poptani
Lalit Poptani

Reputation: 67286

GridView would be better in this case with a PopupWindow. Take a HashMap and store the name of the smiley as String and its Image as Resource Id.

eg:-

HashMap<String, Integer> map = new  HashMap<String, Integer>();
map.put("smile", R.drawable.smile);
.... 

Now, Iterate through all the values(images) of HashMap and poplulate your GridView from the HashMap values(images).

And, inside Adapter class you can show images using map.get(key); to ImageView and set the Resource.

UPDATE:

I had just created a small demo for the same you can check it from here.

Upvotes: 3

Related Questions