Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting rowcount and column count for grid layout dynamically

I am adding LinearLayouts to GridLayout dynamically. I am looking them to be displayed as a grid view.
Is it possible to calculate how many linear layouts can be placed in one row and set column count for grid layout?
Should I use gridview to align them automatically?

like image 415
vignesh Avatar asked Mar 23 '26 23:03

vignesh


1 Answers

If you want to calculate the column count based on the width of the GridLayout, override the onMeasure method. It provides widthSpec and heightSpec as parameters, from which you can get the actual width and height in pixels using MeasureSpec.getSize(). From there, calculate how many columns you'd like to show based on the width of the GridLayout you just found, and use setColumnCount to make it display that number of columns.

like image 132
oakes Avatar answered Mar 26 '26 04:03

oakes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!