-1

I have 64 image view in android studio and I don't want to define them one by one like this:

ImageView iv11 = findViewById(R.id.iv11);

id of ImageViews start by iv11 (the first number is row and second col) and end with iv88 is there any way to define all of them together?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
SMMousaviSP
  • 624
  • 7
  • 23

2 Answers2

0

You should populate your views with an adapter. Since it sounds like you need the images to be displayed in a grid, you could use GridView with an adapter.

There are lot of tutorials that would guide you to do this; eg: you can look at this for a start.

Hope this helps.

Supriya
  • 1,940
  • 24
  • 23
0

Construct your ImageView id by for loops and call getResources().getIdentifier()

for (int col = 0 ; col < 8; ++col) {
    for (int row = 0 ; row < 8; ++row) {
        String idStr = "iv" + String.valueOf(row) + String.valueOf(col);
        @IdRes int id = context.getResources().getIdentifier(idStr, "id", context.getPackageName());

        ImageView imgView = findViewById(id);
        // Do something with your image
    }
}

Anyway, with that much ImageView on screen, it should be managed by an adapter like ListView, RecyclerView...

Tam Huynh
  • 2,026
  • 1
  • 16
  • 20