I am looking for a simple way to make a 2-dimensional array of icons for a board game on android. I wrote a game in java but it uses swing and I am now trying to rewrite it for android. I have searched all over and have found ways to make the 2-d array but I don’t know how to fill it with icons now. Any help would be appreciated. Here is my code from java using the swing lib.):
spots = new JButton[15][15];
grid = new JPanel(new GridLayout(15, 15));
for (int i = 0; i < spots.length; i++) {
for (int j = 0; j < spots.length; j++) {
spots[i][j] = new JButton();
spots[i][j].setActionCommand(i + ":" + j);
spots[i][j].addActionListener(this);
grid.add(spots[i][j]);
spots[i][j].setIcon(backgroundIcon);
}
}
You well need to create a
ImageView[][]to hold your icons. And you can use theGridViewto display them. It’s a rather fixed-size problem so I would recommend using anImageView[]orList<ImageView>. Have a look at this sample.Note: you can set the
OnClickListenerof an ImageView.