0

I currently have an app with a gridlayout of 6 ingredients. Each ingredient is a linearlayout with an image, name, and two buttons. Now the tough part has been that everything needs to be dynamically and programmatically generated, as these ingredients changes (strawberry, apple, orange, etc).

Now I was looking into changing the gridLayout to gridView with an adapter so I could leave room for future expansions as I get more ingredients. Most examples of gridview include an image and text. Now is it possible to have two buttons that I could programmatically set their id inside the gridview? If anyone could point me to the right direction or a more sophesticated gridview example that would be much appreciated.

Update: recycleview did the trick for me. Thanks for the answers.

HaMiD Sani
  • 360
  • 4
  • 19
  • `GridView` item is also a view in which you can add any view, be it 2 buttons or any other view. Every View has an `addView` which you can use to add sub view, in your case Buttons. – Sharjeel Dec 20 '15 at 21:56
  • Yeah, thanks. After reading a bit more you are absolutely correct – HaMiD Sani Dec 21 '15 at 01:46

2 Answers2

1

I don't see why you couldn't just set the buttons in the GridView adapter?

For whatever reason that doesn't work, you could potentially iterate through the GridView's children and, for each child, iterate through their LinearLayout to set the buttons. I haven't tried this, but it would look like:

for (int i = 0; i < myGridView.getChildCount(); i++) {
     LinearLayout childLayout = (LinearLayout) mGridView.getChildAt(i);
         for (int j = 0; j < childLayout.getChildCount(); j++) {
             if (childLayout.getChildAt(j) instanceof Button) {
                 // Set the button... 
             }
     }
}
Sean Thomas
  • 129
  • 12
1

I think what would work the best for you is a Recyclerview with a GridLayoutManager.

Just look up examples how to use a RecyclerView.

Boukharist
  • 1,219
  • 11
  • 19
  • 1
    Yeah good call. I think I was a bit confused about gridView and how it works. But I think Recyclerview is a better solution. Thanks – HaMiD Sani Dec 21 '15 at 01:45