Search code examples
androidandroid-studioandroid-gridlayoutmaterialcardview

Attribute layout_columnWeight not found in MaterialCardView inside Gridlayout


I am using the latest Grid Layout implemented in Gradle as below:


implementation 'com.google.android.material:material:1.3.0'

Below is my MaterialCardView Inside a GridLayout


 <GridLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="3"
            android:columnCount="2"
            android:rowCount="2">

            <com.google.android.material.card.MaterialCardView
                android:id="@+id/mcv_card_1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:clickable="true"
                android:focusable="true"
                android:theme="@style/MaterialTheme"
                app:cardCornerRadius="5dp"
                app:cardElevation="1dp"
                app:cardUseCompatPadding="true"
                app:contentPadding="10dp"
                app:strokeWidth="1dp"
                app:rippleColor="@color/secondaryRippleColor5"
                app:layout_columnWeight="1"
                app:layout_rowWeight="1">

            </com.google.android.material.card.MaterialCardView>
            
        </GridLayout>

And i am getting the following error


AAPT: error: attribute layout_columnWeight (aka myapps.ke.playapp:layout_columnWeight) not found.
AAPT: error: attribute layout_layout_rowWeight (aka myapps.ke.playapp:layout_rowWeight) not found.

I have tried changing app:layout_columnWeight to android:layout_columnWeight and works fine but its not backward compatible and only supports Api Level 21 and above


Solution

  • You need to add the androidx GridLayout to your dependencies:

    implementation "androidx.gridlayout:gridlayout:1.0.0"

    and then in your XML use:

    <androidx.gridlayout.widget.GridLayout
      ....>
    
    </androidx.gridlayout.widget.GridLayout>
    

    instead of the framework one