Sanket990
Sanket990

Reputation: 665

how to get columncount in gridview

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" />

Upvotes: 3

Views: 3139

Answers (2)

Niranj Patel
Niranj Patel

Reputation: 33258

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;
     }
 }            

Upvotes: 7

Giridharan
Giridharan

Reputation: 4462

Try like this..

Math.ceil( (double)gridView.getCount() / (double)gridView.getNumColumns() );

Upvotes: 1

Related Questions