0

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

Emmanuel Njorodongo
  • 1,004
  • 17
  • 35

2 Answers2

1

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

MatPag
  • 41,742
  • 14
  • 105
  • 114
0

layout_columnWeight is a part of android standard libraries, so you need to change app:layout_columnWeight to android:layout_columnWeight

Zain
  • 37,492
  • 7
  • 60
  • 84
  • When i was using android.support.v7.widget.GridLayout before Android X it was just working fine but i dont know why now its failing – Emmanuel Njorodongo Jul 06 '21 at 14:45
  • @EmmanuelNjorodongo If you want to use the standard libraries `GridLayout` then you have to use `android` namespace , if you want to use the support one i.e. `androidx.gridlayout`; then you have to add the dependency pointed out by MatPag answer.. in this case you can use the app namespace – Zain Jul 06 '21 at 14:54