Reputation: 2227
I am having an array of images. I need to display 3 images in a row and then the next in another row. How to do this using nested for loops. can anyone help me over this? thanks.
Upvotes: 0
Views: 2199
Reputation: 128428
Its simple, use GridView with 3 columns.
<GridView
android:layout_height="wrap_content"
android:id="@+id/gridView1"
android:layout_width="match_parent"
android:numColumns="3"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp">
Upvotes: 1
Reputation: 4370
The basic nested loop structure looks like this:
int imageIndex = 0;
for (int row = 0; row < rowCount; row++) {
for (int column = 0; column < columnCount; column++ {
// Draw your image here at x position of (column * image width)
// and y position of (row * image height). Add a bit to each if you
// want some spacing between your images.
// For example:
drawMyImage(images[imageIndex++], column * imageWidth, row * imageHeight);
}
}
Upvotes: 0
Reputation: 8245
You need two loops. The outer loop is for the rows. Each element in the row corresponds with a column, so the inner loop is for the columns.
Upvotes: 0