'How to make a photo grid?
I'm new to android development and I'm trying to make a photo grid of even squares – three in a row, N number of rows. I have some idea, but it doesn't work correctly. It turns out a white screen. Only if you don't set the size for ConstraintLayout in photo.xml. And I need a row of exactly three squares stretched across the width of the screen. Here are the layouts and activity. How do you see the solution? Thank you so much!
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<GridLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp">
<ImageView
android:layout_width="fill_parent"
android:layout_height="0dp"
android:src="@color/accent1_300"
app:layout_constraintDimensionRatio="1:1"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
</GridLayout>
photo.xml:
<?xml version="1.0" encoding="utf-8"?>
<GridLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp">
<ImageView
android:layout_width="fill_parent"
android:layout_height="0dp"
android:src="@color/accent1_300"
app:layout_constraintDimensionRatio="1:1"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
</GridLayout>
MainActivity.java
package com.kennyromanov.home_gallery;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.GridLayout;
import android.widget.TableRow;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridLayout Content = findViewById(R.id.Content);
for (int i = 0; i < 10; i++) {
View photo = getLayoutInflater().inflate(R.layout.photo, null);
Content.addView(photo);
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
