How to support `layout_columnWeight` and `layout_rowWeight` in pre API 21?
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.
![Elye](https://i.stack.imgur.com/yTcB3.png?s=256&g=1)
Comments
-
Elye about 2 years
I use the below grid layout with
layout_columnWeight
andlayout_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
andlayout_rowWeight
feature in pre API 21? -
Elye over 7 yearsI got wrong namspace error on various attribute in the xml :(
-
Amir Ziarati over 7 yearsdid you add the library to the build.gradle file ?
-
Elye over 7 yearsAdded
compile 'com.android.support:gridlayout-v7:24.2.1'
. -
Amir Ziarati over 7 yearsjust updated the layout i wrote. there was sth wrong with the namespace of some attributes ;)
-
Elye over 7 yearsNot working:( It will up the entire instead of 100x100dp.
-
Amir Ziarati over 7 yearsi just updated the namespace to app did you recopy ?
-
Amir Ziarati over 7 yearscopy the whole layout becuase i changed multiple places
-
Elye over 7 yearsIt works for this case of 1 cell, but when I add another view, it doesnt shows the second column or row.
-
Amir Ziarati over 7 yearsdid you use
app
namespace for other cells as well ? -
Elye over 7 yearsI have updated my question to have more than 1 views. Yes, I copy everything.
-
Elye over 7 yearsOps, I think I miss the rows and columns. Now I got it all. Thanks!!
-
Amir Ziarati over 7 yearsyou 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 over 7 yearsYou're right. I got it all, and paste my code onto your answer. Thanks!!. I upvote and tick your answer :)
-
greybeard over 4 yearsPlease argue why to prefer this over "the support approach" in other answers.
-
Emmanuel Njorodongo almost 3 yearsapp: layout_columnWeight) not found