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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With