How to support `layout_columnWeight` and `layout_rowWeight` in pre API 21?

11,227

Solution 1

the version of GridLayout in support library is backward compatible and supports weights as mentioned here:

https://developer.android.com/reference/android/support/v7/widget/GridLayout.html

so you just need to add compile 'com.android.support:gridlayout-v7:23.1.1' to your build.gradle file and use support gridlayout instead ;)

use it like below (android.support.v7.widget.GridLayout) in your layout xml file:

<android.support.v7.widget.GridLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container_grid"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:columnCount="2"
    app:rowCount="3"
    app:orientation="horizontal">

    <View
        android:id="@+id/view_red"
        android:layout_height="100dp"
        android:layout_width="100dp"
        app:layout_columnWeight="1"
        app:layout_rowWeight="1"
        app:layout_gravity="center"
        android:background="#ff0000"
        app:layout_row="0"
        app:layout_column="0" />
</android.support.v7.widget.GridLayout>

Solution 2

For beginners like me @Amir Ziarati's answer is on point. you need to add this line in your build.gradle (Module:app)

implementation 'com.android.support:gridlayout-v7:26.1.0'

Like this

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.android.support:gridlayout-v7:26.1.0'
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' 
}

And then use

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

Solution 3

Instead of Using

android:layout_columnWeight="1"
android:layout_rowWeight="1"

Use:

app:layout_columnWeight="1"
app:layout_rowWeight="1"

This will help.

Share:
11,227
Elye
Author by

Elye

Android Design, Development and Deployment

Updated on June 08, 2022

Comments

  • Elye
    Elye about 2 years

    I use the below grid layout with layout_columnWeight and layout_rowWeight to centralize my view in the grid cell.

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    
    <GridLayout
        android:id="@+id/container_grid"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:columnCount="2"
        android:rowCount="3"
        android:orientation="horizontal">
    
        <View
            android:id="@+id/view_red"
            android:layout_height="100dp"
            android:layout_width="100dp"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"
            android:layout_gravity="center"
            android:background="#ff0000"
            android:layout_row="0"
            android:layout_column="0" />
    
        <View
            android:id="@+id/view_green"
            android:layout_height="100dp"
            android:layout_width="100dp"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"
            android:layout_gravity="center"
            android:background="#00ff00"
            android:layout_row="0"
            android:layout_column="0" />
    
        <View
            android:id="@+id/view_blue"
            android:layout_height="100dp"
            android:layout_width="100dp"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"
            android:layout_gravity="center"
            android:background="#0000ff"
            android:layout_row="0"
            android:layout_column="0" />
        </GridLayout>
    </RelativeLayout>
    

    But they are for v21 and above only. How to support layout_columnWeight and layout_rowWeight feature in pre API 21?

  • Elye
    Elye over 7 years
    I got wrong namspace error on various attribute in the xml :(
  • Amir Ziarati
    Amir Ziarati over 7 years
    did you add the library to the build.gradle file ?
  • Elye
    Elye over 7 years
    Added compile 'com.android.support:gridlayout-v7:24.2.1'.
  • Amir Ziarati
    Amir Ziarati over 7 years
    just updated the layout i wrote. there was sth wrong with the namespace of some attributes ;)
  • Elye
    Elye over 7 years
    Not working:( It will up the entire instead of 100x100dp.
  • Amir Ziarati
    Amir Ziarati over 7 years
    i just updated the namespace to app did you recopy ?
  • Amir Ziarati
    Amir Ziarati over 7 years
    copy the whole layout becuase i changed multiple places
  • Elye
    Elye over 7 years
    It works for this case of 1 cell, but when I add another view, it doesnt shows the second column or row.
  • Amir Ziarati
    Amir Ziarati over 7 years
    did you use app namespace for other cells as well ?
  • Elye
    Elye over 7 years
    I have updated my question to have more than 1 views. Yes, I copy everything.
  • Elye
    Elye over 7 years
    Ops, I think I miss the rows and columns. Now I got it all. Thanks!!
  • Amir Ziarati
    Amir Ziarati over 7 years
    you must specify which row and index you want those view to be in . [0,0] [0,1] then [1,0][1,1] and so on (not all ofthem [0,0])
  • Elye
    Elye over 7 years
    You're right. I got it all, and paste my code onto your answer. Thanks!!. I upvote and tick your answer :)
  • greybeard
    greybeard over 4 years
    Please argue why to prefer this over "the support approach" in other answers.
  • Emmanuel Njorodongo
    Emmanuel Njorodongo almost 3 years
    app: layout_columnWeight) not found