as_grid

Bron Website Project name Grid
compileSdkVersion 28 minSdkVersion 15
buildToolsVersion 28.0.3 targetSdkVersion 28 
Opmerking plaats afbeeldingen drawable ic_back1 – 6

build.gradle

dependencies {
    ...
    implementation 'com.android.support:cardview-v7:28.0.3'
    ...
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="match_parent"
    android:orientation="vertical"
    android:background="#D3D3D3"
    android:weightSum="10"
    tools:context="com.example.grid.MainActivity">
    <RelativeLayout
        android:layout_weight="2"
        android:layout_width="match_parent"
        android:layout_height="0dp">
        <TextView
            android:id="@+id/textGrid"
            android:text="GRID LAYOUT"
            android:textSize="34sp"
            android:textColor="#000"
            android:layout_centerInParent="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </RelativeLayout>
    <GridLayout
        android:id="@+id/mainGrid"
        android:rowCount="3"
        android:columnCount="2"
        android:alignmentMode="alignMargins"
        android:columnOrderPreserved="false"
        android:layout_weight="8"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:padding="14dp">
        <!-- ROW 1 -->
        <!-- COLUMN 1 -->
        <androidx.cardview.widget.CardView> android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_marginBottom="16dp" android:layout_rowWeight="1" android:layout_marginLeft="16dp" android:layout_marginRight="16dp" app:cardElevation="8dp" app:cardCornerRadius="8dp"&gt; <LinearLayout
                android:layout_gravity="center_horizontal|center_vertical"
                android:layout_margin="16dp"
                android:orientation="vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"><ImageView
                    android:src="@drawable/ic_back1"
                    android:layout_gravity="center_horizontal"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" /><TextView
                    android:textAlignment="center"
                    android:text="Text"
                    android:textColor="#000"
                    android:textSize="18sp"
                    android:textStyle="bold"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" /></LinearLayout></androidx.cardview.widget.CardView>
        <!-- COLUMN 2 -->
        <androidx.cardview.widget.CardView> android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_marginBottom="16dp" android:layout_rowWeight="1" android:layout_marginLeft="16dp" android:layout_marginRight="16dp" app:cardElevation="8dp" app:cardCornerRadius="8dp"&gt; <LinearLayout
                android:layout_gravity="center_horizontal|center_vertical"
                android:layout_margin="16dp"
                android:orientation="vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"><ImageView
                    android:src="@drawable/ic_back2"
                    android:layout_gravity="center_horizontal"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" /><TextView
                    android:textAlignment="center"
                    android:text="Text2"
                    android:textColor="#000"
                    android:textSize="18sp"
                    android:textStyle="bold"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" /></LinearLayout></androidx.cardview.widget.CardView>
        <!-- ROW 2 -->
        <!-- COLUMN 1 -->
        <androidx.cardview.widget.CardView> android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_marginBottom="16dp" android:layout_rowWeight="1" android:layout_marginLeft="16dp" android:layout_marginRight="16dp" app:cardElevation="8dp" app:cardCornerRadius="8dp"&gt; <LinearLayout
                android:layout_gravity="center_horizontal|center_vertical"
                android:layout_margin="16dp"
                android:orientation="vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"><ImageView
                    android:src="@drawable/ic_back3"
                    android:layout_gravity="center_horizontal"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" /><TextView
                    android:textAlignment="center"
                    android:text="Text3"
                    android:textColor="#000"
                    android:textSize="18sp"
                    android:textStyle="bold"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" /></LinearLayout></androidx.cardview.widget.CardView>
        <!-- COLUMN 2 -->
        <androidx.cardview.widget.CardView> android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_marginBottom="16dp" android:layout_rowWeight="1" android:layout_marginLeft="16dp" android:layout_marginRight="16dp" app:cardElevation="8dp" app:cardCornerRadius="8dp"&gt; <LinearLayout
                android:layout_gravity="center_horizontal|center_vertical"
                android:layout_margin="16dp"
                android:orientation="vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"><ImageView
                    android:src="@drawable/ic_back4"
                    android:layout_gravity="center_horizontal"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" /><TextView
                    android:textAlignment="center"
                    android:text="Text4"
                    android:textColor="#000"
                    android:textSize="18sp"
                    android:textStyle="bold"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" /></LinearLayout></androidx.cardview.widget.CardView>
        <!-- ROW 3 -->
        <!-- COLUMN 1 -->
        <androidx.cardview.widget.CardView> android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_marginBottom="16dp" android:layout_rowWeight="1" android:layout_marginLeft="16dp" android:layout_marginRight="16dp" app:cardElevation="8dp" app:cardCornerRadius="8dp"&gt; <LinearLayout
                android:layout_gravity="center_horizontal|center_vertical"
                android:layout_margin="16dp"
                android:orientation="vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"><ImageView
                    android:src="@drawable/ic_back5"
                    android:layout_gravity="center_horizontal"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" /><TextView
                    android:textAlignment="center"
                    android:text="Text"
                    android:textColor="#000"
                    android:textSize="18sp"
                    android:textStyle="bold"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" /></LinearLayout></androidx.cardview.widget.CardView>
        <!-- COLUMN 2 -->
        <androidx.cardview.widget.CardView> android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_marginBottom="16dp" android:layout_rowWeight="1" android:layout_marginLeft="16dp" android:layout_marginRight="16dp" app:cardElevation="8dp" app:cardCornerRadius="8dp"&gt; <LinearLayout
                android:layout_gravity="center_horizontal|center_vertical"
                android:layout_margin="16dp"
                android:orientation="vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"><ImageView
                    android:src="@drawable/ic_back6"
                    android:layout_gravity="center_horizontal"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" /><TextView
                    android:textAlignment="center"
                    android:text="Text2"
                    android:textColor="#000"
                    android:textSize="18sp"
                    android:textStyle="bold"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" /></LinearLayout></androidx.cardview.widget.CardView>
    </GridLayout>
</LinearLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {

    GridLayout gridLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        gridLayout=(GridLayout)findViewById(R.id.mainGrid);

        setSingleEvent(gridLayout);

    }
    private void setSingleEvent(GridLayout gridLayout) {
        for(int i = 0; i&lt;gridLayout.getChildCount();i++){
            CardView cardView=(CardView)gridLayout.getChildAt(i);
            final int finalI= i;
            cardView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(MainActivity.this,"Clicked at index "+ finalI,
                            Toast.LENGTH_SHORT).show();
                }
            });
        }
    }


}