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

3

17

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?

Spasmodic answered 10/10, 2016 at 8:43 Comment(0)
I
24

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>
Inquisitor answered 10/10, 2016 at 8:57 Comment(13)
I got wrong namspace error on various attribute in the xml :(Spasmodic
did you add the library to the build.gradle file ?Inquisitor
Added compile 'com.android.support:gridlayout-v7:24.2.1'.Spasmodic
just updated the layout i wrote. there was sth wrong with the namespace of some attributes ;)Inquisitor
Not working:( It will up the entire instead of 100x100dp.Spasmodic
i just updated the namespace to app did you recopy ?Inquisitor
copy the whole layout becuase i changed multiple placesInquisitor
It works for this case of 1 cell, but when I add another view, it doesnt shows the second column or row.Spasmodic
did you use app namespace for other cells as well ?Inquisitor
I have updated my question to have more than 1 views. Yes, I copy everything.Spasmodic
Ops, I think I miss the rows and columns. Now I got it all. Thanks!!Spasmodic
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])Inquisitor
You're right. I got it all, and paste my code onto your answer. Thanks!!. I upvote and tick your answer :)Spasmodic
A
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>
Apocynthion answered 19/7, 2018 at 14:25 Comment(0)
K
2

Instead of Using

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

Use:

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

This will help.

Kenning answered 21/3, 2020 at 19:19 Comment(2)
Please argue why to prefer this over "the support approach" in other answers.Symbolism
app: layout_columnWeight) not foundSchall

© 2022 - 2024 — McMap. All rights reserved.