3

How to get column count in Gridview when gridview noofColumn="autofit" mode

How to get single row no of column

<GridView
                android:id="@+id/gridview"
                android:layout_width="fill_parent"
                android:layout_height="match_parent"

                android:background="@android:color/white"
                android:gravity="center"
                android:numColumns="auto_fit"
                android:stretchMode="columnWidth" />
Sanket990
  • 665
  • 1
  • 10
  • 22

2 Answers2

7

You can get column count in java code..

GridView mGridView = (GridView)findViewById(R.id.gridview);
int column_coutnt = mGridView.getNumColumns();

Second Way:

int columns = 0;
int children = mGridView.getChildCount();
if (children > 0) {
     int width = mGridView.getChildAt(0).getMeasuredWidth();
     if (width > 0) {
        columns = mGridView.getWidth() / width;
     }
 }            
Niranj Patel
  • 32,980
  • 10
  • 97
  • 133
  • @Sanket990 [check this](http://stackoverflow.com/questions/12231835/how-to-get-the-number-of-rows-of-a-gridview) and [this one](http://stackoverflow.com/a/7874011/624069) – Niranj Patel Jun 27 '14 at 07:17
1

Try like this..

Math.ceil( (double)gridView.getCount() / (double)gridView.getNumColumns() );
Giridharan
  • 4,402
  • 5
  • 27
  • 30