Reputation: 665
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
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
Reputation: 4462
Try like this..
Math.ceil( (double)gridView.getCount() / (double)gridView.getNumColumns() );
Upvotes: 1