I want to create a GridLayout able to run in all the APIs.

The thing is, when i use **GridLayout** instead of **android.support.v7.widget.GridLayout** the app runs fine in Android 7.1.1 but in older versions it crashes.

But if i use **android.support.v7.widget.GridLayout** instead of **GridLayout** (like the code below) it always crashes.

> 
> activity\_main 
> 
> 
> 

```
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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="#FFF"
    tools:context="devector.dom.gridtest.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="wrap_content">

    <RelativeLayout

        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textGrid"
            android:text="Grid test"
            android:textColor="#000"
            android:layout_centerInParent="true"
            android:textSize="34sp"
            />
    </RelativeLayout>

    <android.support.v7.widget.GridLayout
        android:textAlignment="center"
        android:id="@+id/mainGrid"
        android:padding="25dp"
        android:layout_marginLeft="8dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:columnCount="2"
        app:rowCount="3"
        app:alignmentMode="alignMargins"
        app:columnOrderPreserved="false">

        <!-- Row 1 -->
        <!-- Column 1-->

        <android.support.v7.widget.CardView
            android:layout_width="150dp"
            android:layout_height="125dp"
            android:layout_marginBottom="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            app:cardElevation="8dp"
            app:cardCornerRadius="8dp"
            >

            <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/zerotwo"
                    android:layout_gravity="center_horizontal"
                    android:layout_width="70dp"
                    android:layout_height="70dp"
                    />

                <TextView
                    android:text="Zero"
                    android:textAlignment="center"
                    android:textColor="#000"
                    android:textSize="18sp"
                    android:textStyle="bold"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </LinearLayout>

        </android.support.v7.widget.CardView>

        <!-- Row 2 -->
        <!-- Column 2-->

        <android.support.v7.widget.CardView
            android:layout_width="150dp"
            android:layout_height="125dp"
            android:layout_marginBottom="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            app:cardElevation="8dp"
            app:cardCornerRadius="8dp"
            >

            <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/zerotwo"
                    android:layout_gravity="center_horizontal"
                    android:layout_width="70dp"
                    android:layout_height="70dp"
                    />

                <TextView
                    android:text="Two"
                    android:textAlignment="center"
                    android:textColor="#000"
                    android:textSize="18sp"
                    android:textStyle="bold"

                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </LinearLayout>

        </android.support.v7.widget.CardView>

        <!-- Row 3-->
        <!-- Column 3-->

        <android.support.v7.widget.CardView
            android:layout_width="150dp"
            android:layout_height="125dp"
            android:layout_marginBottom="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            app:cardElevation="8dp"
            app:cardCornerRadius="8dp"
            >

            <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/zerotwo"
                    android:layout_gravity="center_horizontal"
                    android:layout_width="70dp"
                    android:layout_height="70dp"
                    />

                <TextView
                    android:text="Is"
                    android:textAlignment="center"
                    android:textColor="#000"
                    android:textSize="18sp"
                    android:textStyle="bold"

                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </LinearLayout>

        </android.support.v7.widget.CardView>

        <!-- Row 4 -->
        <!-- Column 4-->

        <android.support.v7.widget.CardView
            android:layout_width="150dp"
            android:layout_height="125dp"
            android:layout_marginBottom="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            app:cardElevation="8dp"
            app:cardCornerRadius="8dp"
            >

            <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/zerotwo"
                    android:layout_gravity="center_horizontal"
                    android:layout_width="70dp"
                    android:layout_height="70dp"
                    />

                <TextView
                    android:text="The"
                    android:textAlignment="center"
                    android:textColor="#000"
                    android:textSize="18sp"
                    android:textStyle="bold"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </LinearLayout>

        </android.support.v7.widget.CardView>

        <!-- Row 5 -->
        <!-- Column 5-->

        <android.support.v7.widget.CardView
            android:layout_width="150dp"
            android:layout_height="125dp"
            android:layout_marginBottom="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            app:cardElevation="8dp"
            app:cardCornerRadius="8dp"
            >

            <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/zerotwo"
                    android:layout_gravity="center_horizontal"
                    android:layout_width="70dp"
                    android:layout_height="70dp"
                    />

                <TextView
                    android:text="Besto"
                    android:textAlignment="center"
                    android:textColor="#000"
                    android:textSize="18sp"
                    android:textStyle="bold"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </LinearLayout>

        </android.support.v7.widget.CardView>

        <!-- Row 6 -->
        <!-- Column 6-->

        <android.support.v7.widget.CardView
            android:layout_width="150dp"
            android:layout_height="125dp"
            android:layout_marginBottom="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            app:cardElevation="8dp"
            app:cardCornerRadius="8dp"
            >

            <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/zerotwo"
                    android:layout_gravity="center_horizontal"
                    android:layout_width="70dp"
                    android:layout_height="70dp"
                    />

                <TextView
                    android:text="Waifu"
                    android:textAlignment="center"
                    android:textColor="#000"
                    android:textSize="18sp"
                    android:textStyle="bold"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </LinearLayout>

        </android.support.v7.widget.CardView>

        <!-- Row 7 -->
        <!-- Column 7-->

        <android.support.v7.widget.CardView
            android:layout_width="150dp"
            android:layout_height="125dp"
            android:layout_marginBottom="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            app:cardElevation="8dp"
            app:cardCornerRadius="8dp"
            >

            <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/zerotwo"
                    android:layout_gravity="center_horizontal"
                    android:layout_width="70dp"
                    android:layout_height="70dp"
                    />

                <TextView
                    android:text="Never"
                    android:textAlignment="center"
                    android:textColor="#000"
                    android:textSize="18sp"
                    android:textStyle="bold"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </LinearLayout>

        </android.support.v7.widget.CardView>

        <!-- Row 8 -->
        <!-- Column 8-->

        <android.support.v7.widget.CardView
            android:layout_width="150dp"
            android:layout_height="125dp"
            android:layout_marginBottom="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            app:cardElevation="8dp"
            app:cardCornerRadius="8dp"
            >

            <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/zerotwo"
                    android:layout_gravity="center_horizontal"
                    android:layout_width="70dp"
                    android:layout_height="70dp"
                    />

                <TextView
                    android:text="Forget"
                    android:textAlignment="center"
                    android:textColor="#000"
                    android:textSize="18sp"
                    android:textStyle="bold"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </LinearLayout>

        </android.support.v7.widget.CardView>

        <!-- Row 9 -->
        <!-- Column 9-->

        <android.support.v7.widget.CardView
            android:layout_width="150dp"
            android:layout_height="125dp"
            android:layout_marginBottom="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            app:cardElevation="8dp"
            app:cardCornerRadius="8dp"
            >

            <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/zerotwo"
                    android:layout_gravity="center_horizontal"
                    android:layout_width="70dp"
                    android:layout_height="70dp"
                    />

                <TextView
                    android:text="About"
                    android:textAlignment="center"
                    android:textColor="#000"
                    android:textSize="18sp"
                    android:textStyle="bold"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </LinearLayout>

        </android.support.v7.widget.CardView>

        <!-- Row 10 -->
        <!-- Column 10-->

        <android.support.v7.widget.CardView
            android:layout_width="150dp"
            android:layout_height="125dp"
            android:layout_marginBottom="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            app:cardElevation="8dp"
            app:cardCornerRadius="8dp"
            >

            <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/zerotwo"
                    android:layout_gravity="center_horizontal"
                    android:layout_width="70dp"
                    android:layout_height="70dp"
                    />

                <TextView
                    android:text="It"
                    android:textAlignment="center"
                    android:textColor="#000"
                    android:textSize="18sp"
                    android:textStyle="bold"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </LinearLayout>

        </android.support.v7.widget.CardView>
    </android.support.v7.widget.GridLayout>
    </LinearLayout>

</ScrollView>

```

> 
> Logs
> 
> 
> 

```
FATAL EXCEPTION: main
Process: devector.dom.gridtest, PID: 15773
java.lang.RuntimeException: Unable to start activity ComponentInfo{devector.dom.gridtest/devector.dom.gridtest.MainActivity}: java.lang.ClassCastException: android.support.v7.widget.GridLayout cannot be cast to android.widget.GridLayout
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2678)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2743)
    at android.app.ActivityThread.-wrap12(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1490)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6165)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)
Caused by: java.lang.ClassCastException: android.support.v7.widget.GridLayout cannot be cast to android.widget.GridLayout
    at devector.dom.gridtest.MainActivity.onCreate(MainActivity.java:21)
    at android.app.Activity.performCreate(Activity.java:6687)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1140)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2631)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2743) 
    at android.app.ActivityThread.-wrap12(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1490) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6165) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778) 

```

> 
> Gradle
> 
> 
> 

```
apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "devector.dom.gridtest"
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support:cardview-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    implementation 'com.android.support:gridlayout-v7:26.1.0'
    implementation 'com.android.support:design:26.1.0'

}

```

> 
> MainActivity.java
> 
> 
> 

```
package devector.dom.gridtest;

import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.view.View;
import android.widget.GridLayout;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    GridLayout mainGrid;

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

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

        setSingleEvent(mainGrid);
    }

    private void setSingleEvent(GridLayout mainGrid) {

        for (int i = 0; i < mainGrid.getChildCount(); i++) {
            CardView cardView = (CardView) mainGrid.getChildAt(i);
            final int finalI = i;
            cardView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                  if(finalI == 0) 
                  {
                      Intent intent = new Intent(MainActivity.this,Activity_one.class );
                      startActivity(intent);
                  } 

                }
            });
        }
    }
}

```